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:

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

 

negative binomial distribution plot in r

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

 

negative binomial cumulative distribution plot in r

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

 

negative binomial quantile function plot in r

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 = "")

 

random numbers based on negative binomial distribution plot in r

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:

 

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.

 

4 Comments. Leave new

  • Steve Jewson
    July 14, 2024 11:38 am

    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.

    Reply
  • Steve Jewson
    July 19, 2024 2:37 pm

    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

    Reply
    • 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

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

The maximum upload file size: 2 MB. You can upload: image. Drop file here

Top