Skip to content
Prev 256016 / 398506 Next

glm predict on new data

On 4/6/2011 2:17 PM, dirknbr wrote:
glm (and most modeling functions) are designed to work with data frames, 
not raw vectors.
I'll made up some data, show the way you approached it, show where it 
went wrong, and then how it works more easily.

# data like what I think you had:
y <- rbinom(200, 1, prob=.8)
x <- data.frame(x=rnorm(250))

# your glm call:
lg <- glm(y[1:200]~x[1:200,1],family=binomial)

# take a look at print(lg).  Notice that your independent variable
# name is "x[1:200, 1]", which is what you would need to match in
# a call to predict.

# Make data.frames of the given and testing data.
DF <- data.frame(y=y, x=x[1:200,1])
DF.new <- data.frame(x=x[200:250,1])
# Notice DF.new has the same name (x) as DF.

lg <- glm(y~x, data=DF, family=binomial)
pred <- predict(lg, newdata=DF.new, type="response")
summary(pred)