Reputation: 1987
user, I am not so familiar with mathematics nor R, but I have a probability question, with a continues probability density. I should find the time where it is 50 % probable that a battery will fail. I believe that this can be expressed as setting the area of the probability density to 0.5 (50%), and it would be very cool if it was possible to say something like this in R; f = probability density function 0.5 = exp(f, lower = 0, upper = x), and R would calculate x.
Is there some easy way to calculate the range of the integral given a lower limit and the area in R?
Upvotes: 2
Views: 1988
Reputation: 206401
This is pretty inefficient, but you it allows you to use an arbitrary density function
findprob <- function(f, interval, target) {
optimize(function(x) {
abs(integrate(f, -Inf, x)$value-target)
}, interval)$minimum
}
mydensity <- function(x) dnorm(x)
findprob(mydensity, interval=c(-1,1), target=.5)
Here we use optimize
to find a value for the integral where the distance from our target value is minimized. Note that optimize
needs an interval in which to search for a solution so you'll want to have some idea of where the point might occur.
Upvotes: 3