使用平滑参数避免过拟合
平滑参数在似然与曲折度之间权衡,以优化模型拟合。这里,您将查看平滑参数,并用不同的固定平滑参数来拟合模型。
本练习是课程的一部分
R 中的广义可加模型(GAM)非线性建模
练习说明
- 通过从模型中提取
sp值,查看已提供的gam_mod模型的平滑参数($\lambda$)。 - 在
mcycle数据上拟合 2 个模型,将accel作为times的平滑函数,并分别使用以下平滑参数:- 0.1
- 0.0001
- 可视化这两个模型。
交互式实操练习
通过完成这段示例代码来试试这个练习。
library(mgcv)
# Extract the smoothing parameter
gam_mod <- gam(accel ~ s(times), data = mcycle, method = "REML")
___
# Fix the smoothing parameter at 0.1
gam_mod_s1 <- gam(accel ~ s(times), data = mcycle, sp = ___)
# Fix the smoothing parameter at 0.0001
gam_mod_s2 <- gam(___)
# Plot both models
par(mfrow = c(2, 1))
plot(gam_mod_s1, residuals = TRUE, pch = 1)
plot(gam_mod_s2, residuals = TRUE, pch = 1)