Skip to contents

The Negative Binomial distribution is a discrete probability distribution that models the number of failures before a specified number of successes occurs in a sequence of independent Bernoulli trials. It is defined by parameters \(r > 0\) (number of successes) and \(0 < p \leq 1\) (probability of success).

Usage

Nbinom(size = 1, prob = 0.5)

# S4 method for class 'Nbinom,numeric'
d(distr, x, log = FALSE)

# S4 method for class 'Nbinom,numeric'
p(distr, q, lower.tail = TRUE, log.p = FALSE)

# S4 method for class 'Nbinom,numeric'
qn(distr, p, lower.tail = TRUE, log.p = FALSE)

# S4 method for class 'Nbinom,numeric'
r(distr, n)

# S4 method for class 'Nbinom'
mean(x)

# S4 method for class 'Nbinom'
median(x)

# S4 method for class 'Nbinom'
mode(x)

# S4 method for class 'Nbinom'
var(x)

# S4 method for class 'Nbinom'
sd(x)

# S4 method for class 'Nbinom'
skew(x)

# S4 method for class 'Nbinom'
kurt(x)

# S4 method for class 'Nbinom'
entro(x)

# S4 method for class 'Nbinom'
finf(x)

llnbinom(x, size, prob)

# S4 method for class 'Nbinom,numeric'
ll(distr, x)

enbinom(x, size, type = "mle", ...)

# S4 method for class 'Nbinom,numeric'
mle(distr, x, na.rm = FALSE)

# S4 method for class 'Nbinom,numeric'
me(distr, x, na.rm = FALSE)

vnbinom(size, prob, type = "mle")

# S4 method for class 'Nbinom'
avar_mle(distr)

# S4 method for class 'Nbinom'
avar_me(distr)

Arguments

size

number of trials (zero or more).

prob

numeric. Probability of success on each trial.

distr

an object of class Nbinom.

x

For the density function, x is a numeric vector of quantiles. For the moments functions, x is an object of class Nbinom. For the log-likelihood and the estimation functions, x is the sample of observations.

log, log.p

logical. Should the logarithm of the probability be returned?

q

numeric. Vector of quantiles.

lower.tail

logical. If TRUE (default), probabilities are \(P(X \leq x)\), otherwise \(P(X > x)\).

p

numeric. Vector of probabilities.

n

number of observations. If length(n) > 1, the length is taken to be the number required.

type

character, case ignored. The estimator type (mle or me).

...

extra arguments.

na.rm

logical. Should the NA values be removed?

Value

Each type of function returns a different type of object:

  • Distribution Functions: When supplied with one argument (distr), the d(), p(), q(), r(), ll() functions return the density, cumulative probability, quantile, random sample generator, and log-likelihood functions, respectively. When supplied with both arguments (distr and x), they evaluate the aforementioned functions directly.

  • Moments: Returns a numeric, either vector or matrix depending on the moment and the distribution. The moments() function returns a list with all the available methods.

  • Estimation: Returns a list, the estimators of the unknown parameters. Note that in distribution families like the binomial, multinomial, and negative binomial, the size is not returned, since it is considered known.

  • Variance: Returns a named matrix. The asymptotic covariance matrix of the estimator.

Details

The probability mass function (PMF) of the negative binomial distribution is: $$ P(X = k) = \binom{k + r - 1}{k} (1 - p)^k p^r, \quad k \in \mathbb{N}_0.$$

See also

Functions from the stats package: dnbinom(), pnbinom(), qnbinom(), rnbinom()

Examples

# -----------------------------------------------------
# Negative Binomial Distribution Example
# -----------------------------------------------------

# Create the distribution
N <- 10 ; p <- 0.4
D <- Nbinom(N, p)

# ------------------
# dpqr Functions
# ------------------

d(D, 0:4) # density function
#> [1] 0.0001048576 0.0006291456 0.0020761805 0.0049828332 0.0097165246
p(D, 0:4) # distribution function
#> [1] 0.0001048576 0.0007340032 0.0028101837 0.0077930168 0.0175095415
qn(D, c(0.4, 0.8)) # inverse distribution function
#> [1] 13 20
x <- r(D, 100) # random generator function

