This may be a very stupid question but
Does anyone know why i am not getting the second bit equals to the mean (100)?
#beta=4, alpha=5, mean=20
qgamma(0.5, 5, 1/4)
# 18.68364
#beta=2500, alpha=0.04, mean=100
qgamma(0.5,0.04,1/2500)
# 0.00004320412
It is because you are using the quantile function, and qgamma(0.5, shape, scale) corresponds to the median - not the mean as you are expecting.
See the example below;
x <- rgamma(50000, shape = 0.04, scale = 2500)
mean(x)
# [1] 98.82911
median(x)
# [1] 3.700012e-05
Related
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)
I have a simple vector, for instance :
a <- c(-1.02, 2.25, 9.12, -2.09, 0.02)
I need to rescale it to an average of 100. But I really don't find the solution in order to solve my problem...
I tried with scale() function in order to rescale the values but however we cannot specify the mean.
I want to have in output when i calculate the mean of the vector : > 100
Thanks in advance for your help !
What about:
rescaled <- a/mean(a)*100
rescaled
[1] -61.594203 135.869565 550.724638 -126.207729 1.207729
mean(rescaled)
[1] 100
scale scales to a mean value of 0, keeping the standard error or scaling it to unity. So just add the desired mean to scale(a) to get a vector with the new mean
b1 <- c(scale(a)) + 100
mean(b1)
#[1] 100
b1
#[1] 99.40138 100.13288 101.66969 99.16202 99.63403
b2 <- c(scale(a, scale = FALSE)) + 100
mean(b2)
#[1] 100
b2
#[1] 97.324 100.594 107.464 96.254 98.364
Note that b2 is equal to
a - mean(a) + 100
#[1] 97.324 100.594 107.464 96.254 98.364
We can use
library(scales)
rescale(a, to = c(0, mean(a))) * 100
Perhaps you can use scale like below
scale(a) + 100
I have a question:
If X is a random variable in a density function which is uniform between -2 and 3.
I want to find these two questions:
What is the upper quartile of X?
What is the 44% quantile of X?
Now the things I have tried are below:
z <- 1 - punif(0.75, min = -2, max = 3, lower.tail = TRUE)
answer: 0.45
y <- qunif(0.44, min = -2, max = 3, lower.tail = TRUE)
answer: 0.2
First is this even the right way to go about it.
Second, I understand that Punif finds the accumulate probability of X. What does qunif find, and what does the result tell me about X and the distribution?
If is you have a random variable x with uniform distribution from a to b
X ~ U(a,b)
Then punif(x, a, b) is the probability that U <= x
And qunif(x, a, b) finds the value y such that Pr(U <= y)=x
You can visualize these plots with
curve(punif(x, -2, 3), from=-2, to=3, main="punif")
curve(qunif(x, -2, 3), from=0, to=1, main="qunif")
Note how punif expects a value anywhere between a and b but qunif expects a probability so it must be between 0 and 1.
When I want to generate a random number with runif() within a specific interval under exclusion of a particular value (e.g. 0.5) I can write this function ex.runif() who does the job, but it is hundreds of times slower than the normal runif(). Could anyone point me to a better solution?
ex.runif <- function(n, excl, min, max) {
# ex.runif() excludes the specific value 'excl'
q <- excl
while (q == excl) {
q <- runif(n, min = min, max = max)
}
return(q)
}
set.seed(42)
ex.runif(1, .5, .25, .75) # exclude .5, interval [.25, .75]
# [1] 0.707403
library(microbenchmark)
microbenchmark(ex.runif(1, .5, .25, .75), runif(1, min = .25, max = .75))
# Unit: microseconds
# expr min lq mean median uq max neval cld
# ex.runif 692.439 704.685 721.51135 715.2735 722.9275 962.373 100 b
# runif 2.041 2.551 3.49044 2.8070 3.3170 21.176 100 a
If the set of values that you want to exclude is finite, then, in most cases, there is no need for a function like that. The reason is that the uniform distribution is continuous and any finite number of values are taken with probability zero. That is, q == excl is, in terms of probability theory, true with probability zero.
For instance,
set.seed(42)
ex.runif(5, .5, .25, .75)
# [1] 0.7074030 0.7185377 0.3930698 0.6652238 0.5708728
set.seed(42)
runif(5, 0.25, 0.75)
# [1] 0.7074030 0.7185377 0.3930698 0.6652238 0.5708728
The same is most likely going to happen under any other seed as well. Thus, you may just keep using runif.
#duckmayr makes a good point about numeric precision. In fact, as the interval [min, max] is getting narrower, q == excl becomes true with increasingly high probability and, in some applications, it may even become relevant.
However, if in theory you indeed need to exclude only a single value 0.5, then performing a check like q == excl might even do harm by excluding unnecessary draws.
For instance, in my case .Machine$double.eps is 2.220446e-16. Then the probability of getting a draw from [0.5 - .Machine$double.eps / 4, 0.5 + .Machine$double.eps / 4] when [min,max] is [0.5 - 10^(-k), 0.5 + 10^(-k)] and making a false conclusion is 2 * (2.220446e-16 / 4) / (2 * 10^(-k)) or around 0.55 * 10^(k-16).
I want to find the mode (x-value) of a univariate density function using R
s optimize function
I.e. For a standard normal function f(x) ~ N(3, 1) the mode should be the mean i.e. x=3.
I tried the following:
# Define the function
g <- function(x) dnorm(x = x, mean = 3, sd = 1)
Dvec <- c(-1000, 1000)
# First get the gradient of the function
gradfun <- function(x){grad(g, x)}
# Find the maximum value
x_mode <- optimize(f=g,interval = Dvec, maximum=TRUE)
x_mode
This gives the incorrect value of the mode as:
$maximum
[1] 999.9999
$objective
[1] 0
Which is incorrect i.e. gives the max value of the (-1000, 1000) interval as opposed to x=3.
Could anyone please help edit the optimisation code.
It will be used to pass more generic functions of x if this simple test case works
I would use optim for this, avoiding to mention the interval. You can tailor the seed by taking the maximum of the function on the original guessed interval:
guessedInterval = min(Dvec):max(Dvec)
superStarSeed = guessedInterval[which.max(g(guessedInterval))]
optim(par=superStarSeed, fn=function(y) -g(y))
#$par
#[1] 3
#$value
#[1] -0.3989423
#$counts
#function gradient
# 24 NA
#$convergence
#[1] 0
#$message
#NULL