[Rd] qbinom returns NaN

From: Spencer Graves <spencer.graves_at_pdf.com>
Date: Wed 23 Nov 2005 - 22:01:29 GMT


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@pdf.com
www.pdf.com <http://www.pdf.com>
Tel:  408-938-4420
Fax: 408-280-7915

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Thu Nov 24 09:13:32 2005

This archive was generated by hypermail 2.1.8 : Mon 20 Feb 2006 - 03:21:34 GMT