Learn R Programming

joker (version 0.14.2)

Nbinom: Negative Binomial Distribution

Description

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 Nbinom,numeric d(distr, x, log = FALSE)

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

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

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

# S4 method for Nbinom mean(x)

# S4 method for Nbinom median(x)

# S4 method for Nbinom mode(x)

# S4 method for Nbinom var(x)

# S4 method for Nbinom sd(x)

# S4 method for Nbinom skew(x)

# S4 method for Nbinom kurt(x)

# S4 method for Nbinom entro(x)

# S4 method for Nbinom finf(x)

llnbinom(x, size, prob)

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

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

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

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

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

# S4 method for Nbinom avar_mle(distr)

# S4 method for Nbinom avar_me(distr)

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.

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?

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

Run this code
# -----------------------------------------------------
# Negative Binomial Distribution Example
# -----------------------------------------------------

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

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

d(D, 0:4) # density function
p(D, 0:4) # distribution function
qn(D, c(0.4, 0.8)) # inverse distribution function
x <- r(D, 100) # random generator function

# alternative way to use the function
df <- d(D) ; df(x) # df is a function itself

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

mean(D) # Expectation
median(D) # Median
mode(D) # Mode
var(D) # Variance
sd(D) # Standard Deviation
skew(D) # Skewness
kurt(D) # Excess Kurtosis
entro(D) # Entropy
finf(D) # Fisher Information Matrix

# List of all available moments
mom <- moments(D)
mom$mean # expectation

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

ll(D, x)
llnbinom(x, N, p)

enbinom(x, N, type = "mle")
enbinom(x, N, type = "me")

mle(D, x)
me(D, x)
e(D, x, type = "mle")

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

vnbinom(N, p, type = "mle")
vnbinom(N, p, type = "me")

avar_mle(D)
avar_me(D)

v(D, type = "mle")

Run the code above in your browser using DataLab