套內數據

十五位三十到十九歲美國婦女的身高和體重.

women
   height weight
1      58    115
2      59    117
3      60    120
4      61    123
5      62    126
6      63    129
7      64    132
8      65    135
9      66    139
10     67    142
11     68    146
12     69    150
13     70    154
14     71    159
15     72    164

基本統計圖形

下面R程式碼畫出women數據集的散點圖.

plot(women, type = 'b', xlab = "Height (in)", ylab = "Weight (lb)")
grid()

線性模型分析

\[y_i = \beta_0 + \beta_1 x_i + \epsilon_i ,~~ \epsilon_i \sim N(0, \sigma^2) \]

體重 = 截距參數 + 斜率參數 x 身高 + 殘差(常態分佈)

分析概要報表

小數點4位,去掉星星.

options(digits = 4, show.signif.stars = FALSE)
summary(m0 <- lm(weight ~ height, data = women))

Call:
lm(formula = weight ~ height, data = women)

Residuals:
   Min     1Q Median     3Q    Max 
-1.733 -1.133 -0.383  0.742  3.117 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept) -87.5167     5.9369   -14.7  1.7e-09
height        3.4500     0.0911    37.9  1.1e-14

Residual standard error: 1.53 on 13 degrees of freedom
Multiple R-squared:  0.991, Adjusted R-squared:  0.99 
F-statistic: 1.43e+03 on 1 and 13 DF,  p-value: 1.09e-14

根據這份數據, 三十到十九歲美國的婦女們,平均身高多出一英寸時, 平均體重大約增加3.45英磅(誤差為0.09). 殘差估計為\(\hat{\sigma} = 1.53\).

方差分析表

anova(m0)
Analysis of Variance Table

Response: weight
          Df Sum Sq Mean Sq F value  Pr(>F)
height     1   3333    3333    1433 1.1e-14
Residuals 13     30       2                

模型擬合圖

plot(women, xlab = "Height (in)", ylab = "Weight (lb)")
abline(m0, lty = 2)
grid()

殘差圖

檢查殘差分配有沒有規律

plot(resid(m0) ~ fitted(m0), xlab = "Fitted values", 
     ylab = "Residuals", ylim = c(-3.5, 3.5))
grid()
abline(h = 0, lty = 2)

驗證殘差常態分佈

qqnorm(resid(m0))
qqline(resid(m0))
grid()

結束

顯示演練單元信息

sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Traditional)_Taiwan.950 
[2] LC_CTYPE=Chinese (Traditional)_Taiwan.950   
[3] LC_MONETARY=Chinese (Traditional)_Taiwan.950
[4] LC_NUMERIC=C                                
[5] LC_TIME=Chinese (Traditional)_Taiwan.950    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_4.1.1    magrittr_2.0.1    tools_4.1.1       htmltools_0.5.1.1
 [5] prettydoc_0.4.1   yaml_2.2.1        stringi_1.6.2     rmarkdown_2.11   
 [9] highr_0.9         knitr_1.33        stringr_1.4.0     xfun_0.24        
[13] digest_0.6.27     rlang_0.4.11      evaluate_0.14