[Rd] using predict method with an offset

Kenneth Knoblauch ken.knoblauch at inserm.fr
Thu Feb 26 15:15:08 CET 2009


Hi,

I have run into another problem using offsets, this time with
the predict function, where there seems to be a contradiction
again between the behavior and the help page.

On the man page for predict.lm, it says

Offsets specified by offset in the fit by lm will not be included in
predictions, whereas those specified by an offset term in the formula
will be.

While it indicates nothings about offsets under ?predict.glm, predict.glm
calls predict.lm. when there is a newdata argument.

In the example below, the behavior is the opposite of the help
page, if I am understanding it correctly, and a warning is thrown
when it does seem to work as desired.

c1 <- structure(list(Contr = c(0.028, 0.043, 0.064, 0.097, 0.146, 0.219
), Correct = c(34L, 57L, 94L, 152L, 160L, 160L), Incorrect = c(126L,
103L, 66L, 8L, 0L, 0L)), .Names = c("Contr", "Correct", "Incorrect"
), row.names = c("13", "15", "17", "19", "21", "23"), class = "data.frame")

q25 <- rep( qlogis( 0.25 ), nrow(c1) )

# offset defined in arguments
c1.glm <- glm( cbind(Correct, Incorrect) ~ Contr - 1, binomial,
	c1, offset = q25 )
# offset defined in formula
c1f.glm <- glm( cbind(Correct, Incorrect) ~ Contr + offset(q25) -1,
	binomial, c1 )
cc <- seq( 0, 1, len = 10 )
nd <- data.frame( Contr = cc )

When predict used with model for which offset was defined in
the arguments, offset is taken into account and a warning
is emitted.

predict(c1.glm, newdata = nd, type = "response")

         1         2         3         4         5         6         7
0.2500000 0.8859251 0.9945037 0.9997628 0.9999898 0.9999996 1.0000000
         8         9        10
1.0000000 1.0000000 1.0000000
Warning message:
In predictor + offset :
   longer object length is not a multiple of shorter object length

When predict used with model for which offset was defined in
the formula, an error occurs

predict( c1f.glm, newdata = nd )

Error in model.frame.default(Terms, newdata, na.action = na.action,  
xlev = object$xlevels) :
   variable lengths differ (found for 'offset(q25)')

even if a column for offset is included in newdata,

ndf <- cbind( nd, "offset(q25)" = rep( qlogis(0.25), length(cc) ) )
predict( c1f.glm, newdata = ndf )

Error in model.frame.default(Terms, newdata, na.action = na.action,  
xlev = object$xlevels) :
   variable lengths differ (found for 'offset(q25)')

unless there is a special way to specify the offset to predict
that I haven't been able to figure out.

traceback indicates the problem, again, with model.frame.default

Thank you for any clarification.

best,

Ken


-- 
Ken Knoblauch
Inserm U846
Stem-cell and Brain Research Institute
Department of Integrative Neurosciences
18 avenue du Doyen Lépine
69500 Bron
France
tel: +33 (0)4 72 91 34 77
fax: +33 (0)4 72 91 34 61
portable: +33 (0)6 84 10 64 10
http://www.sbri.fr/members/kenneth-knoblauch.html



More information about the R-devel mailing list