有没有办法生成不居中的predict.gam(…,type=“terms”值
原始问题: 调用有没有办法生成不居中的predict.gam(…,type=“terms”值,r,gam,mgcv,R,Gam,Mgcv,原始问题: 调用predict.gam(…,type=“terms”)返回以平均值为中心的值。是否有办法获得原始预测项值(即未以平均值为中心的值) 编辑:这是一个重复的示例,我尝试使用lpmatrix获得给定变量的(非居中)拟合值。这些值与使用visreg的值相似,但有一个偏移量。这严格适用于链接为标识且不存在张量积的情况 # read in data air<-data.frame(airquality) air<-air[complete.cases(ai
predict.gam(…,type=“terms”)
返回以平均值为中心的值。是否有办法获得原始预测项值(即未以平均值为中心的值)
编辑:这是一个重复的示例,我尝试使用lpmatrix
获得给定变量的(非居中)拟合值。这些值与使用visreg
的值相似,但有一个偏移量。这严格适用于链接
为标识
且不存在张量积的情况
# 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)
#读入数据
air否。要添加的常量可作为predict()
返回的对象的属性使用,但除此之外,没有自动执行此操作的选项。谢谢。添加常量确实会使术语和predict(…,type=“response”)给出的值相加,但我对术语值本身(而不是它们的和)感兴趣,它们只在中间给出。有没有办法反算非居中值?平滑居中是有充分理由的(可识别性约束)。在模型截距中隔离响应的总体平均值,因此所有平滑均居中。如果你不这样做,就很难适应这个模型。还有其他方法可以添加可识别性约束,但在mgcv中没有。你可能可以抑制这种情况,但这会使拟合变得棘手。你为什么要这样做;什么是最终用例?我的数据是空间的。我试图在每次观察时分离给定变量对响应的影响。在线性模型中,假设y=a*x1+b*x2+c,我会说观察I处x1的效应是a*x1i(即,点I处x1的x1系数*观察值)。我正在寻找一种在GAM中做同样事情的方法。通常的做法是,当使用链接函数而不是标识链接时,这是唯一明智的做法,即使用type='link'
从模型中预测,使用newdata
改变感兴趣的变量并将所有其他变量保持在某个固定值,有代表性的值,比如它们的平均值或中位数。你也可以从平滑曲线图上读取,但y轴不会在响应的自然尺度上,特别是如果使用非标识链接。如果您希望它出现在响应量表上,则按上述方式进行预测(前面的注释),然后使用反向链接转换为响应量表vis.gam()
将帮助您解决此问题。