線性迴歸 _ r 學習筆記01

6
13/6/26 線性迴歸 | R 學習筆記 statlab.nchc.org.tw/rnotes/?page_id=418 1/6 線性迴歸 [ 內容撰寫中] 迴歸分析中最簡單的就是線性迴歸,在 R 中線性的模型配適可以使用 lm() 函數,其支援的參 數有: formula :迴歸模型。 data :資料欄。 subset :用於指定配適回歸模型的資料。 weights :權重,用於加權迴歸。 na.action :指定當資料出現 NA 時的處理方式。 method :指定迴歸配適的方法,目前支援的方法只有 method="qr" model TRUE FALSE ,指定是否輸出迴歸模型之資料欄。 x TRUE FALSE ,指定是否輸出設計矩陣(design matrixmodel matrix)。 y TRUE FALSE ,指定是否輸出反應變數。 qr TRUE FALSE ,指定是否輸出 QR 分解。 singular.ok TRUE FALSE ,指定是否允許 singular fit contrasts :請參考 help(model.matrix.default) contrasts.arg offset :設定模型之 offset ,請參考 help(offset) 其傳回值為 lm 物件,含有下列變數: coefficients :迴歸係數(coeffieients)。 residuals :殘差值(residuals)。 fitted.values :配適結果(fitted values)。 rank the numeric rank of the fitted linear model. weights :權重(weights),只有在加權迴歸時才有此變數。 df.residual :殘差值(residual)的自由度(degrees of freedom)。 call :實際呼叫的函數。 contrasts (only where relevant) the contrasts used. xlevels (only where relevant) a record of the levels of the factors used in fitting. offset :迴歸模型所使用的 offset ,只有在模型有使用 offset 時才有此變數。 y :反應變數,只有在設定 y = TRUE 參數時才有此變數。 x :設計矩陣(design matrixmodel matrix),只有在設定 x = TRUE 參數時才有此變 數。 model :迴歸模型之資料欄,只有在設定 model = TRUE 參數時才有此變數。 na.action :對 NA 資料的處理方式。

Upload: sara-lee

Post on 01-Dec-2015

184 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: 線性迴歸 _ R 學習筆記01

13/6/26 線性迴歸 | R 學習筆記

statlab.nchc.org.tw/rnotes/?page_id=418 1/6

線性迴歸

[內容撰寫中]

迴歸分析中最簡單的就是線性迴歸,在 R 中線性的模型配適可以使用 lm() 函數,其支援的參

數有:

formula:迴歸模型。

data:資料欄。

subset:用於指定配適回歸模型的資料。

weights:權重,用於加權迴歸。

na.action:指定當資料出現 NA 時的處理方式。

method:指定迴歸配適的方法,目前支援的方法只有 method="qr"。

model:TRUE 或 FALSE,指定是否輸出迴歸模型之資料欄。

x:TRUE 或 FALSE,指定是否輸出設計矩陣(design matrix、model matrix)。

y:TRUE 或 FALSE,指定是否輸出反應變數。

qr:TRUE 或 FALSE,指定是否輸出 QR 分解。

singular.ok:TRUE 或 FALSE,指定是否允許 singular fit。

contrasts:請參考 help(model.matrix.default) 之 contrasts.arg。

offset:設定模型之 offset,請參考 help(offset)。

其傳回值為 lm 物件,含有下列變數:

coefficients:迴歸係數(coeffieients)。

residuals:殘差值(residuals)。

fitted.values:配適結果(fitted values)。

rank:the numeric rank of the fitted linear model.

weights:權重(weights),只有在加權迴歸時才有此變數。

df.residual:殘差值(residual)的自由度(degrees of freedom)。

call:實際呼叫的函數。

contrasts:(only where relevant) the contrasts used.

xlevels:(only where relevant) a record of the levels of the factors used in fitting.

offset:迴歸模型所使用的 offset,只有在模型有使用 offset 時才有此變數。

y:反應變數,只有在設定 y = TRUE 參數時才有此變數。

x:設計矩陣(design matrix、model matrix),只有在設定 x = TRUE 參數時才有此變

數。

model:迴歸模型之資料欄,只有在設定 model = TRUE 參數時才有此變數。

na.action:對 NA 資料的處理方式。

Page 2: 線性迴歸 _ R 學習筆記01

13/6/26 線性迴歸 | R 學習筆記

statlab.nchc.org.tw/rnotes/?page_id=418 2/6

lm() 函數可用於配適各種線性模型,以下介紹以 lm() 配適線性模型、變數選擇(variable

selection)與模型診斷(model diagnostic)。

簡單線性迴歸(Simple Linear Regression)

假設我們的解釋變數(explanatory variable)為 x,而反應變數(response variable)為 y:

以 lm() 配適簡單線性迴歸:

