Max*_*don 4 r linear-regression
我正在尝试复制这篇关于过度拟合的博客文章。我想探索样条与测试多项式的比较。
我的问题:使用 rcs() - 限制三次样条 - 来自 rms 包我在常规 lm() 中应用时得到非常奇怪的预测。ols() 工作正常,但我对这种奇怪的行为感到有些惊讶。有人可以向我解释发生了什么吗?
library(rms)
p4 <- poly(1:100, degree=4)
true4 <- p4 %*% c(1,2,-6,9)
days <- 1:70
noise4 <- true4 + rnorm(100, sd=.5)
reg.n4.4 <- lm(noise4[1:70] ~ poly(days, 4))
reg.n4.4ns <- lm(noise4[1:70] ~ ns(days,5))
reg.n4.4rcs <- lm(noise4[1:70] ~ rcs(days,5))
dd <- datadist(noise4[1:70], days)
options("datadist" = "dd")
reg.n4.4rcs_ols <- ols(noise4[1:70] ~ rcs(days,5))
plot(1:100, noise4)
nd <- data.frame(days=1:100)
lines(1:100, predict(reg.n4.4, newdata=nd), col="orange", lwd=3)
lines(1:100, predict(reg.n4.4ns, newdata=nd), col="red", lwd=3)
lines(1:100, predict(reg.n4.4rcs, newdata=nd), col="darkblue", lwd=3)
lines(1:100, predict(reg.n4.4rcs_ols, newdata=nd), col="grey", lwd=3)
legend("top", fill=c("orange", "red", "darkblue", "grey"),
legend=c("Poly", "Natural splines", "RCS - lm", "RCS - ols"))
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,深蓝色遍布整个地方......

小智 5
只要指定结,您就可以将 rcs() 与非 rms 钳工一起使用。对于 ols 对象, predict 默认为 predict.ols ,这很好,因为当它适合模型时,它“记住”了它放置结的位置。predict.lm 没有这个功能,所以它使用新数据集的分布来确定节点的位置,而不是训练数据的分布。
| 归档时间: |
|
| 查看次数: |
2408 次 |
| 最近记录: |