`gam` 包:在 `plot.gam` 上绘制数据时发现额外偏移
`gam` package: extra shift spotted when sketching data on `plot.gam`
我尝试使用 gam
包来安装 GAM(我知道 mgcv
更灵活,但我需要在这里使用 gam
)。我现在遇到的问题是模型看起来不错,但与原始数据相比,它似乎沿着 y-axis 偏移了一个常数值,为此我无法弄清楚这是从哪里来的。
此代码重现了问题:
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"))
Chambers, J. M. 和 Hastie, T. J. (1993) Statistical Models in S (Chapman & Hall) 表明不应该存在偏移量,这在直觉上也是正确的(平滑应该描述数据)。
我注意到 mgcv
中有类似的东西,可以通过为 shift
参数提供模型的截距值来解决(因为平滑看起来居中)。我想这里也是一样的,所以我从原来的 data-points 中减去截距。然而,上面的情节表明这个想法是错误的。我不知道额外的转变来自哪里。我希望这里有人可以帮助我。
(R 版本。3.3.1;gam
版本 1.12)
我想我应该先解释一下拟合的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]
),还需要从中减去c0
y
:
points(x, y - beta[1] - c0)
我尝试使用 gam
包来安装 GAM(我知道 mgcv
更灵活,但我需要在这里使用 gam
)。我现在遇到的问题是模型看起来不错,但与原始数据相比,它似乎沿着 y-axis 偏移了一个常数值,为此我无法弄清楚这是从哪里来的。
此代码重现了问题:
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"))
Chambers, J. M. 和 Hastie, T. J. (1993) Statistical Models in S (Chapman & Hall) 表明不应该存在偏移量,这在直觉上也是正确的(平滑应该描述数据)。
我注意到 mgcv
中有类似的东西,可以通过为 shift
参数提供模型的截距值来解决(因为平滑看起来居中)。我想这里也是一样的,所以我从原来的 data-points 中减去截距。然而,上面的情节表明这个想法是错误的。我不知道额外的转变来自哪里。我希望这里有人可以帮助我。
(R 版本。3.3.1;gam
版本 1.12)
我想我应该先解释一下拟合的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]
),还需要从中减去c0
y
:
points(x, y - beta[1] - c0)