如何从R中的线性回归预测单个值? [英] How can I predict a single value from a linear regression in R?
问题描述
我已经将美元价格与GDPPC进行了线性回归,像这样:
I've made a linear regression of dollar prices to GDPPC like so:
r = lm(dollar_value ~ GDPPC, prices_gdp)
( prices_gdp
是 data.table
,如果这很重要)。
(prices_gdp
is a data.table
, if that matters).
我现在可以轻松地基于<$ c生成一堆值$ c> data.table 使用 predict
。但是我要做的是(为了在图表上绘制 geom_abline
)是在GDPPC为零时计算美元价值,然后将其取回为数字, em> like
I can now easily generate a bunch of values based on a data.table
using predict
. But what I want to do (in order to plot a geom_abline
on a chart) is calculate the dollar value when GDPPC is zero, and get that back as a number—something like
predict(r, 0)
这给了我一个错误: eval中的错误(predvars,data,env):找不到对象'GDPPC'
。有什么办法可以做到,没有创建一个新的虚拟机 data.table
,将GDPPC = 0作为唯一的行,输入它,然后将其取出呢? / p>
This gives me an error: Error in eval(predvars, data, env): object 'GDPPC' not found
. Is there any way of doing this short of creating a new dummy data.table
with GDPPC=0 as its only row, feeding it in, and then pulling the number out?
推荐答案
您可以创建相同的数据表,并将回归系数GDPPC设置为零。尝试:
You can just create the same data table and put the regressor GDPPC to zero. Try:
predict(r, data.frame(GDPPC = 0))
这篇关于如何从R中的线性回归预测单个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!