library(tidyverse)
library(car)
library(faraway)
data(hsb)

看看資料結構

str(hsb)
## 'data.frame':    200 obs. of  11 variables:
##  $ id     : int  70 121 86 141 172 113 50 11 84 48 ...
##  $ gender : Factor w/ 2 levels "female","male": 2 1 2 2 2 2 2 2 2 2 ...
##  $ race   : Factor w/ 4 levels "african-amer",..: 4 4 4 4 4 4 1 3 4 1 ...
##  $ ses    : Factor w/ 3 levels "high","low","middle": 2 3 1 1 3 3 3 3 3 3 ...
##  $ schtyp : Factor w/ 2 levels "private","public": 2 2 2 2 2 2 2 2 2 2 ...
##  $ prog   : Factor w/ 3 levels "academic","general",..: 2 3 2 3 1 1 2 1 2 1 ...
##  $ read   : int  57 68 44 63 47 44 50 34 63 57 ...
##  $ write  : int  52 59 33 44 52 52 59 46 57 55 ...
##  $ math   : int  41 53 54 47 57 51 42 45 54 52 ...
##  $ science: int  47 63 58 53 53 63 53 39 58 50 ...
##  $ socst  : int  57 61 31 56 61 61 61 36 51 51 ...
head(hsb)
##    id gender  race    ses schtyp     prog read write math science socst
## 1  70   male white    low public  general   57    52   41      47    57
## 2 121 female white middle public vocation   68    59   53      63    61
## 3  86   male white   high public  general   44    33   54      58    31
## 4 141   male white   high public vocation   63    44   47      53    56
## 5 172   male white middle public academic   47    52   57      53    61
## 6 113   male white middle public academic   44    52   51      63    61

現在想要知道,性別與閱讀能力對寫作能力的預測力。

因為性別為類別變項,所以不需做相關檢定。

#看一下閱讀能力和寫作能力在性別上的差異:

ggplot(aes(y = read, x = write, color = gender), data = hsb) +
  geom_point() +
  geom_smooth(method = lm, se = F) + 
  theme_bw()
## `geom_smooth()` using formula 'y ~ x'

從上方的圖表可以看出,閱讀能力和寫作能力在性別上皆呈現正相關,斜率差不多。

#開始跑線性回歸模型,看性別與閱讀能力在寫作能力上的預測力:

rwg <- lm(write ~ gender + read, data = hsb)
summary(rwg)
## 
## Call:
## lm(formula = write ~ gender + read, data = hsb)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -17.523  -5.658   0.168   5.043  15.175 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 25.71526    2.64464   9.724  < 2e-16 ***
## gendermale  -5.48689    1.01426  -5.410 1.82e-07 ***
## read         0.56589    0.04938  11.459  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7.133 on 197 degrees of freedom
## Multiple R-squared:  0.4394, Adjusted R-squared:  0.4337 
## F-statistic: 77.21 on 2 and 197 DF,  p-value: < 2.2e-16

yi=β0+β1x1i+β2x2i+εi

25.72-5.49+0.57+7.13=27.93

從上述的結果來看,可以看出性別和閱讀能力對寫作能力的影響達到非常顯著,也就是說,閱讀能力越好,寫作能力也越佳;女生的寫作能力較男生的寫作能力優異。使用性別和閱讀能力能夠解釋約43%的寫作能力。

#檢測資料是否為常態

直方圖可以直觀的看出資料分布情況:

hist( x = residuals(rwg),
      xlab = "Value of residual",
      main = "",
      breaks = 20)

從上方圖表看出,資料分布接近常態(0的數量越多越好),但依舊有一些現象無法被此模型解釋。

Normal Q-Q

plot(rwg, which = 2)

從上方圖表可以看出,殘差的點所組成的線段接近直線,所以此筆資料的殘差接近常態分佈。

#觀察線性關係

plot(rwg, which  = 1)

residualPlots(rwg)

##            Test stat Pr(>|Test stat|)  
## gender                                 
## read         -1.1356          0.25752  
## Tukey test   -1.7249          0.08455 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

從上方兩個圖表(殘差與期望值比較)來看,可以看出殘差的分布很分散,雖然最後出來的線接近水平,但依舊有模型無法解釋的資料。但從下方的事後檢定來看並無達到顯著,無法虛無拒絕(殘差為常態分佈),所以殘差可能為常態分佈。

#檢測標準化殘差和期望值的同質性

plot(rwg, which = 3)

雖然同質性的線並未有一個趨勢,但從圖形上來看,標準化殘差與期望值的分布點很分散,表示可能殘差可能為常態。