How to jitter the values only in positive range - r

I have the following code:
set.seed(1)
jitter(rep(0, 7))
#> [1] -0.009379653 -0.005115044 0.002914135 0.016328312 -0.011932723
#> [6] 0.015935587 0.017787011
Notice that the function jitter add negative values. How can I
parameterize the function so that it will return only positive values?

If you only want a positive jitter, then perhaps you could just take the absolute value of your call:
x <- rep(0, 7)
abs(jitter(x))
[1] 0.009379653 0.005115044 0.002914135 0.016328312 0.011932723 0.015935587
[7] 0.017787011

Related

R - which of these functions is giving the correct value of the integrals?

I am trying to compute this integral in R:
I found three functions which can be used for this and they are all giving me different results. Here is the code:
integrand <- function(x){
r <- 1/x
return(r)
}
First is the option from base R:
integrate(integrand,-Inf, Inf)
Giving the result:
0 with absolute error < 0
The second is from the pracma package:
quadinf(integrand, -Inf, Inf)
Giving this output:
$Q
[1] -106.227
$relerr
[1] 108.0135
$niter
[1] 7
And the last one is from the cubature package:
cubintegrate(integrand, -Inf, Inf)
Which gives the following result:
$integral
[1] Inf
$error
[1] NaN
$neval
[1] 15
$returnCode
[1] 0
So then, which one of these is correct and which should I trust? Is it 0, infinity, or -106.227? Why are they all different in the first place?
1/x isn't integrable in [-Inf,Inf] range, because not integrable in 0.
On an integrable range, results are similar:
integrate(\(x) 1/x,1,2)
#0.6931472 with absolute error < 7.7e-15
pracma::quadinf( \(x) 1/x,1,2)
#$Q
#[1] 0.6931472
#$relerr
#[1] 7.993606e-15
#$niter
#[1] 4
Note that integral of 1/x in ]0,Inf] range is log(x):
log(2)-log(1)
#[1] 0.6931472

How to solve a cubic function in R

Good day to all!
I have a following cubic equation.
Left <- P^3+4*P^2+6*P
Right <- 2
How do I get R to solve for P to get Left = Right?
Thanks in advance.
1. uniroot()
You could use uniroot() to search for a root of a function with respect to its first argument.
uniroot(\(x, y) x^3 + 4*x^2 + 6*x - y, c(0, 1), y = 2, extendInt = "yes")
$root
[1] 0.278161
$f.root
[1] -1.779565e-05
$iter
[1] 6
$init.it
[1] NA
$estim.prec
[1] 6.103516e-05
2. polyroot()
If the function is a real or complex polynomial, you could specifically use polyroot(z), where z is the vector of polynomial coefficients in increasing order.
y <- 2
polyroot(c(-y, 6, 4, 1))
# [1] 0.2781631-0.000000i -2.1390815+1.616897i -2.1390815-1.616897i
Both approaches solve the equation with the root 0.278161. (Besides a real root, polyroot also gives two imaginary roots)
If you want symbolic solutions, I guess you can try Ryacas like below
> library(Ryacas)
> yac_str("Solve(P^3+4*P^2+6*P==2,P)")
[1] "{P==(71/27+Sqrt(187/27))^(1/3)-(Sqrt(187/27)-71/27)^(1/3)-4/3,P==Complex(-(4/3+((71/27+Sqrt(187/27))^(1/3)-(Sqrt(187/27)-71/27)^(1/3))/2),Sqrt(3/4)*((71/27+Sqrt(187/27))^(1/3)+(Sqrt(187/27)-71/27)^(1/3))),P==Complex(-(4/3+((71/27+Sqrt(187/27))^(1/3)-(Sqrt(187/27)-71/27)^(1/3))/2),-Sqrt(3/4)*((71/27+Sqrt(187/27))^(1/3)+(Sqrt(187/27)-71/27)^(1/3)))}"

signif doesn't give me the correct number of significant digits in R