# alternative way to use the function
df <- d(D) ; df(x) # df is a function itself
#>   [1] 0.0638953912 0.0644631755 0.0499115616 0.0329101076 0.0244856421
#>   [6] 0.0513778730 0.0681223171 0.0513778730 0.0604342270 0.0156364106
#>  [11] 0.0671491411 0.0554572907 0.0585707753 0.0585707753 0.0038031980
#>  [16] 0.0681223171 0.0681223171 0.0244856421 0.0681223171 0.0638953912
#>  [21] 0.0513778730 0.0499115616 0.0428148942 0.0191622678 0.0232269913
#>  [26] 0.0638953912 0.0670901608 0.0499115616 0.0604342270 0.0681223171
#>  [31] 0.0001048576 0.0638953912 0.0604342270 0.0428148942 0.0163237614
#>  [36] 0.0499115616 0.0604342270 0.0441323282 0.0513778730 0.0156364106
#>  [41] 0.0163237614 0.0062980310 0.0644631755 0.0513778730 0.0428148942
#>  [46] 0.0101035268 0.0191622678 0.0499115616 0.0681223171 0.0329101076
#>  [51] 0.0585707753 0.0638953912 0.0038031980 0.0029237085 0.0383951255
#>  [56] 0.0191622678 0.0499115616 0.0329101076 0.0329101076 0.0670901608
#>  [61] 0.0554572907 0.0670901608 0.0244856421 0.0383951255 0.0670901608
#>  [66] 0.0681223171 0.0644631755 0.0644631755 0.0671491411 0.0670901608
#>  [71] 0.0670901608 0.0585707753 0.0670901608 0.0681223171 0.0638953912
#>  [76] 0.0585707753 0.0644631755 0.0329101076 0.0671491411 0.0671491411
#>  [81] 0.0428148942 0.0604342270 0.0244856421 0.0585707753 0.0163237614
#>  [86] 0.0604342270 0.0671491411 0.0604342270 0.0585707753 0.0513778730
#>  [91] 0.0499115616 0.0062980310 0.0022326501 0.0671491411 0.0644631755
#>  [96] 0.0441323282 0.0671491411 0.0554572907 0.0513778730 0.0513778730

# ------------------
# Moments
# ------------------

mean(D) # Expectation
#> [1] 15
median(D) # Median
#> [1] 14
mode(D) # Mode
#> [1] 13
var(D) # Variance
#> [1] 37.5
sd(D) # Standard Deviation
#> [1] 6.123724
skew(D) # Skewness
#> [1] 0.6531973
kurt(D) # Excess Kurtosis
#> [1] 0.6266667
entro(D) # Entropy
#> [1] 1.048722
finf(D) # Fisher Information Matrix
#>     prob 
#> 104.1667 

# List of all available moments
mom <- moments(D)
mom$mean # expectation
#> [1] 15

# ------------------
# Point Estimation
# ------------------

ll(D, x)
#> [1] -325.7134
llnbinom(x, N, p)
#> [1] -325.7134

enbinom(x, N, type = "mle")
#> $prob
#> [1] 0.4025765
#> 
enbinom(x, N, type = "me")
#> $prob
#> [1] 0.4025765
#> 

mle(D, x)
#> $prob
#> [1] 0.4025765
#> 
me(D, x)
#> $prob
#> [1] 0.4025765
#> 
e(D, x, type = "mle")
#> $prob
#> [1] 0.4025765
#> 

# ------------------
# Estimator Variance
# ------------------

vnbinom(N, p, type = "mle")
#>   prob 
#> 0.0096 
vnbinom(N, p, type = "me")
#>   prob 
#> 0.0096 

avar_mle(D)
#>   prob 
#> 0.0096 
avar_me(D)
#>   prob 
#> 0.0096 

v(D, type = "mle")
#>   prob 
#> 0.0096