Using predict to find values of non-linear model

♀尐吖头ヾ 提交于 2019-11-27 18:37:35

问题


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  

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

What am I doing wrong?


回答1:


If you read the help for predict.lm, you will see that it takes a number of arguments including newdata

newdata -- An optional data frame in which to look for variables with which to predict. If omitted, the fitted values are used.

predict(mypol, newdata = data.frame(x=7))


来源:https://stackoverflow.com/questions/12593006/using-predict-to-find-values-of-non-linear-model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!