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
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
Based on the plot function of the R programming language, we can create a graph showing our output:
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
The pnbinom function is now applied as follows…
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

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
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
A plot of the output of qnbinom can be created as follows:
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
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...
The following histogram illustrates the RStudio output of our previous R code:
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.
Subscribe to the Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
Statistics Globe Newsletter
Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.







4 Comments. Leave new
I need to link dbinom with the output from glm.nb. It’s a bit of a headache because the two routines use different notation.
Let me know if you’ve ever figured that out.
Hey Steve,
I’ve never done this myself. Could you provide some more context? How exactly should your result look like?
Regards,
Joachim
Thanks for replying.
What I meant was, link dnbinom with the output from glm.nb.
Here’s the situation.
I have 74 predictands (hurricane numbers per year since 1950), y.
I have 74 predictors (sea surface temperature indices since 1950), x.
I am regressing y onto x using glm.nb.
FYI, the negative binomial with exponential link is often considered a reasonable model for that regression (poisson regression is more commonly used, but negative binomial might be better…one could look at the AIC to check I guess).
Having trained the regression using glm.nb, I then want to feed in a new predictor value for x.
(which would be predicted sea surface temperature for 2025, or earlier sea surface temperatures as part of a cross-validation scheme).
I can do that using predict.
What I then want, in the end, is to calculate the densities of the predicted negative binomial. I could do the math and calculate that myself, I suppose. But I was hoping I could take the various outputs from predict, and feed them into dnbinom as the negative binomial parameters. It’s making that connection which is the headache. Do you see what I mean?
Thanks,
Steve
Hey Steve,
Thank you for the clarifications. I understand your question better now, and I think this is very interesting. I assume this should be possible, but I’m unfortunately not an expert on this topic.
I run a Facebook discussion group where people can ask questions about R programming and statistics. Could you post your question there? This way, others can contribute/read as well: https://www.facebook.com/groups/statisticsglobe
Regards,
Joachim