为什么我的 GAM fit 似乎没有正确的拦截? [R]

why my GAM fit doesn't seem to have a correct intecept? [R]

我的 GAM 曲线正在向下移动。拦截有问题吗?我使用与 Introduction to statistical learning 相同的代码...任何帮助表示赞赏..

这是代码。我模拟了一些数据(一条带有噪声的直线),并使用 bootstrap 多次拟合 GAM。 (我花了一段时间才弄清楚如何在一张图中绘制多个 GAM 拟合。感谢 this post Sam's answer, and this post

library(gam)

N = 1e2

set.seed(123)

dat = data.frame(x = 1:N,
                 y = seq(0, 5, length = N) + rnorm(N, mean = 0, sd = 2))
plot(dat$x, dat$y, xlim = c(1,100), ylim = c(-5,10))


gamFit = vector('list', 5)

for (ii in 1:5){

        ind = sample(1:N, N, replace = T)  #bootstrap
        gamFit[[ii]] = gam(y ~ s(x, 10), data = dat, subset = ind)

        par(new=T)

        plot(gamFit[[ii]], col = 'blue',
             xlim = c(1,100), ylim = c(-5,10),
             axes = F, xlab='', ylab='')
}

问题出在 plot.gam。如果您看一下帮助页面 (?plot.gam),有一个名为 scale 的参数,它指出:

a lower limit for the number of units covered by the limits on the ‘y’ for each plot. The default is scale=0, in which case each plot uses the range of the functions being plotted to create their ylim. By setting scale to be the maximum value of diff(ylim) for all the plots, then all subsequent plots will produced in the same vertical units. This is essential for comparing the importance of fitted terms in additive models.

这是一个问题,因为您没有使用正在绘制的函数的范围(即 y 的范围不是 -5 到 10)。所以你需要做的就是改变

plot(gamFit[[ii]], col = 'blue',
     xlim = c(1,100), ylim = c(-5,10),
     axes = F, xlab='', ylab='')

plot(gamFit[[ii]], col = 'blue',
     scale = 15,
     axes = F, xlab='', ylab='')

你得到:

或者您可以从对 plot 的两次调用中删除 xlimylim 参数,并自动设置 plot 以使用整个范围数据将使一切正常。