Solved – Calculating predictions and confidence intervals from a negative binomial distribution

confidence intervalnegative-binomial-distributionoffsetr

I'm trying to use R's glm.nb to calculate predictions and confidence intervals.
When I'm using linear models after training a model, e.g., using:

model <- lm(y ~ x)

I can get predictions and CIs using:

pred <- predict(model, new_x, se.fit=T, interval="prediction", level=0.95)
CI.upper <- pred$fit[2]
    CI.lower <- pred$fit[3]

Now I'm using:

nb_model <- glm.nb(z ~ x + offset(y))
nb_pred <- predict(nb_model, new_data=data.frame(x=X, y=Y), type="response", se.fit=T)

My questions are:

  1. How do I get the CI from nb_pred? e.g., the equivalent of pred$fit[2] and pred$fit[3]
  2. The predict function seems to ignore Y – I get the same value from predict with and without providing y=Y. I don't understand why the model seems to ignore the offset variable.

Best Answer

I'm not sure what your new data.frame looks like, but you need to make sure they have the same variable names as the variables in your model. So in your model, your variable names are y and x. Then
new = data.frame(X,Y)
colnames(new) = c("x","y")
nb_pred <- predict(nb_model, new_data=new, type="response", se.fit=T)

Related Question