Aritalab:Lecture/Math/LR/R LM

From Metabolomics.JP
< Aritalab:Lecture | Math | LR
Revision as of 09:36, 4 June 2013 by Adm (Talk | contribs)

Jump to: navigation, search
参考サイト

Rによる回帰分析

R では lm という関数で回帰分析ができます。Rに組み込んである airquality というデータをみてみます。

> help("iris")

アンダーソンによる菖蒲のデータです。

> require(graphics)
> pairs(iris[1:4], main = "Iris Data", pch=21, bg = c("red", "green","blue")[unclass(iris$Species)])

Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species の 5 変数があり総当りの相関が出ています。 この中で Petal(花弁)の Length と Width はとても相関が高いことを確認しましょう。Sepal(萼片)の長さはどの変数と関連するのでしょうか。重回帰分析をおこないます。

> s <- lm (Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width , data=iris)
> summary(s)


Call:
lm(formula = Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, 
    data = iris)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.82816 -0.21989  0.01875  0.19709  0.84570 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)   1.85600    0.25078   7.401 9.85e-12 ***
Sepal.Width   0.65084    0.06665   9.765  < 2e-16 ***
Petal.Length  0.70913    0.05672  12.502  < 2e-16 ***
Petal.Width  -0.55648    0.12755  -4.363 2.41e-05 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.3145 on 146 degrees of freedom
Multiple R-squared: 0.8586,     Adjusted R-squared: 0.8557 
F-statistic: 295.5 on 3 and 146 DF,  p-value: < 2.2e-16 

目的変量が Sepal.Length, 説明変量が Sepal.Width, Petal.Length, Petal.Width です。各変量の重みは summary の Estimate として表示されています。Significance code は帰無仮説に対する有意差を示しています。Sepal.Width, Petal.Length が大きく関係することがわかります。 しかし Petal.Length と Petal.Width は相関が高かったのではないでしょうか。

> cor(iris$Petal.Length, iris$Petal.Width)
[1] 0.9628654

これほど相関するのに重回帰分析の Estimate は Length と Width で逆になっています。これは問題です。 Length と Width はほぼ同じ意味を示すのに Estimate が逆になるからです。実際、Petal.Width を説明変数から外してもほとんど同じ精度を得ることができます。

変数選択は step 関数でも行えます。 AIC (Akaike Information Criterion) を指標に変数を除去するはずですが、Petal.Width は残るようです。

> s.step1 <- step(s)
Start:  AIC=-343.04
Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width

               Df Sum of Sq    RSS     AIC
<none>                      14.445 -343.04
- Petal.Width   1    1.8834 16.329 -326.66
- Sepal.Width   1    9.4353 23.881 -269.63
- Petal.Length  1   15.4657 29.911 -235.86
Personal tools
Namespaces

Variants
Actions
Navigation
metabolites
Toolbox