简体   繁体   中英

Issues predicting with `nlme::gls`quadratic model fitted with `poly(..., 2)`

I have fitted a quadratic model with a variance structure that allows different variance levels per level of a factor, and I'm having trouble predicting on a new data set with 2 entries only. Here's a reproducible example:

library(nlme)
set.seed(101)
mtcars$amf <- factor(mtcars$am)
modGLS <- gls(mpg ~ amf*poly(hp, 2),
           weights = varIdent(form = ~ 1|amf), data = mtcars)
minhp <- min(mtcars$hp); maxhp <- max(mtcars$hp)
newdata <- data.frame(amf = as.factor(c(0, 1)),
                        hp = round(runif(2, min = minhp, max = maxhp)))
newdata2 <- data.frame(amf = as.factor(c(0, 0, 1)),
                        hp = round(runif(3, min = minhp, max = maxhp)))
predict(modGLS, newdata = newdata)
# Error in poly(hp, 2) : 'degree' must be less than number of unique points

predict(modGLS, newdata = newdata2)
## [1]  5.973306 13.758955 44.037921
## attr(,"label")
## [1] "Predicted values"

However, the prediction works well on an lm framework:

modLM <- lm(mpg ~ amf*poly(hp, 2), mtcars)
predict(modLM, newdata = newdata)
##        1        2 
## 25.22253 16.83943 

Why would that be? One of the package maintainers of emmeans seems to believe this may be related with missing information on attr(, “predvars”) (see our discussion here https://github.com/rvlenth/emmeans/issues/133 )

I have reported this to Dr Bates ( nlme point of contact) but thought I'd reach out to the wider community too. Thanks in advance

Thanks to @BenBolker and @russ-lenth for confirming that the issue is related to the missing terms attribute "predvars" in the GLS object, which provides the fitted coefficients for poly . Notice how this works in an LM framework (original post) and the attribute is there (see also ?makepredictcall ). Note that this can have potential implications for prediction.

## e.g. this fails
poly(newdata$hp, 2)
## but this is okay, because the polynomial has been estimated
polyFit <- poly(mtcars$hp, 2)
predict(polyFit, newdata = newdata$hp)

attr(terms(modLM), "predvars")
## list(mpg, amf, poly(hp, 2, coefs = list(alpha = c(146.6875, 198.071514938048), norm2 = c(1, 32, 145726.875, 977168457.086594))))

attr(terms(modGLS), "predvars")
## NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM