一般線形モデル(重回帰や分散分析など)の関数lm()が返す結果オブジェクトの中味

2009.1.12(2008.4.18より)

 一般線形モデル(正規線形モデル、つまり、重回帰や分散分析や共分散分析の大部分)を扱う、Rの関数lm()が返してくる、オブジェクトの中味のメモです。

 こんなデータ(以下のx1とy1)を使って直線回帰をするという例で、関数lm()の結果が入っているオブジェクトの中味を見てみます。まず、使ったデータです。
> x1
[1] 0.58941912 1.12998140 -0.32388348 1.47011907 0.18353773 -0.86984008 0.09616585
[8] 1.52924473 0.30353585 -0.23631159 -1.54928327 -1.48225806
> y1
[1] -0.60754212 0.18604543 1.14655748 -0.28869604 0.65738843 -1.10658161 1.01419318
[8] 1.83122581 0.04195594 0.33037116 -0.59844476 -1.66059471

lmの結果は以下のようにres1に入れておきます

> res1<-lm(y1~x1)

res1の中味を関数str()で見ます。

> str(res1)


List of 12
$ coefficients : Named num [1:2] 0.0412 0.5373
..- attr(*, "names")= chr [1:2] "(Intercept)" "x1"
$ residuals : Named num [1:12] -0.965 -0.462 1.279 -1.120 0.518 ...
..- attr(*, "names")= chr [1:12] "1" "2" "3" "4" ...
$ effects : Named num [1:12] -0.273 -1.839 1.467 -0.831 0.734 ...
..- attr(*, "names")= chr [1:12] "(Intercept)" "x1" "" "" ...
$ rank : int 2
$ fitted.values: Named num [1:12] 0.358 0.648 -0.133 0.831 0.140 ...
..- attr(*, "names")= chr [1:12] "1" "2" "3" "4" ...
$ assign : int [1:2] 0 1
$ qr :List of 5
..$ qr : num [1:12, 1:2] -3.464 0.289 0.289 0.289 0.289 ...
.. ..- attr(*, "dimnames")=List of 2
.. .. ..$ : chr [1:12] "1" "2" "3" "4" ...
.. .. ..$ : chr [1:2] "(Intercept)" "x1"
.. ..- attr(*, "assign")= int [1:2] 0 1
..$ qraux: num [1:2] 1.29 1.28
..$ pivot: int [1:2] 1 2
..$ tol : num 1e-07
..$ rank : int 2
..- attr(*, "class")= chr "qr"
$ df.residual : int 10
$ xlevels : list()
$ call : language lm(formula = y1 ~ x1)
$ terms :Classes 'terms', 'formula' length 3 y1 ~ x1
.. ..- attr(*, "variables")= language list(y1, x1)
.. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. ..- attr(*, "dimnames")=List of 2
.. .. .. ..$ : chr [1:2] "y1" "x1"
.. .. .. ..$ : chr "x1"
.. ..- attr(*, "term.labels")= chr "x1"
.. ..- attr(*, "order")= int 1
.. ..- attr(*, "intercept")= int 1
.. ..- attr(*, "response")= int 1
.. ..- attr(*, ".Environment")=<R_GlobalEnv>
.. ..- attr(*, "predvars")= language list(y1, x1)
.. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. ..- attr(*, "names")= chr [1:2] "y1" "x1"
$ model :'data.frame': 12 obs. of 2 variables:
..$ y1: num [1:12] -0.608 0.186 1.147 -0.289 0.657 ...
..$ x1: num [1:12] 0.589 1.130 -0.324 1.470 0.184 ...
..- attr(*, "terms")=Classes 'terms', 'formula' length 3 y1 ~ x1
.. .. ..- attr(*, "variables")= language list(y1, x1)
.. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
.. .. .. ..- attr(*, "dimnames")=List of 2
.. .. .. .. ..$ : chr [1:2] "y1" "x1"
.. .. .. .. ..$ : chr "x1"
.. .. ..- attr(*, "term.labels")= chr "x1"
.. .. ..- attr(*, "order")= int 1
.. .. ..- attr(*, "intercept")= int 1
.. .. ..- attr(*, "response")= int 1
.. .. ..- attr(*, ".Environment")=<R_GlobalEnv>
.. .. ..- attr(*, "predvars")= language list(y1, x1)
.. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
.. .. .. ..- attr(*, "names")= chr [1:2] "y1" "x1"
- attr(*, "class")= chr "lm"

となります。

「List of 12」は、以下の12個のことです(重みをつけたときはこれにweightsが加わって13個となります)。
$ coefficients
$ residuals
$ effects
$ rank
$ fitted.values
$ assign
$ qr
$ df.residual
$ xlevels
$ call
$ terms
$ model


$ coefficientsは推定された回帰式の係数です

> res1$coefficients

で以下のように中味を見ることができます。

(Intercept) x1
0.04119304 0.53730014

(Intercept)の下の数値が切片で、x1は説明変数x1の回帰係数(直線回帰なので、傾き)です。