I want to round my data with significant digits. Therefore I am using signif to achieve that.
For example:
signif(0.0054324, digits=2)
[1] 0.0054
However, I realised that there are some cases where signif doesn't work as I expected.
signif(1003.04, digits=3)
[1] 1000
signif(1005.04, digits=3)
[1] 1010
As you can see, in the previous examples, it seems that it is using ceiling or just giving me integers, without any digits.
Note that I don't want to work with round because I have some cases like this (see example below) and I don't want 0s.
round(0.000000054324, digits=2)
[1] 0
For that case, signif gives me exactly what I want.
signif(0.000000054324, digits=2)
[1] 5.4e-08
Does anyone have any idea about how can I fix that when I use "big" numbers? (Since it works perfectly with small ones). Or if you know any other method.
Thanks very much in advance
This little function should give you the results you are looking for. It works by only applying signif to the fractional part of your number, while preserving the integer part.
mysignif <- function(x, digits = 3) {
x %/% 1 + signif(x %% 1, digits)
}
mysignif(0.0054324, digits=2)
#> [1] 0.0054
mysignif(1003.04, digits=3)
#> [1] 1003.04
mysignif(1005.04, digits=3)
#> [1] 1005.04
mysignif(0.000000054324, digits=2)
#> [1] 5.4e-08
mysignif(1.0005433, digits = 3)
#> [1] 1.000543
From ?signif:
For numeric x, signif(x, dig) is the same as round(x, dig - ceiling(log10(abs(x))))
When dig > ceiling(log10(abs(x)))) this leads to negative numbers in round(), and thus you get the rounding of your large numbers.
I would fix it like this, by limiting dig - ceiling(log10(abs(x)))) to 0:
my_signif <- function(x, digits = 4) {
round(x, pmax(digits - ceiling(log10(abs(x))), 0))
}
my_signif(0.0054324, digits=2)
#> [1] 0.0054
my_signif(0.000000054324, digits=2)
#> [1] 5.4e-08
my_signif(1003.04, digits=3)
#> [1] 1003
my_signif(1005.04, digits=3)
#> [1] 1005
Created on 2022-04-04 by the reprex package (v2.0.1)

How do I optimize a function in R for all values of y?

So I'm trying to plot a function into a graph and ultimately I landed on the easiest option being to give alpha-values and optimize velocity for the x_position values. The problem is, I'm doing something wrong with the optimization.
Here's what I've got thus far:
y <- seq(0, 55, 0.1)
x_position <- function(alpha,velo)
{velo*cos(alpha)*((velo*sin(alpha)+sqrt((velo^2*sin(alpha))^2+2*16.5*9.81)/9.81))}
x <- optimize(x_position,c(1,1000),alpha=y,maximum=TRUE)$objective
Basically, I'm trying to make "y" a vector for the angle and "x" a vector of maximum function value for each angle value so that I could then plot the x,y vector for the function. The problem is, I can't get the x-vector right. For whatever reason it just keeps telling me "invalid function value in 'optimize'". Changing the optimization interval doesn't seem to accomplish anything and I'm out of ideas. The function seems to work just fine when I tested it with e.g. alpha 55 and velocity 10.
y <- seq(0, 55, 0.1)
x_position <- function(velo,alpha){
velo*cos(alpha)*((velo*sin(alpha)+sqrt((velo^2*sin(alpha))^2+2*16.5*9.81)/9.81))
}
optimize(f = x_position, interval = c(1,1000), maximum=TRUE, alpha = y[1])
#> $maximum
#> [1] 999.9999
#>
#> $objective
#> [1] 1834.098
a <- sapply(y, function(y) optimize(f = x_position, interval = c(1,1000),
maximum=TRUE, alpha = y)$objective)
head(a)
#> [1] 1834.098 10225190.493 20042734.667 29061238.316 36921162.118
#> [6] 43309155.705
Created on 2021-09-29 by the reprex package (v2.0.1)

most occurring value in a vector

