Using Prediction to Find Nonlinear Model Values

I am trying to have the following code try to figure out if the prediction can help me find the values ​​of the dependent variable for a polynomial of order 2, in this case y = x ^ 2 is obvious:

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 x = 7, I get the following:

> predict(mypol, 7)
Error in eval(predvars, data, env) : not that many frames on the stack

What am I doing wrong?

+5
source share
1 answer

If you read the help for predict.lm, you will see that it takes several arguments, includingnewdata

newdata - an additional data frame in which to search for variables with which you can predict. If omitted, the set values ​​are used.

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

All Articles