2011-12-02 90 views
2

我是R的新手,我正在嘗試做線性預測。這裏有一些簡單的數據:用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)但無法理解預測值在哪裏。有人可以澄清?

非常感謝......

+0

我已經更新了我的答案,讓正確的答案與變你的問題的名字 – abcde123483

+1

另外,'lm(value〜year,data = test.frame)'是指定模型的一種更可讀的方式,第一次學習R時相當興奮。 –

+0

@ mindless.panda好的謝謝。 1投票起來 – yCalleecharan

回答

4

攔截:

lma$coefficients[1] 

預測,試試這個:

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

@ ulvund謝謝。它很好地工作。 1投票。 – yCalleecharan