The Negative Binomial Distribution
Density, distribution function, quantile function and random
generation for the negative binomial distribution with parameters
1 2 3 4
vector of (non-negative integer) quantiles.
vector of quantiles.
vector of probabilities.
number of observations. If
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.
probability of success in each trial.
alternative parametrization via mean: see ‘Details’.
logical; if TRUE, probabilities p are given as log(p).
logical; if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].
The negative binomial distribution with
size = n and
prob = p has density
Γ(x+n)/(Γ(n) x!) p^n (1-p)^x
for x = 0, 1, 2, …, n > 0 and 0 < p ≤ 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 μ = 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
pgamma) with scale parameter
(1 - prob)/prob
and shape parameter
size. (This definition allows non-integer
An alternative parametrization (often used in ecology) is by the
mu (see above), and
size, the dispersion
size/(size+mu). The variance
mu + mu^2/size in this parametrization.
If an element of
x is not integer, the result of
is zero, with a warning.
size == 0 is the distribution concentrated at zero.
This is the limiting distribution for
size approaching zero,
mu rather than
prob is held constant. Notice
though, that the mean of the limit distribution is 0, whatever the
The quantile is defined as the smallest value x such that F(x) ≥ p, where F is the distribution function.
dnbinom gives the density,
pnbinom gives the distribution function,
qnbinom gives the quantile function, and
rnbinom generates random deviates.
prob will result in return value
NaN, with a warning.
The length of the result is determined by
rnbinom, 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.
dnbinom computes via binomial probabilities, using code
contributed by Catherine Loader (see
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 Poissons, see
Devroye, L. (1986) Non-Uniform Random Variate Generation. Springer-Verlag, New York. Page 480.
Distributions for standard distributions, including
dbinom for the binomial,
dpois for the
dgeom for the geometric distribution, which
is a special case of the negative binomial.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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)]))
Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.