Aritalab:Lecture/Math/LR/R LM

From Metabolomics.JP
< Aritalab:Lecture | Math | LR(Difference between revisions)
Jump to: navigation, search
m (Rによる回帰分析)
(Rによる回帰分析)
 
Line 15: Line 15:
 
> pairs(iris[1:4], main = "Iris Data", pch=21, bg = c("red", "green","blue")[unclass(iris$Species)])
 
> pairs(iris[1:4], main = "Iris Data", pch=21, bg = c("red", "green","blue")[unclass(iris$Species)])
 
</pre>
 
</pre>
Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species の 5 変数があり総当りの相関が出ています。
+
萼片の長さと幅(Sepal.Length, Sepal.Width)、花びらの長さと幅(Petal.Length, Petal.Width)について、3種(Species) のデータについて総当りの相関が出ています。この中で Petal(花弁)の Length と Width はとても相関が高いことを確認しましょう。
この中で Petal(花弁)の Length と Width はとても相関が高いことを確認しましょう。Sepal(萼片)の長さはどの変数と関連するのでしょうか。重回帰分析をおこないます。
+
 
 +
では、Sepal(萼片)の長さはどの変数と関連するのでしょうか。ここで重回帰分析をおこないます。
 
<pre>
 
<pre>
 
> s <- lm (Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width , data=iris)
 
> s <- lm (Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width , data=iris)
Line 43: Line 44:
 
F-statistic: 295.5 on 3 and 146 DF,  p-value: < 2.2e-16  
 
F-statistic: 295.5 on 3 and 146 DF,  p-value: < 2.2e-16  
 
</pre>
 
</pre>
目的変量が Sepal.Length, 説明変量が Sepal.Width, Petal.Length, Petal.Width です。各変量の重みは summary の Estimate として表示されています。Significance code は帰無仮説に対する有意差を示しています。Sepal.Width, Petal.Length が大きく関係することがわかります。
+
目的変量が Sepal.Length です。説明変量として Sepal.Width, Petal.Length, Petal.Width を使います。各変量の重みは summary の Estimate として表示されています。Significance code は帰無仮説に対する有意差を示しています。
しかし Petal.Length と Petal.Width は相関が高かったのではないでしょうか。
+
 
 +
上の結果から、全てのパラメータが有意に関与することがわかります。そして Petal.Width だけがマイナスの影響になっています。
 +
しかし Petal.Length と Petal.Width は相関が高かったのではないでしょうか。なぜ両者は正と負の値になっているのでしょう。
 +
 
 
<pre>
 
<pre>
 
> cor(iris$Petal.Length, iris$Petal.Width)
 
> cor(iris$Petal.Length, iris$Petal.Width)
 
[1] 0.9628654
 
[1] 0.9628654
 
</pre>
 
</pre>
これほど相関するのに重回帰分析の Estimate は Length と Width で逆になっています。これは問題です。
+
 
Length と Width はほぼ同じ意味を示すのに Estimate が逆になるからです。実際、Petal.Width を説明変数から外してもほとんど同じ精度を得ることができます。
+
これこそ重回帰分析の弱点で、共線性と呼ばれる問題です。
 +
実際、Petal.Width を説明変数から外してもほとんど同じ精度を得ることができます。
 +
 
  
 
変数選択は step 関数でも行えます。 AIC (Akaike Information Criterion) を指標に変数を除去するはずですが、Petal.Width は残るようです。
 
変数選択は step 関数でも行えます。 AIC (Akaike Information Criterion) を指標に変数を除去するはずですが、Petal.Width は残るようです。

Latest revision as of 11:37, 15 July 2015

参考サイト

[edit] Rによる回帰分析

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

> help("iris")

helpをみるとアンダーソンが用いたアヤメのデータ(50ずつ3種)であることがわかります。 アヤメ3種 I. setosa, versicolor, virginica についての萼片と花びらの長さと幅が記録してあります。 原論文では setosa が他 2 種と形態が異なること、versicolor は setosa と virginica の雑種かもしれないことが示唆されています。

> 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)について、3種(Species) のデータについて総当りの相関が出ています。この中で 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 は帰無仮説に対する有意差を示しています。

上の結果から、全てのパラメータが有意に関与することがわかります。そして Petal.Width だけがマイナスの影響になっています。 しかし Petal.Length と Petal.Width は相関が高かったのではないでしょうか。なぜ両者は正と負の値になっているのでしょう。

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

これこそ重回帰分析の弱点で、共線性と呼ばれる問題です。 実際、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