第七回(11月27日) Task Check and Weekly Assignment

回帰分析

To Do
□ 散布図を書く(復習)
□ 単回帰分析をやってみる
□ 重回帰分析をやってみる

Assignment
□ 国語の点数を英語の点数で回帰するモデルを実行して結果をレポート
□ 国語の点数を算数,理科,社会,英語の点数で回帰する重回帰分析モデルを実行。
標準偏回帰係数に基づいて,もっとも説明力の高い変数を報告すること。
□ 英語の点数をうまく説明する回帰分析をステップワイズ法で実施し,最適モデルをレポート

いつものプロジェクトを開いているものと仮定します。
いつものサンプルがワークスペースに入っているものと仮定します。
できてないひとは,次の通りやってみよう。
※Workspaceにsampleが入っている場合=済んでいる場合です。

sample <- read.csv("sample(mac).csv", na.strings = "*")
sample$sex <- factor(sample$sex, labels = c("male", "female"))

まずは,身長と体重の回帰分析から。
この二つの変数の散布図を書いてみましょう。

plot(sample$height, sample$weight)

plot of chunk unnamed-chunk-2

右肩あがりの関係がありそうです。

そこで回帰分析。

reg <- lm(weight ~ height, data = sample)
reg
## 
## Call:
## lm(formula = weight ~ height, data = sample)
## 
## Coefficients:
## (Intercept)       height  
##      -62.70         0.79

この線を書き足すと,

abline(reg, col = "red")
## Error: まだ plot.new が呼ばれていません

回帰分析のレポートには,当てはまりのよさ(適合度)を入れる必要がある。
当てはまりの良さは重相関係数の二乗(Multiple R-squared)。

summary(reg)
## 
## Call:
## lm(formula = weight ~ height, data = sample)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -19.074  -4.574   0.814   4.685  16.083 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -62.701     11.222   -5.59  2.1e-07 ***
## height         0.790      0.074   10.67  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
## 
## Residual standard error: 6.67 on 98 degrees of freedom
## Multiple R-squared: 0.537,   Adjusted R-squared: 0.533 
## F-statistic:  114 on 1 and 98 DF,  p-value: <2e-16

説明する変数が増えたら重回帰分析。
モデルの書き方は次の通り。

reg2 <- lm(kokugo ~ sansuu + eigo, data = sample)
reg2
## 
## Call:
## lm(formula = kokugo ~ sansuu + eigo, data = sample)
## 
## Coefficients:
## (Intercept)       sansuu         eigo  
##     22.0649      -0.0789       0.8036
summary(reg2)
## 
## Call:
## lm(formula = kokugo ~ sansuu + eigo, data = sample)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -10.167  -3.533   0.802   3.664   8.564 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  22.0649     6.7036    3.29   0.0014 ** 
## sansuu       -0.0789     0.0872   -0.91   0.3675    
## eigo          0.8036     0.0340   23.64   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
## 
## Residual standard error: 5 on 95 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared: 0.856,   Adjusted R-squared: 0.853 
## F-statistic:  282 on 2 and 95 DF,  p-value: <2e-16

しかしこれでは,もし単位が違ったら,大きさを過大・過小評価してしまう。
そこで,データの標準化を考える。

