[R] inverse binomial in R

Duncan Murdoch murdoch.duncan at gmail.com
Thu May 31 15:32:38 CEST 2012


On 12-05-31 9:10 AM, anna freni sterrantino wrote:
> Hello!
> I'm having some trouble
> trying to replicate in R a Stata function
>
>   invbinomial(n,k,p)
>         Domain n:     1 to 1e+17
>         Domain k:     0 to n - 1
>         Domain p:     0 to 1 (exclusive)
>         Range:        0 to 1
>         Description:  returns the inverse of the cumulative binomial; i.e., it
>                           returns the probability of success on one trial such
>                           that the probability of observing floor(k) or fewer
>                           successes in floor(n) trials is p.
>
> I've found some hints on the web like
> http://rwiki.sciviews.org/doku.php?id=guides:tutorials:regression:table
>
> I tried to replicate using qbinom
> the results obtained in
>
>> invbinomial(10,5, 0.5)
>> .54830584
>
> but with no success.

I don't think base R has a function like that, though some contributed 
package probably does.  If you're writing it yourself you'd need to use 
uniroot or some other solver, e.g

invbinomial <- function(n, k, p) {
   uniroot(function(x) pbinom(5, 10, x) - p, c(0, 1))
}



More information about the R-help mailing list