使用预测来查找非线性模型的值 [英] Using predict to find values of non-linear model

查看:96
本文介绍了使用预测来查找非线性模型的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下一个代码,以尝试预测是否可以帮助我找到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

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))

这篇关于使用预测来查找非线性模型的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