#載入資料
data("seeds")
#資料的前六筆、結構
head(seeds)
##   germ moisture covered
## 1   22        1      no
## 2   41        3      no
## 3   66        5      no
## 4   82        7      no
## 5   79        9      no
## 6    0       11      no
str(seeds)
## 'data.frame':    48 obs. of  3 variables:
##  $ germ    : num  22 41 66 82 79 0 25 46 72 73 ...
##  $ moisture: num  1 3 5 7 9 11 1 3 5 7 ...
##  $ covered : Factor w/ 2 levels "no","yes": 1 1 1 1 1 1 1 1 1 1 ...

Multiple Regression Analysis

#畫圖
ggplot(aes(y = germ, x = moisture, color = covered), data = seeds) +
  geom_point() +
  geom_smooth(method = lm, se = F) + 
  theme_bw()
## `geom_smooth()` using formula 'y ~ x'
## Warning: Removed 1 rows containing non-finite values (stat_smooth).
## Warning: Removed 1 rows containing missing values (geom_point).

#multiple linear regression
mod <- lm(germ ~ moisture + covered, data = seeds)
summary(mod)
## 
## Call:
## lm(formula = germ ~ moisture + covered, data = seeds)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -39.526 -26.798   2.901  24.275  39.182 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  64.2390     8.8400   7.267 4.65e-09 ***
## moisture     -2.7134     1.1514  -2.357    0.023 *  
## coveredyes   -0.6601     7.8853  -0.084    0.934    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 27.02 on 44 degrees of freedom
##   (因為不存在,1 個觀察量被刪除了)
## Multiple R-squared:  0.1121, Adjusted R-squared:  0.07174 
## F-statistic: 2.777 on 2 and 44 DF,  p-value: 0.07312

關於回歸模型的解釋

因為這組資料中有一筆資料是N/A,但R很聰明地把它刪掉了。

64.24 - 2.71moisture - 0.66coveredyes + 27.02

R-square 0.0717,代表回歸模型佔結果測量變異性的7.17%。

所以,此回歸模型不顯著,不具有預測能力。

Diagnostic

確認殘差值,越接近0,實際情形和預測得差不多。

也就是圖應該要呈現常態分布。

#normality of the residuals
hist( x = residuals(mod),
      xlab = "Value of residual",
      main = "",
      breaks = 20)

在QQplot中,當點點在線上或越靠近線時,代表很好。

主要是檢查是否有normally distributed。

#根據模型畫圖(QQplot)
plot(mod, which = 2)

沒有明顯的圖案的水平線表示線性圖形,代表是好的。

也就是越接近中間那條水平線代表越好。

#plot of "Residuals vs. Fitted"
plot(mod, which  = 1)

#Advance version
residualPlots(mod)

##            Test stat Pr(>|Test stat|)    
## moisture     -10.632        1.302e-13 ***
## covered                                  
## Tukey test   -11.735        < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Checking the homogeneity of variance Scale-Location

#Plot of the fitted values (model predictions) against the square root of the abs standardised residuals.
plot(mod, which = 3)

結論

從圖形來看,實際情形和理想情形存在一大段差異,像是QQplot的圖中,它的點沒有在線上,而且離得還滿遠的;又像是它的殘差值並沒有很接近0,圖形也幾乎呈現拋物線,這說明了實際與預期有落差。

於是,在考量了其他變項之後,moisture似乎無法可以準確地預測germ的生長情況。