Compute Randomly Drawn Negative Binomial Density in R Programming

The Negative Binomial Density

The negative binomial density is a probability distribution that models the number of successes in a sequence of independent and identically distributed Bernoulli trials before a specified number of failures (or non-successes) occurs. The negative binomial distribution is a generalization of the geometric distribution, which models the number of trials needed to obtain the first success.

The negative binomial density function is defined by two parameters: the mean (or expected value) of the distribution, denoted by mu, and the dispersion parameter, denoted by size. The probability mass function of the negative binomial distribution is given by:

P(X = k) = (k + size – 1 choose k) * (1 – p)^size * p^k

where X is the random variable representing the number of trials needed to obtain k successes, p is the probability of success on each trial, and choose denotes the binomial coefficient.

The negative binomial density can be used to model a variety of phenomena, such as the number of defective items in a batch, the number of calls to a customer service center before a problem is resolved, or the number of accidents before a safety violation occurs.

Compute Randomly Drawn Negative Binomial Density in R Programming – rnbinom() Function

To compute a randomly drawn negative binomial density in R programming, you can use the rnbinom() function which generates random numbers from a negative binomial distribution with given parameters.

Here’s an example code that generates 1000 random numbers from a negative binomial distribution with mean mu = 5 and dispersion parameter size = 2, and then plots the density function:

 

# set the mean and dispersion parameter
mu <- 5
size <- 2

# generate 1000 random numbers from a
# negative binomial distribution
x <- rnbinom(1000, size = size, mu = mu)

# plot the density function
hist(x, freq = FALSE, col = "gray", 
main = "Negative Binomial Density")

curve(
dnbinom(x, size = size, mu = mu),
add = TRUE,
lwd = 2,
col = "red"
)

In this code, the rnbinom() function generates 1000 random numbers from a negative binomial distribution with mean mu = 5 and dispersion parameter size = 2. The hist() function is used to plot a histogram of the generated data, while the curve() function plots the density function of the negative binomial distribution with the same parameters as used in generating the data. The resulting plot shows the density function in red and the histogram in gray.

Binomial Distribution in R Programming

Poisson Functions in R Programming