qbinomR {DPQ}R Documentation

Pure R Implementation of R's qbinom() with Tuning Parameters

Description

A pure R implementation, including many tuning parameter arguments, of R's own Rmathlib C code algorithm, but with more flexibility.

It is using Vectorize(qbinomR1, *) where the hidden qbinomR1 works for numbers (aka ‘scalar’, length one) arguments only, the same as the C code.

Usage

qbinomR(p, size, prob, lower.tail = TRUE, log.p = FALSE,
        yLarge = 4096, # was hard wired to 1e5
        incF = 1/64,   # was hard wired to .001
        iShrink = 8,   # was hard wired to 100
        relTol = 1e-15,# was hard wired to 1e-15
        pfEps.n = 8,   # was hard wired to 64: "fuzz to ensure left continuity"
        pfEps.L = 2,   # was hard wired to 64:   "   "   ..
        fpf = 4, # *MUST* be >= 1 (did not exist previously)
        trace = 0)

Arguments

p, size, prob, lower.tail, log.p

qbinom() standard argument, see its help page.

yLarge

when y >= y_L, y_L = yLarge, the binary root finding search is made “cleverer”, taking larger increments, determined by incF and iShrink:

incF

a positive “increment factor” (originally hardwired to 0.001), used only when y >= yLarge; defines the initial increment in the search algorithm as incr <- floor(incF * y).

iShrink

a positive increment shrinking factor, used only when y >= yLarge to define the new increment from the old one as incr <- max(1, floor(incr/iShrink)) where the LHS was hardired original to (incr/100).

relTol

relative tolerance, > 0; the search terminates when the (integer!) increment is less than relTol * y or the previous increment was not larger than 1.

pfEps.n

fuzz factor to ensure left continuity in the normal case log.p=FALSE; used to be hardwired to 64 (in R up to 2021-05-08).

pfEps.L

fuzz factor to ensure left continuity in case log.p=TRUE; used to be hardwired to 64 (in R up to 2021-05-08).

fpf

factor f \ge 1 for the normal upper tail case (log.p=FALSE, lower.tail=FALSE): p is only “fuzz-corrected”, i.e., multiplied by 1+e when 1 - p > fpf*e for e <- pfEps.n * c_e and c_e = 2^{-52}, the .Machine$double_epsilon.

trace

logical (or integer) specifying if (and how much) output should be produced from the algorithm.

Details

as mentioned on qbinom help page, qbinom uses the Cornish–Fisher Expansion to include a skewness correction to a normal approximation, thus defining y := Fn(p, size, prob, ..).

The following (root finding) binary search is tweaked by the yLarge, ..., fpf arguments.

Value

a numeric vector like p recycled to the common lengths of p, size, and prob.

Author(s)

Martin Maechler

See Also

qbinom, qpois.

Examples

set.seed(12)
pr <- (0:16)/16 # supposedly recycled
x10 <- rbinom(500, prob=pr, size =  10); p10 <- pbinom(x10, prob=pr, size= 10)
x1c <- rbinom(500, prob=pr, size = 100); p1c <- pbinom(x1c, prob=pr, size=100)
## stopifnot(exprs = {
table( x10  == (qp10  <- qbinom (p10, prob=pr, size= 10) ))
table( qp10 == (qp10R <- qbinomR(p10, prob=pr, size= 10) )); summary(warnings()) # 30 x NaN
table( x1c  == (qp1c  <- qbinom (p1c, prob=pr, size=100) ))
table( qp1c == (qp1cR <- qbinomR(p1c, prob=pr, size=100) )); summary(warnings()) # 30 x NaN
## })

[Package DPQ version 0.5-8 Index]