Search code examples
rquantileexponential-distribution

Finding the distribution through random number generation in R


X is exponentially distributed with parameter lambda=0.5.
I want to find Pr(1/mean(X)< K)=0.95 As I don't know the distribution of 1/mean(X) I generate a distribution by the following code.

m<-c()
exponentialFunc<-function(n,lambda,nsim){
    for(i in seq(nsim)){
    x<-rexp(n,lambda)
    y<-mean(x)
    m<-c(m,1/y)
    }
    m

}   

Now m is the distribution of 1/mean(x).
Now I called this function by exponentialFunc(10,0.5,10000) and it gives a series of values for m.
But then as I want to find the quantile of this distribution which has a probability of 0.95 when I used quantile(m,0.95) the output is

> quantile(m,0.95) 95% NA
Why does it produces NA? Is there anything wrong in my code?


Solution

  • It seems that you didn't save the output of your function in an object named m:

    > m <- exponentialFunc(10,0.5,10000)
    > quantile(m,0.95)
         95% 
    0.939102 
    

    You are calling your function and not saving it anywhere, it seems that what you are doing is (basically) this:

    > m<-c()
    > quantile(m,0.95)
    95% 
     NA