首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有没有办法产生不居中的predict.gam(...,type="terms")值?

有没有办法产生不居中的predict.gam(...,type="terms")值?
EN

Stack Overflow用户
提问于 2020-02-11 10:56:36
回答 1查看 643关注 0票数 0

原问题:

调用predict.gam(..., type="terms")会返回以平均值为中心的值。有没有办法获得原始的预测项值(即那些没有以平均值为中心的值)?

编辑:以下是我尝试使用lpmatrix获取给定变量的(非居中)拟合值的可重复示例。这些值与使用visreg的值相似,但有一个偏移量。这严格适用于linkidentity且没有张量积的情况。

代码语言:javascript
运行
复制
    # 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)

给了我们这张图,显示了相同的曲线,但截距不同。为什么会这样呢?

EN

回答 1

Stack Overflow用户

发布于 2020-02-11 12:19:00

不是的。要添加的常量可以作为predict()返回的对象上的一个属性,但除此之外,没有选项可以自动执行此操作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60161344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档