Failing to simulate data for a negative binomial probability distribution

Viewed 89

I am hoping someone can help me.

In a beginners workshop I attended, in the process of fitting a multiple regression model, the instructor initially established a prior predictive check using a Poisson distribution for the outcome. This was done in two steps. Initially, a function was created:

    multiple_regression_poisson_dgp <- function(predictor1, 
          predictor2, alpha_mean, alpha_sd, beta_predictor1_mean, 
          beta_predictor1_sd,  beta_predictor2_mean, 
          beta_predictor2_sd) {
    N <- length(predictor1)
    alpha <- rnorm(1, mean = alpha_mean, sd = alpha_sd);
    beta_predictor1 <- rnorm(1, mean = beta_predictor1_mean, 
            sd = beta_predictor1_sd);
    beta_predictor2 <- rnorm(1, mean = beta_predictor2_mean, 
            sd = beta_super_sd);
    outcome <- rpois(N, lambda = alpha + beta_predictor1 * 
                predictor1 + beta_predictor2 * predictor2)
    return(outcome)
    }

After this function was created, the following priors were generated:

    multiple_regression_poisson_dgp(dataset$predictor1,
                                dataset$predictor2,
                                alpha_mean = 1,
                                alpha_sd = 0.5,
                                beta_predictor1_mean = -0.25,
                                beta_predictor1_sd = 0.5,
                                beta_predictor2_mean = 0,
                                beta_predictor2_sd = 1)

This worked fine. The issue is that, further down the line, it was shown that the Poisson distribution was not the most adequate. The negative binomial was suggested as the next step. Unfortunately, when I try to replicate the process for the negative binomial, I am unsuccessful. I have tried to replicate both of the steps shown above, but for the negative binomial. The first step was coded as:

    multiple_regression_negative_binomial_dgp <- 
       function(predictor1, predictor2, alpha_mean, alpha_sd, 
       beta_predictor1_mean, beta_predictor1_sd, 
       beta_predictor2_mean, beta_predictor2_sd, phi_mean, 
       phi_sd) {
    N <- length(predictor1)
    alpha <- rnorm(1, mean = alpha_mean, sd = alpha_sd);
    beta_predictor1 <- rnorm(1, mean = beta_predictor1_mean, 
        sd = beta_predictor1_sd); 
    beta_predictor2 <- rnorm(1, mean = beta_predictor2_mean, 
        sd = beta_super_sd);
    phi <- rnorm(1, mean = phi_mean, sd = phi_sd);
    outcome<- rnbinom(N, size = mu + mu^2/phi, mu = alpha + 
          beta_predictor1 * predictor1 + beta_predictor2 * 
               predictor2)
    return(outcome)
    }

Because there is a phi in the negative binomial, and given that it will be a parameter whose prior I will be calculating, I assumed it needed to be added to the equation. Additionally, given the documentation for rnbinom(), I thought i could treat mu as I treated lambda in the Poisson generation, feeding the regression equation onto it.

The function is likely inadequate, but after I create it and move onto the second step, the errors emerge. The second step I coded as:

    multiple_regression_negative_binomial_dgp(dataset$predictor1,
                                dataset$predictor2,
                                alpha_mean = 1,
                                alpha_sd = 0.5,
                                beta_predictor1_mean = -0.25,
                                beta_predictor1_sd = 0.5,
                                beta_predictor2_mean = 0,
                                beta_predictor2_sd = 1,
                                phi_mean = 0,
                                phi_sd = 1)

However, as soon as I try to run this data generating process, I get the warning stating:

    Error in rnbinom(N, size = mu, mu = alpha + beta_predictor1 * predictor1 + beta_predictor2 * predictor2 : object 'mu' not found

Any help would be much appreciated, I realize that I am applying a more mechanistic mindset in trying to replicate the Poisson data generating process for the negative binomial one, but I have been unable to find any clues as to how to solve this. Most examples I came across define a value for mu and for size, instead of 'feeding' it the formula.

1 Answers

In your multiple_regression_negative_binomial_dgp function, you call rnbinom. That function needs a size argument, and you assign mu + mu^2/phi to it, but mu is not defined within the function, nor passed to it. The fact that rnbinom contains a mu argument, which you do provide (alpha + beta_predictor1 * predictor1 + beta_predictor2 * predictor2), doesn't take care of it, because rnbinom doesn't pass that information over to size. I would suggest you try:

multiple_regression_negative_binomial_dgp <- function(predictor1, predictor2,  
                                alpha_mean, alpha_sd, beta_predictor1_mean, 
                                beta_predictor1_sd, beta_predictor2_mean, 
                                beta_predictor2_sd, phi_mean,  phi_sd) {
  N               <- length(predictor1)
  alpha           <- rnorm(1, mean=alpha_mean,           sd=alpha_sd)
  beta_predictor1 <- rnorm(1, mean=beta_predictor1_mean, sd=beta_predictor1_sd) 
  beta_predictor2 <- rnorm(1, mean=beta_predictor2_mean, sd=beta_super_sd);
  phi             <- rnorm(1, mean=phi_mean,             sd=phi_sd)
  Mu              <- alpha + beta_predictor1*predictor1 + beta_predictor2*predictor2
  outcome         <- rnbinom(N, size=Mu + Mu^2/phi, mu=Mu)
  return(outcome)
}
Related