[英]Is there a way to produce predict.gam(..., type="terms") values that are NOT centered
原問題:
調用predict.gam(..., type="terms")
返回以平均值為中心的值。 有沒有辦法獲得原始預測項值(即那些沒有以平均值為中心的值)?
編輯:這是我嘗試使用lpmatrix
給定變量的(非中心)擬合值的可lpmatrix
。 這些值與使用visreg
的值相似,但具有偏移量。 這嚴格適用於link
是identity
且沒有張量積的情況。
# 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)
不可以。要添加的常量可用作predict()
返回的對象的屬性,否則,不可以,沒有選項可以自動執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.