xy.lm 就是配適出來的線性模型物件,要取用物件內的變數,可使用 $ 運算子,例如取用迴歸

係數:

lm 物件可以直接將其輸出查看基本的資訊:

輸出為

Call:

lm(formula = y ~ x)

Coefficients:

(Intercept) x

-1.274 3.282

或用 summary() 查看更詳細的資料:

輸出為

Call:

lm(formula = y ~ x)

Residuals:

Min 1Q Median 3Q Max

-3.6761 -1.1256 -0.1126 1.7858 3.3250

Coefficients:

Estimate Std. Error t value Pr(>|t|)

12

x <- c(8.4, 7.0, 5.4, 8.8, 5.5, 0.1, 8.7, 8.4, 1.6, 9.4)y <- c(29.3, 20.4, 16.7, 27.0, 13.1, 1.1, 23.7, 27.3, 3.5, 32.9)

1 xy.lm <- lm(y ~ x)

1 xy.lm$coefficients

1 xy.lm

1 summary(xy.lm)

Page 3: 線性迴歸 _ R 學習筆記01

13/6/26 線性迴歸 | R 學習筆記

statlab.nchc.org.tw/rnotes/?page_id=418 3/6

(Intercept) -1.2736 1.8929 -0.673 0.52

x 3.2818 0.2694 12.180 1.91e-06 ***

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.597 on 8 degrees of freedom

Multiple R-squared: 0.9488, Adjusted R-squared: 0.9424

F-statistic: 148.4 on 1 and 8 DF, p-value: 1.913e-06

此迴歸之 R-squared 為 0.9488,而 Adjusted R-squared 為 0.9424,配適的情況不錯。最後一

行的 F-statistic 是檢定是否所有的解釋變數的參數都是 0,若是在簡單線性迴歸中(只有一個

解釋變數)此 F 檢定等同於個別解釋變數的 t 檢定,因此下方 F 檢定的 p-value 與上方 t 檢定

的 p-value 值相同,都是 1.913e-06,所以這個解釋變數的係數不為 0。

將配適的迴歸線畫出來:

圖形為

12

plot(y ~ x)abline(xy.lm)

Page 4: 線性迴歸 _ R 學習筆記01

13/6/26 線性迴歸 | R 學習筆記

statlab.nchc.org.tw/rnotes/?page_id=418 4/6

複迴歸

以 faraway 套件中的 savings 資料作為範例,首先載入 faraway 套件:

並確定 savings 資料有正確載入:

此資料是 1960 至 1970 年各國的平均積蓄資料,其內容為:

sr pop15 pop75 dpi ddpi

Australia 11.43 29.35 2.87 2329.68 2.87

Austria 12.07 23.32 4.41 1507.99 3.93

Belgium 13.17 23.80 4.43 2108.47 3.82

Bolivia 5.75 41.89 1.67 189.13 0.22

Brazil 12.88 42.19 0.83 728.47 4.56

[略]

1 library(faraway)

1 savings

Page 5: 線性迴歸 _ R 學習筆記01

13/6/26 線性迴歸 | R 學習筆記

statlab.nchc.org.tw/rnotes/?page_id=418 5/6

關於資料詳細的說明請參考 savings 線上手冊(help(savings))。

接下來以 lm() 函數對 savings 資料進行複迴歸分析,假設我們要看 sr 與其他變數的關係,則

sr 為反應變數(response variable),其餘為解釋變數(explanatory variable):

得到的線性模型物件 savings.lm 一樣可以使用 summary() 函數查看詳細資料:

變異數分析(ANOVA)

變異數分析(ANOVA)可使用 anova() 函數:

輸出為

Analysis of Variance Table

Response: y

Df Sum Sq Mean Sq F value Pr(>F)

x 1 1000.55 1000.55 148.36 1.913e-06 ***

Residuals 8 53.95 6.74

---

Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

或是使用 aov() 函數也可以:

輸出為

Call:

aov(formula = xy.lm)

Terms:

x Residuals

Sum of Squares 1000.547 53.953

Deg. of Freedom 1 8

Residual standard error: 2.596946

Estimated effects may be unbalanced

1 savings.lm <- lm(sr ~ pop15 + pop75 + dpi + ddpi, data = savings)

1 summary(savings.lm)

1 anova(xy.lm)

1 aov(xy.lm)

Page 6: 線性迴歸 _ R 學習筆記01

13/6/26 線性迴歸 | R 學習筆記

statlab.nchc.org.tw/rnotes/?page_id=418 6/6

Cross Validation

先安裝 DAGG 套件並載入它(套件的安裝請參考套件):

使用 cv.lm() 函數進行 cross validation:

12

install.packages("DAGG")library(DAGG)

1 cv.lm(data.frame(x, y), xy.lm)