我正在尝试重现这篇博文 http://www.portfolioprobe.com/2011/03/28/the-devil-of-overfitting/关于过度拟合。我想探索样条曲线与测试多项式的比较。
我的问题:使用 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"))
正如你所看到的,深蓝色无处不在......