【发布时间】:2020-09-23 06:41:49
【问题描述】:
我正在尝试在过度拟合时重现 this blog post。我想探索样条与测试多项式的比较。
我的问题: 使用 rms 包中的 rcs() - 受限三次样条曲线 - 在常规 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"))
如你所见,深蓝色到处都是......
【问题讨论】:
-
rcs并非设计为与lm一起使用 - 您为什么希望它可以使用? -
@hadley:我知道它不是为 lm 设计的。我只是认为所有样条曲线、多项式等都只是将向量转换为矩阵,并且它不是特定于包的。
标签: r linear-regression