r - 使用 R 进行线性预测 : How to access the predicted parameter(s)?

标签 r linear-regression predict

我是R新手,我正在尝试进行线性预测。这是一些简单的数据:

test.frame<-data.frame(year=8:11, value= c(12050,15292,23907,33991))

假设我是否想预测 year=12 的值。这就是我正在做的事情(使用不同的命令进行实验):

lma=lm(test.frame$value~test.frame$year)  # let's get a linear fit
summary(lma)                              # let's see some parameters
attributes(lma)                           # let's see what parameters we can call
lma$coefficients                          # I get the intercept and gradient
predict(lm(test.frame$value~test.frame$year))  
newyear <- 12                             # new value for year
predict.lm(lma, newyear)                  # predicted value for the new year

一些疑问:

  1. 例如,如果我发出命令 lma$coefficients,则会向我返回一个包含两个值的向量。如何只选取截距值?

  2. 我使用 predict.lm(lma, newyear) 获得大量输出,但无法理解预测值在哪里。有人可以澄清一下吗?

非常感谢...

最佳答案

拦截:

lma$coefficients[1]

预测一下,试试这个:

test.frame <- data.frame(year=12, value=0)
predict.lm(lma, test.frame)   

关于r - 使用 R 进行线性预测 : How to access the predicted parameter(s)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8352673/

相关文章:

r - R 中的手动主成分分析

r - 手动检查 R 中的面板单位根测试

python - 使用预测值和实际值的两个相邻列创建 Pandas 数据框

使用 dplyr 和 rollapply 在数据框中滚动预测

json - 将包含嵌入列表的 JSON 解析为扁平化的 data.frame,忽略不需要的键

r - 基于平均月份和滚动数据的值差异

r - 从统计测试中提取输出

python - ValueError : Cannot feed value of shape (2, ) 对于张量 'Placeholder:0' ,其形状为 '(1, 2)'

R使用列索引号预测数据框中每一列的glm拟合

R预测警告