# Negative Binomial Distribution in R (4 Examples) | dnbinom, pnbinom, qnbinom & rnbinom Functions

This article illustrates how to **use the negative binomial functions** in the R programming language.

The content of the article looks as follows:

- Example 1: Negative Binomial Density in R (dnbinom Function)
- Example 2: Negative Binomial Cumulative Distribution Function (pnbinom Function)
- Example 3: Negative Binomial Quantile Function (qnbinom Function)
- Example 4: Simulation of Random Numbers (rnbinom Function)
- Video, Further Resources & Summary

Sound good? Here’s how to do it…

## Example 1: Negative Binomial Density in R (dnbinom Function)

Example 1 explains how to create an R graphic showing the negative binomial density. As a first step, we need to create a sequence with non-negative integers in R:

x_dnbinom <- seq(0, 100, by = 1) # Specify x-values for dnbinom function |

x_dnbinom <- seq(0, 100, by = 1) # Specify x-values for dnbinom function

Now, we can use the dnbinom R function to return the corresponding negative binomial values of each element of our input vector with non-negative integers. Note that we are using a size (i.e. number of trials) and a probability of 0.5 (i.e. 50%) in this example:

y_dnbinom <- dnbinom(x_dnbinom, size = 100, prob = 0.5) # Apply dnbinom function |

y_dnbinom <- dnbinom(x_dnbinom, size = 100, prob = 0.5) # Apply dnbinom function

Based on the plot function of the R programming language, we can create a graph showing our output:

plot(y_dnbinom) # Plot dnbinom values |

plot(y_dnbinom) # Plot dnbinom values

**Figure 1: Negative Binomial Density in R.**

## Example 2: Negative Binomial Cumulative Distribution Function (pnbinom Function)

In the second example, I’ll show you how to plot the cumulative distribution function of the negative binomial distribution based on the pnbinom command.

Again, we need to create a sequence on non-negative integers as input for the pnbinom function:

x_pnbinom <- seq(0, 100, by = 1) # Specify x-values for pnbinom function |

x_pnbinom <- seq(0, 100, by = 1) # Specify x-values for pnbinom function

The pnbinom function is now applied as follows…

y_pnbinom <- pnbinom(x_pnbinom, size = 100, prob = 0.5) # Apply pnbinom function |

y_pnbinom <- pnbinom(x_pnbinom, size = 100, prob = 0.5) # Apply pnbinom function

…and we can create a plot illustrating the output of pnbinom as follows:

plot(y_pnbinom) # Plot pnbinom values |

plot(y_pnbinom) # Plot pnbinom values

**Figure 2: Negative Binomial Cumulative Distribution Function.**

## Example 3: Negative Binomial Quantile Function (qnbinom Function)

Similar to the R syntax of Examples 1 and 2, we can create a plot containing the negative binomial quantile function. As input, we need to specify a vector of probabilities:

x_qnbinom <- seq(0, 1, by = 0.01) # Specify x-values for qnbinom function |

x_qnbinom <- seq(0, 1, by = 0.01) # Specify x-values for qnbinom function

We can now apply the qnbinom function to these probabilities as shown in the R code below:

y_qnbinom <- qnbinom(x_qnbinom, size = 100, prob = 0.5) # Apply qnbinom function |

y_qnbinom <- qnbinom(x_qnbinom, size = 100, prob = 0.5) # Apply qnbinom function

A plot of the output of qnbinom can be created as follows:

plot(y_qnbinom) # Plot qnbinom values |

plot(y_qnbinom) # Plot qnbinom values

**Figure 3: Negative Binomial Quantile Function.**

## Example 4: Simulation of Random Numbers (rnbinom Function)

In order to generate a set of random numbers that are following the negative binomial distribution, we need to specify a seed and a sample size first:

set.seed(53535) # Set seed for reproducibility N <- 10000 # Specify sample size |

set.seed(53535) # Set seed for reproducibility N <- 10000 # Specify sample size

We can now draw a set of random numbers of this sample size as follows:

y_rnbinom <- rnbinom(N, size = 100, prob = 0.5) # Draw N nbinomially distributed values y_rnbinom # Print values to RStudio console # 102 102 89 96 94 74 92 112 87 99 87 131 109... |

y_rnbinom <- rnbinom(N, size = 100, prob = 0.5) # Draw N nbinomially distributed values y_rnbinom # Print values to RStudio console # 102 102 89 96 94 74 92 112 87 99 87 131 109...

The following histogram illustrates the RStudio output of our previous R code:

hist(y_rnbinom, # Plot of randomly drawn nbinom density breaks = 100, main = "") |

hist(y_rnbinom, # Plot of randomly drawn nbinom density breaks = 100, main = "")

**Figure 4: Simulation of Random Numbers Based on Negative Binomial Distribution.**

## Video, Further Resources & Summary

Have a look at the following video of my YouTube channel. In the video, I explain the R code of this article:

*The YouTube video will be added soon.*

You may also have a look at the other articles on probability distributions and the simulation of random numbers in the R programming language:

- Bernoulli Distribution in R
- Beta Distribution in R
- Binomial Distribution in R
- Bivariate & Multivariate Distributions in R
- Cauchy Distribution in R
- Chi-Squred Distribution in R
- Exponential Distribution in R
- F Distribution in R
- Gamma Distribution in R
- Geometric Distribution in R
- Hypergeometric Distribution in R
- Log Normal Distribution in R
- Logistic Distribution in R
- Negative Binomial Distribution in R
- Normal Distribution in R
- Poisson Distribution in R
- Student t Distribution in R
- Studentized Range Distribution in R
- Uniform Distribution in R
- Weibull Distribution in R
- Wilcoxon Signedank Statistic Distribution in R
- Wilcoxonank Sum Statistic Distribution in R

Besides that, you could have a look at the other tutorials on my homepage. A selection of posts can be found here.

This article showed how to **create and simulate a negative binomial distribution** in the R programming language. Don’t hesitate to let me know in the comments section below, if you have additional questions.

**5**/

**5**(

**1**vote )

### Statistics Globe Newsletter