[R] predict nbinomial glm

From: K. Steinmann <Katharina.Steinmann_at_stud.unibas.ch>
Date: Tue 16 Aug 2005 - 19:42:59 EST


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)



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Aug 16 19:49:55 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:49 EST