# Simulate data based on negative binomial regression coefficient

I'm trying to simulate a dataframe with columns x and y based on a real-world dataset. Fitting a negative binomial regression model onto the real world dataset produced a coefficient of 0.1922 with a standard error of 0.0268.

Now I want to create an artificial dataframe based on this finding to run some follow up analyses. So far I've done the following to generate hypothetical y values:

y <- rlnorm(1000000) # generate 1,000,000 numbers from log normal distribution
y <- y*10 # multiply by ten to get more realistic numbers
y <- round(y, digits = 0) # round to whole number
hist(y) # take a look


How can I use the results of the negative binomial regression to map my simulated y values onto simulated x values?

FYI: If you need it, you can find a good overview of the negative binomial regression here

• Not sure, but MASS has an application for simulating negative binomial data: stat.ethz.ch/R-manual/R-devel/library/MASS/html/rnegbin.html Does this meet your needs? Commented Mar 5, 2019 at 12:59
• Thanks, but the rnegbin function only generates a single vector of data points from a negative binomial distribution. I need to generate a dataframe of two variables with a relationship similar to that which I found in a real-world dataset I analysed. Commented Mar 6, 2019 at 15:07
• This is really not yet a programming question but rather displays a lack of statistical knowledge and should have been posted on CrossValidated.com
– DWin
Commented Mar 7, 2019 at 18:53
• @Jason is your question, what information do I need to simulate data according to a negative binomial regression model where $E[Y|X] \propto \alpha + \beta X$? and $Y$ has a negative binomial distribution? Commented Mar 7, 2019 at 23:47
• @AdamO Yes, that's correct. Commented Mar 13, 2019 at 16:14

If a response $$Y_i \in \left\{0, 1, \dotsc \right\}$$ given a vector of covariates $$X_i \in \mathbb{R}^p$$ follows a negative binomial regression model (with a $$\log$$ link) and coefficients $$\beta$$ and an (inverse) dispersion parameter $$\theta$$ then $$E(Y_i|X_i) = \exp( X_i \beta) = \mu_i$$, $$\text{Var} (Y_i | X_i) = \mu_i + \mu_i^2/ \theta$$ and

$$P(Y_i = y | X_i) = \binom{y+\theta - 1}{y} \left(\frac{\mu_i}{\mu_i + \theta} \right)^y \left(\frac{\theta}{\mu_i + \theta} \right)^{\theta}$$

In R, to simulate a vector of responses, given a design matrix X you could write

eta = X %*% beta
y = rnbinom(length(eta), mean = exp(eta), size = theta)

• I see "eta" in the R code but no Greek lettereta in the explanation. It should be possible to "back calculate" the parameters from mean and variance.
– DWin
Commented Mar 8, 2019 at 3:01
• Eta commonly used for the "linear predictor" in a regression. It's equal to $\log \mu$ in this case. In the question, it was not explained how the regression was fit, but one shouldn't need to "back calculate" anything -- all that is needed is the coefficient and the dispersion parameter estimates. Commented Mar 8, 2019 at 3:34
• @Andrew M Thanks, this is helpful. Could you please explain what you mean by "design matrix X" ? At present, I only have a vector/column of simulated data that I want to apply a negative binomial regression equation to, ultimately creating a bivariate negative binomial distribution (i.e., where the relationship between x and y takes the shape of a negative binomial distribution). Commented Mar 13, 2019 at 16:18
• In that case I don't understand what you are trying to ask. I don't know what a "bivariate negative binomial distribution" is, nor do I think it has a common definition. I don't understand what it means to say "that relationship between x and y takes the shape of a negative binomial distribution" . There are many senses in which a distribution could have a shape. Commented Mar 16, 2019 at 21:11
• @Andrew M Sorry for the confusion. Can you please explain where design matrix X comes from? That is where I get lost. I have read into it here (mathematica-journal.com/2013/06/negative-binomial-regression) but I am confused as to how to generate it in R. Commented Mar 20, 2019 at 12:51