I am getting started in R. I need help with a command to:

"Simulate 10000 binom(20,0.3)-RV's". I would do it like:

binsim <- rbinom(10000, 20, 0.3)

Question cont.: "Let X be a binomial(20,0.3)-RV. Use simulated numbers to estimate: P(X<=5) and P(X=5)"

Okay for the first I would use: pbinom(???) and for the second dbinom(?????)

How do I connect the simulation results from `binsim and the X-RV's to get the required probabilities?

Could I do it with length(binsim(binsim<=5)/length(binsim)?

  • 3
    $\begingroup$ Is this homework? $\endgroup$ Commented Aug 4, 2012 at 15:30
  • $\begingroup$ not really, it is an exercise from the book by Brown and Murdoch, First Course in stat. progr. with R, Doing it also as a possible prep. for an actuarial course. Regards $\endgroup$ Commented Aug 7, 2012 at 13:31
  • $\begingroup$ gung, Thanks, just did it. Q: how could I store ocram's answer, will it stay in my account as long as I wish? Sorry, Q is serious. Thanks again. $\endgroup$ Commented Aug 7, 2012 at 16:55
  • $\begingroup$ No problem. CV is intended as both a simple Q&A site and also a dynamic, permanent repository of knowledge. This question will remain listed on your user page under the questions you've asked. You & any future visitor will be able to access & learn from it. $\endgroup$ Commented Aug 7, 2012 at 17:26

1 Answer 1


Functions like pbinom(), ppois(),... are used to compute the true values of $\Pr(X \leq x)$. Similarly, dbinom(), dpois(),... are used to compute true values of $\Pr(X = x)$. Here you are asked to perform simulations. Simulations are relevant when you do not have closed-form expressions for $\Pr(X \leq x)$ or $\Pr(X = x)$. The basic idea is to draw a sample and to evaluate the empirical version of the quantity of interest. For example, if you have drawn $\{0, 2, 7, 3, 10\}$, the empirical version of $\Pr(X \leq 2)$ is $$\frac{\#\{0, 2\}}{\#\{0, 2, 7, 3, 10\}}= \frac{2}{5}=0.4.$$

We do have closed-form expressions for the binomial random variable but, as pointed out by @gung, this looks like an exercice. So, here is an example to help.

Let $X \sim N(\mu=3, \sigma^2 = 1.3)$. We know that $\textrm{E}(X) = \mu = 3$ and that $\textrm{Var}(X) = \sigma^2 = 1.3$, but let us check that by simulations:

> x <- rnorm(1000, mean=3, sd=sqrt(1.3))
> mean(x)
[1] 3.078271
> var(x)
[1] 1.315806

You might find useful to type help(dbinom) first...

  • $\begingroup$ ocram, thanks, your answer helped me. Unfortunately, in my stat.-courses I never came across simulations, (as far as I remember). Sorry if you receive three times the same answer. This exercise is from book by J. Brown and D. Murdoch. $\endgroup$ Commented Aug 7, 2012 at 13:21

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.