[R] number of required trials

Rolf Turner r.turner at auckland.ac.nz
Sun Oct 19 23:49:46 CEST 2008


On 20/10/2008, at 9:51 AM, Boks, M.P.M. wrote:

>
> Dear Experts,
>
> Probably trivial, but I am struggling to get what I want:
>
> I need to know how the number of required trials to get a certain  
> number of successes.
>
> By example:
>
> How many trials do I need to have 98% probability of 50 successes,  
> when the a priory probability is 0.1 per trial.
>
> The Negative binomial function may do the job (not sure):
>
> NegBinomial {stats}
> The Negative Binomial Distribution
> Description
> Density, distribution function, quantile function and random  
> generation for the negative binomial distribution with parameters  
> size and prob.
> Usage
> dnbinom(x, size, prob, mu, log = FALSE)
> pnbinom(q, size, prob, mu, lower.tail = TRUE, log.p = FALSE)
> qnbinom(p, size, prob, mu, lower.tail = TRUE, log.p = FALSE)
> rnbinom(n, size, prob, mu)
>
> I tried finding out how to do this by using examples, but I am at a  
> loss. Any help would be much appreciated!

As far as I can see (which is often not very far) the negative  
binomial distribution has nothing
to do with it.

You want Pr(X >= 50) = 0.98 where X is binomially distributed with n  
= ?, p = 0.1.

Equivalently Pr(X <= 49) = 0.02.

After some trial-and-error I found:

 > pbinom(49,645:655,0.1,lower=FALSE)
  [1] 0.9786144 0.9792460 0.9798610 0.9804599 0.9810430 0.9816106  
0.9821632
  [8] 0.9827009 0.9832242 0.9837334 0.9842288

I.e. the *smallest* n that makes Pr(X>=50) >= 0.98 is n = 648.

Note that you have to be careful with the ``at leasts'' here; it's easy
to make parity errors in respect of looking at upper and lower tails  
when
dealing the cumulative distribution of a discrete random variable.

	cheers,

		Rolf Turner



######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}



More information about the R-help mailing list