[R] predict nbinomial glm

K. Steinmann Katharina.Steinmann at stud.unibas.ch
Tue Aug 16 11:42:59 CEST 2005


Dear R-helpers,

let us assume, that I have the following dataset:

a <- rnbinom(200, 1, 0.5)
b <- (1:200)
c <- (30:229)
d <- rep(c("q", "r", "s", "t"), rep(50,4))
data_frame <- data.frame(a,b,c,d)

In a first step I run a glm.nb (full code is given at the end of this mail) and
want to predict my response variable a.
In a second step, I would like to run a glm.nb based on a subset of the
data_frame. As soon as I want to predict the response variable a, I get the
following error message:
"Error in model.frame.default(Terms, newdata, na.action = na.action, xlev =
object$xlevels) :
        factor d has new level(s) q"

Does anybody have a solution to this problem?

Thank you in advance,
K. Steinmann (working with R 2.0.0)


Code:

library(MASS)

a <- rnbinom(200, 1, 0.5)
b <- (1:200)
c <- (30:229)
d <- rep(c("q", "r", "s", "t"), rep(50,4))


data_frame <- data.frame(a,b,c,d)


model_1 = glm.nb(a ~ b + d , data = data_frame)


pred_model_1 = predict(model_1, newdata = data_frame, type = "response", se.fit
= FALSE, dispersion = NULL, terms = NULL)


subset_of_dataframe = subset(data_frame, (b > 80 & c < 190 ))


model_2 = glm.nb(a ~ b + d , data = subset_of_dataframe)
pred_model_2 = predict(model_2, newdata = subset_of_dataframe, type =
"response", se.fit = FALSE, dispersion = NULL, terms = NULL)




More information about the R-help mailing list