Named numとあるように、名前付きのベクトルで個々の要素は数値です。

また、.- attr(*, "names")とあるのは、

> attr(res1$coefficients ,"names")

[1] "(Intercept)" "x1"

と名前の部分("(Intercept)"と "x1")を見られることを示しています。

> res1$coefficients["x1"]

とすると

x1
0.5373001

数値(説明変数x1の回帰係数[傾き])だけがほしいなら

> res1$coefficients[["x1"]]

[1] 0.5373001

となります。"x1"のところを、"(Intercept)"にすれば切片の値が返されます。

 res1の1番目の要素はres1$coefficientsなので、

> res1[1]

としてもres1$coefficientsと同じく

$coefficients
(Intercept) x1
0.04119304 0.53730014

が返されます(わかりにくいので、とくに必要でなければ、coefficientsで呼び出した方がいいように思いますが)。


$ residualsは各データ点の残差です

> res1$residuals

1 2 3 4 5 6 7 8
-0.9654301 -0.4622868 1.2793871 -1.1197843 0.5175805 -0.6804095 0.9213302 0.9683694
9 10 11 12
-0.1623270 0.4161484 0.1927923 -0.9053703

個々のデータ点での残差を取り出したければ、名前を使って

> res1$residuals["1"]

1
-0.9654301

あるいは

> res1$residuals[["1"]]

[1] -0.9654301

もちろん、要素の番号を使ってもできます、

> res1$residuals[1]

1
-0.9654301

> res1$residuals[[1]]

[1] -0.9654301


$ effectsは、直交効果です

詳しくは、チャンバース&ヘイスティーの「Sと統計モデル」を参照してください。

> res1$effects

(Intercept) x1
-0.27305151 -1.83903118 1.46690132 -0.83118657 0.73368553 -0.52365726 1.13251221
1.26029849 0.06053935 0.60859686 0.31126114 -0.78312491

最初の2つにだけ、それぞれ(Intercept) とx1という名前が付いています

 このeffectsのx1という要素の二乗は、この説明変数(x1)で説明される平方和(回帰平方和)です。

まず、x1という要素の二乗を見ます

> res1$effects["x1"]^2

x1
3.382036

次に分散分析表を関数anova()で出させて(anova()が返す結果の詳細は別ページ)、回帰平方和を見ます。x1のSum Sqのところです

> anova(res1)

Analysis of Variance Table

Response: y1
Df Sum Sq Mean Sq F value Pr(>F)
x1 1 3.3820 3.3820 4.444 0.06124 .
Residuals 10 7.6103 0.7610
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

両者が等しいことが分かります。また、名前の付いていない項(この例では第3からあと、12番目まで)の要素の二乗の和は残差平方和に等しくなっています。名前の付いていない項(この例では3番目から12番目まで)の要素の二乗の和を見てみます

[1] 7.610341

なお、関数effects()でも、このeffectsの内容を見ることができます


$ fitted.valuesは、モデル(回帰式)による各データ点の、目的変数の予測値です

目的変数の実際の値とこの予測値の差が、res1$residualで返される残差です。最初のデータ点を例にとると、実際の値-0.6075421と予測値0.358の差が、残差の-0.965です。


$ df.residualは、残差の自由度です

単にintとあるように1つの整数がその内容です。

> res1$df.residual

[1] 10


$ callはあてはめられるモデルの式です

> res1$call

lm(formula = y1 ~ x1)


$ modelはそれ自体がデータフレームです

使われたデータは、$model$変数名で見られます

この例では、x1なら

> res1$model$x1

[1] 0.58941912 1.12998140 -0.32388348 1.47011907 0.18353773 -0.86984008 0.09616585
[8] 1.52924473 0.30353585 -0.23631159 -1.54928327 -1.48225806

y1は、

> res1$model$y1

[1] -0.60754212 0.18604543 1.14655748 -0.28869604 0.65738843 -1.10658161 1.01419318
[8] 1.83122581 0.04195594 0.33037116 -0.59844476 -1.66059471

res1$model$"x1"やres1$model$"y1"としても、も同様に見られます。

また、名前でなく要素の番号を使って、res1$model[2]やres1$model[1]でも以下のように見ることができます。

y1
1 -0.60754212
2 0.18604543
3 1.14655748
4 -0.28869604
5 0.65738843
6 -1.10658161
7 1.01419318
8 1.83122581
9 0.04195594
10 0.33037116
11 -0.59844476
12 -1.66059471

x1
1 0.58941912
2 1.12998140
3 -0.32388348
4 1.47011907
5 0.18353773
6 -0.86984008
7 0.09616585
8 1.52924473
9 0.30353585
10 -0.23631159
11 -1.54928327
12 -1.48225806


 結果のオブジェクトres1にsummary()を適用して(この場合はsummary.lm()になります)得られるオブジェクトの中味はこちらです。

 結果のオブジェクトres1にanova()を適用して(この場合はanova.lm()になります)得られるオブジェクトの中味はこちらです。

 関数aov()の結果オブジェクトの中味はこちらです。