Skip to content
Prev 313949 / 398506 Next

scope, lme, ns, nlme, splines

Your solution works beautifully for predict.lme in the same data that
were used to compute the model. But what if I want to compute the 
population fitted values in newdata? In the expanded example below,
predict.lm is able to find the object called "fixed". But predict.lme is
unable to find it and returns an error.

library("nlme")
library(splines)
library(ggplot2)
set.seed(5)
junk<-data.frame(x=rnorm(100))
junk$y<-junk$x + rnorm(nrow(junk))
junk$idvar<- factor(sample(LETTERS[1:10], size=nrow(junk), replace=TRUE))
PRETTYNEWDATA<-data.frame(x=seq(-2, 2, length=20))

fitfunction<-function(splineDF=1) {
       fixed <- eval(
          substitute(
                expr= y ~ ns(x, df=splineDF)
                , env= list(splineDF=splineDF)
             )
          )
 		thislm<- lm( fixed , data= junk)
 		junk$fitlm<-predict(thislm)
       thislme<-lme( fixed= fixed , random= ~ 1 | idvar , data= junk)
       junk$fitlme<-predict(thislme,level=0)
       print(
          ggplot( junk, aes(x, y))
             + geom_point(shape=1)
             + geom_line( aes(x, fitlme), color="red", lwd=2)
             + geom_line( aes(x, fitlm), color="blue", lty=3)
 				+ ggtitle(deparse(fixed))
          )
       PRETTYNEWDATA$fitlm<-predict(thislm,newdata=PRETTYNEWDATA)
 		print(head(PRETTYNEWDATA))
 	cat("about to use newdata with lme \n")
       PRETTYNEWDATA$fitlme<-predict(thislme,level=0, newdata=PRETTYNEWDATA)
       thislme
}

Jake Wegelin
On Thu, 6 Dec 2012, Duncan Murdoch wrote: