rcs在lm()模型中生成错误的预测

问题描述 投票:1回答:1

我试图在过度拟合时重现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"))

如您所见,整个地方都是深蓝色...

“剧情”

r linear-regression
1个回答
0
投票

您可以将rcs()与非rms钳工配合使用,只要指定了结点即可。预测默认值是ols对象的predict.ols,这很好,因为它可以“记住”适合模型的位置。预报.lm不具有此功能,因此它使用新数据集的分布来确定结的位置,而不是训练数据的分布。

© www.soinside.com 2019 - 2024. All rights reserved.