我是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
一些疑问:
例如,如果我发出命令
lma$coefficients
,则会向我返回一个包含两个值的向量。如何只选取截距值?我使用
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/