Statistics with R
- Statistics with R
- R Objects, Numbers, Attributes, Vectors, Coercion
- Matrices, Lists, Factors
- Data Frames in R
- Control Structures in R
- Functions in R
- Data Basics: Compute Summary Statistics in R
- Central Tendency and Spread in R Programming
- Data Basics: Plotting – Charts and Graphs
- Normal Distribution in R
- Skewness of statistical data
- Bernoulli Distribution in R
- Binomial Distribution in R Programming
- Compute Randomly Drawn Negative Binomial Density in R Programming
- Poisson Functions in R Programming
- How to Use the Multinomial Distribution in R
- Beta Distribution in R
- Chi-Square Distribution in R
- Exponential Distribution in R Programming
- Log Normal Distribution in R
- Continuous Uniform Distribution in R
- Understanding the t-distribution in R
- Gamma Distribution in R Programming
- How to Calculate Conditional Probability in R?
- How to Plot a Weibull Distribution in R
- Hypothesis Testing in R Programming
- T-Test in R Programming
- Type I Error in R
- Type II Error in R
- Confidence Intervals in R
- Covariance and Correlation in R
- Covariance Matrix in R
- Pearson Correlation in R
- Normal Probability Plot in R
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.