R:无法预测具体值
> age <- c(23,19,25,10,9,12,11,8)
> steroid <- c(27.1,22.1,21.9,10.7,7.4,18.8,14.7,5.7)
> sample <- data.frame(age,steroid)
> fit2 <- lm(sample$steroid~poly(sample$age,2,raw=TRUE))
> fit2
Call:
lm(formula = sample$steroid ~ poly(sample$age, 2, raw = TRUE))
Coefficients:
(Intercept) -27.7225
poly(sample$age, 2, raw = TRUE)1 5.1819
poly(sample$age, 2, raw = TRUE)2 -0.1265
> (newdata=data.frame(age=15))
age
1 15
> predict(fit2,newdata,interval="predict")
fit lwr upr
1 24.558395 17.841337 31.27545
2 25.077825 17.945550 32.21010
3 22.781034 15.235782 30.32628
4 11.449490 5.130638 17.76834
5 8.670526 2.152853 15.18820
6 16.248596 9.708411 22.78878
7 13.975514 7.616779 20.33425
8 5.638620 -1.398279 12.67552
Warning message:
'newdata' had 1 rows but variable(s) found have 8 rows
为什么predict
函数无法预测age=15?
> age <- c(23,19,25,10,9,12,11,8)
> steroid <- c(27.1,22.1,21.9,10.7,7.4,18.8,14.7,5.7)
> sample <- data.frame(age,steroid)
> fit2 <- lm(sample$steroid~poly(sample$age,2,raw=TRUE))
> fit2
Call:
lm(formula = sample$steroid ~ poly(sample$age, 2, raw = TRUE))
Coefficients:
(Intercept) -27.7225
poly(sample$age, 2, raw = TRUE)1 5.1819
poly(sample$age, 2, raw = TRUE)2 -0.1265
> (newdata=data.frame(age=15))
age
1 15
> predict(fit2,newdata,interval="predict")
fit lwr upr
1 24.558395 17.841337 31.27545
2 25.077825 17.945550 32.21010
3 22.781034 15.235782 30.32628
4 11.449490 5.130638 17.76834
5 8.670526 2.152853 15.18820
6 16.248596 9.708411 22.78878
7 13.975514 7.616779 20.33425
8 5.638620 -1.398279 12.67552
Warning message:
'newdata' had 1 rows but variable(s) found have 8 rows
Why does the predict
function unable to predict for age=15?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
lm(y ~ x, data)
形式代替lm(data$y ~ data$x)
。那应该可以解决你的问题。编辑:问题不仅在于对
lm
的调用,还在于poly(*, raw=TRUE)
的使用。如果删除raw=TRUE
位,它就应该可以工作。不知道为什么raw=TRUE
在这里中断。Instead of
lm(data$y ~ data$x)
, use the formlm(y ~ x, data)
. That should solve your problem.EDIT: the problem is not only with the call to
lm
, but also the use ofpoly(*, raw=TRUE)
. If you remove theraw=TRUE
bit, it should then work. Not sure whyraw=TRUE
breaks here.