[Rd] qbinom returns NaN

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Nov 24 08:55:33 CET 2005


This is a very simple change at C level: the !R_FINITE(p) test is 
incorrect for log_p != 0.

Changed now.


On Wed, 23 Nov 2005, Spencer Graves wrote:

> Hi, All:
>
> 	  For most but not all cases, qbinom is the inverse of pbinom.
> Consider the following example, which generates an exception:
>
> > (pb01 <- pbinom(0:1, 1, .5, log=T, lower.tail=FALSE))
> [1] -0.6931472       -Inf
>
> 	  Since "lower.tail=FALSE", Pr{X>1} = 0 in this context, and log(0) =
> -Inf, consistent with the documentation.
>
> 	  However, the inverse of this does NOT recover 0:1:
>
> > qbinom(pb01,1, .5, log=T, lower.tail=F)
> [1]   0 NaN
>
> 	  Shouldn't the NaN here be 1?  If yes, this is relatively easy to fix.
>  Consider for example the following:
>
> qbinom. <-
> function (p, size, prob, lower.tail = TRUE, log.p = FALSE){
>   q. <- .Internal(qbinom(p, size, prob, lower.tail, log.p))
>   q.[p==(-Inf)] <- 1
>   q.
> }
> > qbinom.(pb01,1, .5, log=T, lower.tail=F)
> [1] 0 1
> Warning message:
> NaNs produced in: qbinom(p, size, prob, lower.tail, log.p)
>
> 	  It's also easy to eliminate the Warning.  Consider for example the
> following:
>
> qbinom. <-
> function (p, size, prob, lower.tail = TRUE, log.p = FALSE){
>   if(any(p.inf <- p==(-Inf))&&(!lower.tail)&&log.p){
>     n <- max(length(p), length(size), length(prob))
>     p <- rep(p, length=n)
>     size <- rep(size, length=n)
>     prob <- rep(prob, length=n)
>     q. <- size
>     q.[p>(-Inf)] <- .Internal(qbinom(p[!p.inf],
>             size[!p.inf], prob[!p.inf], lower.tail, log.p))
>     return(q.)
>   }
>   .Internal(qbinom(p, size, prob, lower.tail, log.p))
> }
>
> 	  I suspect that for the right person, it would likely be easy to fix
> this in the .Internal qbinom code.  However, that's beyond my current R
> skill level.
>
> 	  Thanks for all your efforts to make R what it is today.
> 	  Best Wishes,
> 	  spencer graves
>
> -- 
> Spencer Graves, PhD
> Senior Development Engineer
> PDF Solutions, Inc.
> 333 West San Carlos Street Suite 700
> San Jose, CA 95110, USA
>
> spencer.graves at pdf.com
> www.pdf.com <http://www.pdf.com>
> Tel:  408-938-4420
> Fax: 408-280-7915
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-devel mailing list