我正在运行线性回归模型并使用GAM包来使用plot.gam函数。我倾向于在以后适合非线性模型。
然而,为了获得y轴上的原始比例,我将根据响应变量制作每个预测器的单独图。该图应显示线性拟合线。但是,我得到的线图在x(maxt)的某个范围内增加,而在另一个范围内减小。我尝试在x变量(maxt)上对数据进行排序,但是没有用。鉴于maxt上的斜率系数是负的,当maxt从负变为正时,拟合值应该均匀下降。但这不会发生。我确信我错过了一些东西。
这是我的R代码
library(gam)
reg.l<-gam(y.res~ maxt.res + mint.res + sr.res+ rain.res + share.res,data=data.res)
#creating newdata for prediction
pred.data<-data.frame(data.res$maxt.res,mint=mean(data.res$mint.res),sr=mean(data.res$sr.res), rain=mean(data.res$rain.res),share_irrigated=mean(data.res$share.res))
names(pred.data)[1]<-"maxt"
ndx=order(pred.data$maxt)
pred.sorted=pred.data[ndx,]
save(pred.sorted,file="/users/ridhima/documents/paper3/R/pred.sorted.rda")
load("/users/ridhima/documents/paper3/R/pred.sorted.rda")
#getting the fitted values
fits=predict(reg.l, newdata=pred.sorted,type="response", se.fit=TRUE)
predicts=data.frame(pred.sorted,fits)
ndx1=order(predicts$maxt)
predicts.sorted=predicts[ndx1,]
library(ggplot2)
pdf("test2.pdf")
ggplot(aes(x=maxt,y=fit),data=predicts.sorted) + geom_smooth(aes(ymin=fit- 1.96*se.fit,ymax=fit+1.96*se.fit),fill="gray80",size=1,stat="identity")+geom_rug(sides="b")
dev.off()
plot.gam显示的情节显示了线性拟合线,但我不知道为什么我的情节不起作用。提前谢谢了。