![](/img/trans.png)
[英]Non-linear model predict NA values in a leave one ID out cross validation mode
[英]Using predict to find values of non-linear model
我正在尝试下一个代码,以尝试查看 predict 是否可以帮助我找到 2 阶多项式的因变量值,在这种情况下很明显 y=x^2:
x <- c(1, 2, 3, 4, 5 , 6)
y <- c(1, 4, 9, 16, 25, 36)
mypol <- lm(y ~ poly(x, 2, raw=TRUE))
> mypol
Call:
lm(formula = y ~ poly(x, 2, raw = TRUE))
Coefficients:
(Intercept) poly(x, 2, raw = TRUE)1 poly(x, 2, raw = TRUE)2
0 0 1
如果我试图找到 x=7 的值,我会得到这个:
> predict(mypol, 7)
Error in eval(predvars, data, env) : not that many frames on the stack
我究竟做错了什么?
如果您阅读predict.lm
的帮助,您将看到它需要许多参数,包括newdata
newdata -- 一个可选的数据框,在其中查找要预测的变量。 如果省略,则使用拟合值。
predict(mypol, newdata = data.frame(x=7))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.