rcs在lm()模型中生成错误的预测 [英] rcs generates bad prediction in lm() models

查看:96
本文介绍了rcs在lm()模型中生成错误的预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试复制此博客文章过度拟合.我想探索样条曲线与测试多项式的比较.

I'm trying to reproduce this blog post on overfitting. I want to explore how a spline compares to the tested polynomials.

我的问题:使用rms包中的rcs()-受限三次样条曲线,在常规lm()中应用时会得到非常奇怪的预测. ols()可以正常工作,但是我对此奇怪的行为感到有些惊讶.有人可以告诉我发生了什么事吗?

My problem: Using the rcs() - restricted cubic splines - from the rms package I get very strange predictions when applying in regular lm(). The ols() works fine but I'm a little surprised by this strange behavior. Can someone explain to me what's happening?

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"))

您会看到深蓝色遍布整个地方...

As you can see the darkblue is allover the place...

推荐答案

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

You can use rcs() with non-rms fitters as long as you specify the knots. predict defaults to predict.ols for an ols object, which is nice because it "remembers" where it put the knots when it fit the model. predict.lm does not have that functionality, so it uses the distribution of the new data set to determine the placement of the knots, rather than the distribution of the training data.

这篇关于rcs在lm()模型中生成错误的预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