[R] inverse binomial in R

Eik Vettorazzi E.Vettorazzi at uke.de
Tue Jun 19 16:02:10 CEST 2012


Hi Anna,
you are at the upper end of the support of the respective binomial
distribution when asking for invbinomial(n,n,p). The probability of
seeing n or less successes in n trials is 1, regardless of the
underlying success rate. It cannot be less, so asking for
invbinomial(50,50, 0.6) makes no sense at all - neither does the result
of stata.

You may insert some exception handling in the function by throwing a
warning or error, when this special case occurs - or even return 0 to
cope the erroneous stata result.

cheers

Am 19.06.2012 11:39, schrieb anna freni sterrantino:
> 
> 
> 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]]
> 
> 
> 
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


-- 
Eik Vettorazzi

Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790

--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Guido Sauter (Vertreter des Vorsitzenden), Dr. Alexander Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus 



More information about the R-help mailing list