2
$\begingroup$

I want to generate a dataset from the Exponential distribution, where 90% of the observations are above a threshold value 0.15. How to find the corresponding scale parameter for the Exponential distribution? How can I do this in R? I wrote the following code in R. In this case I found the scale parameter, b = 1.3 manually. But I want to repeat this process for 1000 times.

set.seed(12)
n = 50
threshold = 0.15
b = 1.3
Y <- rexp(n, b)
delta<-ifelse(threshold<=Y, 1, 0)
prop<-sum(delta==0)/n
prop
$\endgroup$

2 Answers 2

1
$\begingroup$

As @Dave already noted, you can solve your problem analytically without resorting to R. Although this is possible in this case beacuse of the very simple density function of the exponential distribution, it might nevertheless be interesting how to achive this in case of more complicated distrubutions.

The important thing to know is that R has four functions for each distribution, which are identified by their prefix. For your problem, you need the function with the prefix p, i.e. pexp, which is the CDF (distribution function) $$\mbox{pexp}(x,\lambda) = \int_{-\infty}^x f(x')\,dx'$$ You are asking for the solution $\lambda$ of the equation $$\mbox{pexp}(0.15, \lambda) = 1 - 0.9$$ which can be found numerically with uniroot:

f <- function(lambda) { return(pexp(0.15,lambda) - 0.1) }
uniroot(f, c(0,100))

When you try it out, you will find that the result is the same as teh analytical solution (as it should be) up to some numerical inaccuracy.

$\endgroup$
1
  • $\begingroup$ Thank you so much! $\endgroup$
    – score324
    Sep 18, 2020 at 19:50
2
$\begingroup$

The value of the CDF (y-axis) is the quantile, and you want quantile $0.10$ at $x=0.15$.

$$F_X(x\vert\lambda) = 1-e^{-\lambda x}$$

$$0.10 = 1-e^{-0.15\lambda }$$

Solve for lambda.

There is no reason to use R for this task, except maybe as a calculator when you need to evaluate a logarithm (but you might even prefer to use the exact value and let your software carry the decimal points so maybe you'll want to use commands like lambda <- log(...) and rexp(n, lambda)).

$\endgroup$
1
  • $\begingroup$ Thank you so much! I got it! $\endgroup$
    – score324
    Sep 18, 2020 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.