R: The Negative Binomial Distribution (original) (raw)

NegBinomial {stats} R Documentation

Description

Density, distribution function, quantile function and random generation for the negative binomial distribution with parameterssize and prob, or alternatively,size and mu.

Usage

dnbinom(x, size, prob, mu, log = FALSE)
pnbinom(q, size, prob, mu, lower.tail = TRUE, log.p = FALSE)
qnbinom(p, size, prob, mu, lower.tail = TRUE, log.p = FALSE)
rnbinom(n, size, prob, mu)

Arguments

x vector of (non-negative integer) quantiles.
q vector of quantiles.
p vector of probabilities.
n number of observations. If length(n) > 1, the length is taken to be the number required.
size target for number of successful trials, or dispersion parameter (the shape parameter of the gamma mixing distribution). Must be strictly positive, need not be integer.
prob probability of success in each trial. 0 < prob <= 1.
mu alternative parametrization via mean = \mu: see ‘Details’.
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 negative binomial distribution with size = n andprob = p has density

p(x) = \frac{\Gamma(x+n)}{\Gamma(n) x!} p^n (1-p)^x

for x = 0, 1, 2, \ldots, n > 0 and 0 < p \le 1.

This represents the number of failures which occur in a sequence of Bernoulli trials before a target number of successes is reached. The mean is \mu = n(1-p)/p and variance n(1-p)/p^2.

A negative binomial distribution can also arise as a mixture of Poisson distributions with mean distributed as a gamma distribution (see [pgamma](../../stats/help/pgamma.html)) with scale parameter (1 - prob)/proband shape parameter size. (This definition allows non-integer values of size.)

An alternative parametrization (often used in ecology) is by the_mean_ mu (= \mu, see above), and size, the dispersion parameter, where prob = size/(size+mu) = p = n/(n+\mu). The variance is mu + mu^2/size in this parametrization.

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

The case size == 0 is the distribution concentrated at zero. This is the limiting distribution for size approaching zero, even if mu rather than prob is held constant. Notice though, that the mean of the limit distribution is 0, whatever the value of mu.

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

Value

dnbinom gives the density,pnbinom is the cumulative distribution function, andqnbinom is the quantile function of the negative binomial distribution.rnbinom generates random deviates.

Invalid size or prob will result in return valueNaN, with a warning.

The length of the result is determined by n forrnbinom, 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.

rnbinom returns a vector of type integer unless generated values exceed the maximum representable integer when [double](../../base/html/double.html)values are returned.

Source

dnbinom computes via binomial probabilities, using code contributed by Catherine Loader (see [dbinom](../../stats/help/dbinom.html)).

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

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

rnbinom uses the derivation as a gamma mixture of Poisson distributions, see ⁠Devroye (1986, page 480).

References

⁠Devroye L (1986).Non-Uniform Random Variate Generation. Springer, New York.https://luc.devroye.org/handbooksimulation1.pdf.

See Also

Distributions for standard distributions, including[dbinom](../../stats/help/dbinom.html) for the binomial, [dpois](../../stats/help/dpois.html) for the Poisson and [dgeom](../../stats/help/dgeom.html) for the geometric distribution, which is a special case of the negative binomial.

Examples

require(graphics)
x <- 0:11
dnbinom(x, size = 1, prob = 1/2) * 2^(1 + x) # == 1
126 /  dnbinom(0:8, size  = 2, prob  = 1/2) #- theoretically integer

## Cumulative ('p') = Sum of discrete prob.s ('d');  Relative error :
summary(1 - cumsum(dnbinom(x, size = 2, prob = 1/2)) /
                  pnbinom(x, size  = 2, prob = 1/2))

x <- 0:15
size <- (1:20)/4
persp(x, size, dnb <- outer(x, size, function(x,s) dnbinom(x, s, prob = 0.4)),
      xlab = "x", ylab = "s", zlab = "density", theta = 150)
title(tit <- "negative binomial density(x,s, pr = 0.4)  vs.  x & s")

image  (x, size, log10(dnb), main = paste("log [", tit, "]"))
contour(x, size, log10(dnb), add = TRUE)

## Alternative parametrization
x1 <- rnbinom(500, mu = 4, size = 1)
x2 <- rnbinom(500, mu = 4, size = 10)
x3 <- rnbinom(500, mu = 4, size = 100)
h1 <- hist(x1, breaks = 20, plot = FALSE)
h2 <- hist(x2, breaks = h1$breaks, plot = FALSE)
h3 <- hist(x3, breaks = h1$breaks, plot = FALSE)
barplot(rbind(h1$counts, h2$counts, h3$counts),
        beside = TRUE, col = c("red","blue","cyan"),
        names.arg = round(h1$breaks[-length(h1$breaks)]))

[Package _stats_ version 4.6.0 Index]