webbe
webbe

Reputation: 130

Is there a way to produce predict.gam(..., type="terms") values that are NOT centered

Original question:

Calling predict.gam(..., type="terms") returns values that are centered on the average. Is there a way to obtain the raw predicted term values (i.e. ones that have not been centered on the average)?

Edited: Here is a reproducible example of my attempt to get the (non-centered) fitted values of a given variable using lpmatrix. The values are similar to those using visreg but with an offset. This is strictly for the case where the link is identity and there are no tensor products.

    # read in data
    air<-data.frame(airquality)
    air<-air[complete.cases(air),]

    # set up m odel
    model<-gam(Temp~s(Ozone) + s(Solar.R) + s(Wind),data=air,method="ML")

#get predicted values 
predicted<-as.data.frame(predict(model,na.action=na.exclude))

    colnames(predicted)<-"predicted"

# using the lpmatrix, set values of s(Ozone), s(Solar.R), and s(Wind) to 0    
lpmat<-predict(model, type="lpmatrix")
    lpmat_Ozone<-lpmat; lpmat_Ozone[,grep("Ozone",colnames(lpmat))]<-0
    lpmat_Solar.R<-lpmat; lpmat_Solar.R[,grep("Solar.R",colnames(lpmat))]<-0
    lpmat_Wind<-lpmat; lpmat_Wind[,grep("Wind",colnames(lpmat))]<-0

#obtain response predictions with s(each variable) set to 0 (respectively)
    predicted$Ozone<-unname(lpmat_Ozone%*%coef(model))[,1]
    predicted$Solar.R<-unname(lpmat_Solar.R%*%coef(model))[,1]
    predicted$Wind<-unname(lpmat_Wind%*%coef(model))[,1]

#obtain term predictions
    answerdf<-as.data.frame(predicted$predicted - predicted$Ozone)
    colnames(answerdf)<-"Ozone"
    answerdf$Solar.R<-(predicted$predicted - predicted$Solar.R)
    answerdf$Wind<-(predicted$predicted - predicted$Wind)

#visualize using visreg method and the alternative method above 
    visregdat<-visreg(model, "Ozone", plot=FALSE)$fit
    plot(visregFit~Ozone,data=visregdat, type="l", lwd=5, ylim=c(-30,90), ylab= "fitted values")
    points(answerdf$Ozone~air$Ozone, col="violet", pch=20)
    legend(100,60, legend=c("Visreg", "Alt. method"),
           col=c("black", "violet"), pch=20, cex=0.8)

Gives us this plot, showing the same curves but with with different intercepts. Why would this be? enter image description here

Upvotes: 1

Views: 1601

Answers (1)

Gavin Simpson
Gavin Simpson

Reputation: 174813

No. The constant to add is available as an attribute on the object returned by predict(), but otherwise, no, there is no option to do this automatically.

Upvotes: 2

Related Questions