首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >`gam`包:在`plot.gam`上绘制数据时发现的额外移位

`gam`包:在`plot.gam`上绘制数据时发现的额外移位
EN

Stack Overflow用户
提问于 2016-10-21 19:22:03
回答 1查看 433关注 0票数 2

我尝试使用gam包来安装GAM (我知道mgcv更灵活,但这里需要使用gam )。我现在有一个问题,这个模型看起来很好,但是与原始数据相比,它似乎被一个常数沿y轴偏移,我不知道它是从哪里来的。

此代码复制问题:

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-21 20:18:47

我想我应该首先解释一下适合的GAM模型的各种输出:

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

您可以首先验证addpredfit$additive.predictors给出的,因为我们是拟合高斯响应的加性模型,这也和fit$fitted.values一样。

plot.gam所做的就是绘制censmooth

代码语言:javascript
复制
plot.gam(fit, col = 4, ylim = c(-1.5,1.5))
points(x, censmooth, col = "gray")

记住,有

代码语言:javascript
复制
addpred = beta[0] + censmooth + c0

如果您想要移动原始数据y 以匹配此图,则不仅需要减去拦截(**beta[0]**),,还需要从 y中减去 c0

代码语言:javascript
复制
points(x, y - beta[1] - c0)

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

https://stackoverflow.com/questions/40184092

复制
相关文章

相似问题

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