I have a vector file with 1000 values. All the values were generated using Random function between 0-1.
x <- runif(100,min=0,max=1)
x
[1] 0.84620011 0.82525410 0.31622827 0.08040362 0.12894525 0.23997187 0.57177296 0.91691368 0.65751720
[10] 0.39810175 0.60632205 0.26339035 0.93543618 0.09662383 0.35147739 0.51731042 0.29151612 0.54411769
[19] 0.73688309 0.26086586 0.37808273 0.19163366 0.62776847 0.70973345 0.31802726 0.69101574 0.50042561
[28] 0.20768256 0.23555818 0.21015820 0.18221151 0.85593725 0.12916935 0.52222127 0.62269135 0.51267707
[37] 0.60164023 0.30723904 0.81990231 0.61771762 0.02502631 0.47427724 0.21250040 0.88611710 0.88648546
[46] 0.92586513 0.57015942 0.33454379 0.03572245 0.68120369 0.48692522 0.76587764 0.55214917 0.31137200
[55] 0.47170307 0.48639510 0.68922858 0.73506033 0.23541740 0.81793240 0.17184666 0.06670039 0.55664270
[64] 0.10030533 0.94620061 0.58572228 0.53333567 0.80887841 0.55015406 0.82491114 0.81251132 0.06038019
[73] 0.10918904 0.84011824 0.33169617 0.03568364 0.07703029 0.15601158 0.31623253 0.25021777 0.77024833
[82] 0.88588620 0.49044305 0.10165930 0.55494697 0.17455070 0.94458467 0.43135868 0.99313733 0.04482747
[91] 0.53453604 0.52500493 0.35496966 0.06994880 0.11377845 0.71307042 0.35086237 0.04032254 0.23744845
[100] 0.81131033
Out of all these values in the vector, I need to find the most occurring value(Or close to that). I'm new to R and have no idea what this. Please help?
One approach I have - Divide all the values in a certain ranges and find the frequency distribution. But will it be helpful?
One possibility to analyze the distribution of the numbers could consist in plotting a histogram and adding an approximate probability density distribution.
This can be done with the ggplot2 library:
set.seed(123) # used here for reproducibility
x <- runif(100) # pseudo-random numbers between 0 and 1
library(ggplot2)
p <- ggplot(as.data.frame(x),aes(x=x, y=..density..)) +
geom_histogram(fill="lightblue",colour="grey60",bins=50) +
geom_density()
The value of bins specified in geom_histigram() is the number of bars in the histogram. You may want to try to change this value to obtain a different representation of the distribution.
OR
You could use base Rand plot a simple histogram:
hist(x)
There you can also change the bin width (see breaks), but the default might be sufficient to show the concept.
You can identify which bin in this histogram has the most entries with
> hist(x)$mids[which.max(hist(x)$counts)]
#[1] 0.45
Which in this case means that most values occur near a value of 0.45 (the middle of the bin describing the range between 0.4 and 0.5).
Hope this helps.
You can do this:
set.seed(12)
x <- runif(100,min=0,max=1)
n <- length(x)
x_cut<-cut(x, breaks = n/4)
which(table(x_cut)==max(table(x_cut)))
The result depends on the breaks value you set. This is an alternative to using a histogram if you don't need one.
To really get just the most occurrent value, or when using discrete data as input, you could simply create a table, sort the results and return the highest value:
values <- c("a", "a", "c", "c", "c")
names(sort(table(values), decreasing = TRUE)[1])
#> [1] "c"
Breaking it down:
# create a table of the values
table(values)
#> a c
#> 2 3
# sort the table descending on number of occurrences
sort(table(values), decreasing = TRUE)
#> c a
#> 3 2
# now only keep the first value
sort(table(values), decreasing = TRUE)[1]
#> c
#> 3
# so the final line:
names(sort(table(values), decreasing = TRUE)[1])
#> [1] "c"
If you're feeling like wanting to do fancy stuff, create a function that does this for you:
get_mode <- function(x) {
names(sort(table(values), decreasing = TRUE)[1])
}
get_mode(values)
#> [1] "c"

Resources