head(sample)
##   ID class  sex height weight kokugo sansuu rika syakai eigo
## 1  1     A male  170.2  64.16     34     74   43     20   28
## 2  2     B male  165.7  75.63     82     63   44     54   72
## 3  3     C male  157.8  62.64     50     74   55     26   44
## 4  4     A male  161.6  69.57     57     75   55     46   44
## 5  5     B male  161.1  60.23     74     73   54     41   65
## 6  6     C male  156.2  54.99     NA     58   38     47   58
sample.z <- scale(sample[4:10])
sample.z <- data.frame(sample.z)
head(sample.z)
##   height  weight  kokugo  sansuu    rika  syakai    eigo
## 1 2.0793  0.7503 -2.3432  0.4133 -1.3310 -2.2584 -2.1329
## 2 1.5834  1.9263  1.3463 -1.4314 -1.1529  0.3498  0.8109
## 3 0.7120  0.5944 -1.1134  0.4133  0.8058 -1.7982 -1.0625
## 4 1.1272  1.3050 -0.5753  0.5810  0.8058 -0.2639 -1.0625
## 5 1.0764  0.3473  0.7314  0.2456  0.6277 -0.6475  0.3426
## 6 0.5319 -0.1900      NA -2.2699 -2.2214 -0.1872 -0.1258
summary(sample.z)
##      height           weight            kokugo            sansuu       
##  Min.   :-2.151   Min.   :-2.4191   Min.   :-2.3432   Min.   :-2.2699  
##  1st Qu.:-0.661   1st Qu.:-0.6414   1st Qu.:-0.7291   1st Qu.:-0.5929  
##  Median :-0.154   Median :-0.0823   Median :-0.0373   Median : 0.0779  
##  Mean   : 0.000   Mean   : 0.0000   Mean   : 0.0000   Mean   : 0.0000  
##  3rd Qu.: 0.630   3rd Qu.: 0.6372   3rd Qu.: 0.7314   3rd Qu.: 0.6649  
##  Max.   : 2.308   Max.   : 3.0891   Max.   : 2.2687   Max.   : 2.4257  
##                                     NA's   :1         NA's   :1        
##       rika             syakai            eigo        
##  Min.   :-2.9336   Min.   :-2.258   Min.   :-2.3336  
##  1st Qu.:-0.7078   1st Qu.:-0.667   1st Qu.:-0.7279  
##  Median : 0.0935   Median :-0.111   Median : 0.0749  
##  Mean   : 0.0000   Mean   : 0.000   Mean   : 0.0000  
##  3rd Qu.: 0.6277   3rd Qu.: 0.599   3rd Qu.: 0.7440  
##  Max.   : 2.7645   Max.   : 2.805   Max.   : 2.2828  
##  NA's   :1

ということで,あらためて。

reg3 <- lm(kokugo ~ sansuu + eigo, data = sample.z)
reg3
## 
## Call:
## lm(formula = kokugo ~ sansuu + eigo, data = sample.z)
## 
## Coefficients:
## (Intercept)       sansuu         eigo  
##     0.00409     -0.03618      0.92324
summary(reg3)
## 
## Call:
## lm(formula = kokugo ~ sansuu + eigo, data = sample.z)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -0.7815 -0.2715  0.0617  0.2817  0.6583 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.00409    0.03882    0.11     0.92    
## sansuu      -0.03618    0.03996   -0.91     0.37    
## eigo         0.92324    0.03905   23.64   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
## 
## Residual standard error: 0.384 on 95 degrees of freedom
##   (2 observations deleted due to missingness)
## Multiple R-squared: 0.856,   Adjusted R-squared: 0.853 
## F-statistic:  282 on 2 and 95 DF,  p-value: <2e-16

最後に変数選択。

reg.all <- lm(eigo ~ ., data = na.omit(sample.z))
reg.step <- step(reg.all)
## Start:  AIC=-178.9
## eigo ~ height + weight + kokugo + sansuu + rika + syakai
## 
##          Df Sum of Sq  RSS    AIC
## - syakai  1       0.0 13.3 -180.8
## - height  1       0.0 13.3 -180.7
## - weight  1       0.0 13.3 -180.6
## - sansuu  1       0.1 13.3 -180.5
## <none>                13.3 -178.9
## - rika    1       0.4 13.7 -178.0
## - kokugo  1      44.5 57.8  -38.2
## 
## Step:  AIC=-180.8
## eigo ~ height + weight + kokugo + sansuu + rika
## 
##          Df Sum of Sq  RSS    AIC
## - height  1       0.0 13.3 -182.6
## - sansuu  1       0.0 13.3 -182.5
## - weight  1       0.0 13.3 -182.5
## <none>                13.3 -180.8
## - rika    1       0.4 13.7 -180.0
## - kokugo  1      82.7 95.9    8.9
## 
## Step:  AIC=-182.6
## eigo ~ weight + kokugo + sansuu + rika
## 
##          Df Sum of Sq  RSS    AIC
## - weight  1       0.0 13.3 -184.5
## - sansuu  1       0.0 13.4 -184.4
## <none>                13.3 -182.6
## - rika    1       0.4 13.7 -182.0
## - kokugo  1      82.6 95.9    6.9
## 
## Step:  AIC=-184.5
## eigo ~ kokugo + sansuu + rika
## 
##          Df Sum of Sq  RSS  AIC
## - sansuu  1       0.0 13.4 -186
## <none>                13.3 -184
## - rika    1       0.4 13.7 -184
## - kokugo  1      82.7 96.0    5
## 
## Step:  AIC=-186.2
## eigo ~ kokugo + rika
## 
##          Df Sum of Sq  RSS  AIC
## <none>                13.4 -186
## - rika    1       0.4 13.8 -185
## - kokugo  1      82.7 96.0    3