本文介绍了使用预测来查找非线性模型的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试下一个代码,以尝试预测是否可以帮助我找到2阶多项式的因变量值,在这种情况下,显然y = x ^ 2:
I'm trying the next code to try to see if predict can help me to find the values of the dependent variable for a polynomial of order 2, in this case it is obvious 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的值,我会得到:
If I try to find the value of x=7, I get this:
> predict(mypol, 7)
Error in eval(predvars, data, env) : not that many frames on the stack
我做错了什么?
推荐答案
如果您阅读了predict.lm
的帮助,则会看到它包含许多参数,包括newdata
If you read the help for predict.lm
, you will see that it takes a number of arguments including newdata
predict(mypol, newdata = data.frame(x=7))
这篇关于使用预测来查找非线性模型的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!