[R] inverse binomial in R

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Tue Jun 19 18:06:23 CEST 2012


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of anna freni sterrantino
> Sent: Tuesday, June 19, 2012 2:40 AM
> To: Duncan Murdoch
> Cc: Rcran help
> Subject: Re: [R] inverse binomial in R
> 
> 
> 
> Hi Duncan and Rlist,
> I've notice a different behaviour in the invbinomial
> you suggest me and invbinomial in stata.
> 
> invbinomial(50,50, 0.4)
> Error in uniroot(function(x) pbinom(k, n, x) - p, c(0, 1)) :
>   f() values at end points not of opposite sign
> > invbinomial(50,50, 0.6)
> Error in uniroot(function(x) pbinom(k, n, x) - p, c(0, 1)) :
>   f() values at end points not of opposite sign
> 
> while stata
> gen p3=invbinomial(50,50, 0.4)
> 
> . display p3
> 0
> 
> 
> 
> 
> . gen p4=invbinomial(50,50, 0.6)
> 
> . display p4
> 0
> 
> Thanks
> Cheers
> 
> Anna
> 
> 
> ________________________________
>  Da: Duncan Murdoch <murdoch.duncan at gmail.com>
> 
> Cc: Rcran help <r-help at r-project.org>
> Inviato: Giovedì 31 Maggio 2012 15:32
> Oggetto: Re: [R] inverse binomial  in R
> 
> 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))
> }
> 	[[alternative HTML version deleted]]

Anna,

Acccording to the excerpt of Stata documentation that you quote, k must be strictly less than n.  So, 
Stata apparently does not return an error (or even a warning) when you supply an invalid input, but the R function does.  How do the functions compare when you supply valid inputs?  What do you want the R function to do when k equals n?


Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204




More information about the R-help mailing list