R: The Binomial Distribution (original) (raw)

Binomial {stats} R Documentation

Description

Density, distribution function, quantile function and random generation for the binomial distribution with parameters sizeand prob.

This is conventionally interpreted as the number of ‘successes’ in size trials.

Usage

dbinom(x, size, prob, log = FALSE)
pbinom(q, size, prob, lower.tail = TRUE, log.p = FALSE)
qbinom(p, size, prob, lower.tail = TRUE, log.p = FALSE)
rbinom(n, size, prob)

Arguments

x, q vector of quantiles, the number of successes.
p vector of probabilities.
n number of observations. If length(n) > 1, the length is taken to be the number required.
size number of trials (zero or more).
prob probability of success on each trial.
log, log.p logical; if TRUE, probabilities are given as logarithms.
lower.tail logical; if TRUE (default), probabilities areP[X \le x], otherwise, P[X > x].

Details

The binomial distribution with size = n andprob = p has density

p(x) = {n \choose x} {p}^{x} {(1-p)}^{n-x}

for x = 0, \ldots, n. Note that binomial coefficients can be computed by[choose](../../base/html/Special.html) in R.

If an element of x is not integer, the result of dbinomis zero, with a warning.

p(x) is computed using Loader's algorithm, see the reference below.

The quantile is defined as the smallest value x such thatF(x) \ge p, where F is the distribution function.

Value

dbinom gives the density,pbinom is the cumulative distribution function, andqbinom is the quantile function of the binomial distribution.rbinom generates random deviates.

If size is not an integer, NaN is returned.

The length of the result is determined by n forrbinom, and is the maximum of the lengths of the numerical arguments for the other functions.

The numerical arguments other than n are recycled to the length of the result. Only the first elements of the logical arguments are used.

Source

For dbinom a saddle-point expansion is used: see ⁠Loader (2000).

pbinom uses [pbeta](../../stats/help/pbeta.html).

qbinom uses the Cornish–Fisher Expansion to include a skewness correction to a normal approximation, followed by a search.

rbinom (for size < .Machine$integer.max) is based on ⁠Kachitvichyanukul and Schmeiser (1988). For larger values it uses inversion.

References

⁠Kachitvichyanukul V, Schmeiser BW (1988). “Binomial Random Variate Generation.”Communications of the ACM, 31(2), 216–222.doi:10.1145/42372.42381.

⁠Loader C (2000). “Fast and Accurate Computation of Binomial Probabilities.”https://www.r-project.org/doc/reports/CLoader-dbinom-2002.pdf.

See Also

Distributions for other standard distributions, including[dnbinom](../../stats/help/dnbinom.html) for the negative binomial, and[dpois](../../stats/help/dpois.html) for the Poisson distribution.

Examples

require(graphics)
# Compute P(45 < X < 55) for X Binomial(100,0.5)
sum(dbinom(46:54, 100, 0.5))

## Using "log = TRUE" for an extended range :
n <- 2000
k <- seq(0, n, by = 20)
plot (k, dbinom(k, n, pi/10, log = TRUE), type = "l", ylab = "log density",
      main = "dbinom(*, log=TRUE) is better than  log(dbinom(*))")
lines(k, log(dbinom(k, n, pi/10)), col = "red", lwd = 2)
## extreme points are omitted since dbinom gives 0.
mtext("dbinom(k, log=TRUE)", adj = 0)
mtext("extended range", adj = 0, line = -1, font = 4)
mtext("log(dbinom(k))", col = "red", adj = 1)

[Package _stats_ version 4.6.0 Index]