我尝试使用gam包来安装GAM (我知道mgcv更灵活,但这里需要使用gam )。我现在有一个问题,这个模型看起来很好,但是与原始数据相比,它似乎被一个常数沿y轴偏移,我不知道它是从哪里来的。
此代码复制问题:
library(gam)
data(gam.data)
x <- gam.data$x
y <- gam.data$y
fit <- gam(y ~ s(x,6))
fit$coefficients
#(Intercept) s(x, 6)
# 1.921819 -2.318771
plot(fit, ylim = range(y))
points(x, y)
points(x, y -1.921819, col=2)
legend("topright", pch=1, col=1:2, legend=c("Original", "Minus intercept"))

钱伯斯,J.M.和Hastie,T.J. (1993)在S (Chapman & Hall)中的统计模型表明,不应该有偏移,这也是直观正确的(光滑应该描述数据)。
我注意到了mgcv中类似的东西,它可以通过提供shift参数和模型的截距值来解决(因为平滑看起来是中心的)。我原以为这也是真的,所以我从原始数据点中减去了截距。然而,上面的情节表明这个想法是错误的。我不知道额外的轮班是从哪里来的。我希望这里有人能帮我。
(R版本。3.3.1;gam版本1.12)
发布于 2016-10-21 20:18:47
我想我应该首先解释一下适合的GAM模型的各种输出:
library(gam)
data(gam.data)
x <- gam.data$x
y <- gam.data$y
fit <-gam(y ~ s(x,6), model = FALSE)
## coefficients for parametric part
## this includes intercept and null space of spline
beta <- coef(fit)
## null space of spline smooth (a linear term, just `x`)
nullspace <- fit$smooth.frame[,1]
nullspace - x ## all 0
## smooth space that are penalized
## note, the backfitting procedure guarantees that this is centred
pensmooth <- fit$smooth[,1]
sum(pensmooth) ## centred
# [1] 5.89806e-17
## estimated smooth function (null space + penalized space)
smooth <- nullspace * beta[2] + pensmooth
## centred smooth function (this is what `plot.gam` is going to plot)
c0 <- mean(smooth)
censmooth <- smooth - c0
## additive predictors (this is just fitted values in Gaussian case)
addpred <- beta[1] + smooth您可以首先验证addpred是fit$additive.predictors给出的,因为我们是拟合高斯响应的加性模型,这也和fit$fitted.values一样。
plot.gam所做的就是绘制censmooth
plot.gam(fit, col = 4, ylim = c(-1.5,1.5))
points(x, censmooth, col = "gray")记住,有
addpred = beta[0] + censmooth + c0如果您想要移动原始数据y 以匹配此图,则不仅需要减去拦截(**beta[0]**),,还需要从 y中减去 c0 。
points(x, y - beta[1] - c0)

https://stackoverflow.com/questions/40184092
复制相似问题