[Rd] Recommended package nlme: bug in predict.lme when an independent variable is a polynomial (PR#8905)

renaud.lancelot at cirad.fr renaud.lancelot at cirad.fr
Sat May 27 16:19:52 CEST 2006


Full_Name: Renaud Lancelot
Version: Version 2.3.0 (2006-04-24)
OS: MS Windows XP Pro SP2
Submission from: (NULL) (82.239.219.108)


I think there is a bug in predict.lme, when a polynomial generated by poly() is
used as an explanatory variable, and a new data.frame is used for predictions. I
guess this is related to * not * using, for predictions, the coefs used in
constructing the orthogonal polynomials before fitting the model:

> fm <- lme(distance ~ poly(age, 3) + Sex, data = Orthodont, random = ~ 1)
> 
> # data for predictions
> Newdata <- head(Orthodont)
> Newdata$Sex <- factor(Newdata$Sex, levels = levels(Orthodont$Sex))
> 
> # "naive" model matrix for predictions
> mm1 <- model.matrix(~ poly(age, 3) + Sex, data = Newdata)
> 
> # "correct" model matrix for predictions
> p <- poly(Orthodont$age, 3)
> mm2 <- model.matrix(~ poly(age, 3, coefs = attr(p, "coefs")) + Sex, data =
Newdata)
> 
> data.frame(pred1 = predict(fm, level = 0, newdata = Newdata),
+            pred2 = mm1 %*% fixef(fm),
+            pred3 = head(predict(fm, level = 0)),
+            pred4 = mm2 %*% fixef(fm))
     pred1    pred2    pred3    pred4
1 18.61469 18.61469 23.13079 23.13079
2 23.23968 23.23968 24.11227 24.11227
3 29.90620 29.90620 25.59375 25.59375
4 36.19756 36.19756 27.03819 27.03819
5 18.61469 18.61469 23.13079 23.13079
6 23.23968 23.23968 24.11227 24.11227

Best regards,

Renaud



More information about the R-devel mailing list