15位30-19歲美國婦女的身高和體重
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{} = 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 19041)
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 fastmap_1.1.0 tools_4.1.1
[5] htmltools_0.5.2 yaml_2.2.1 jquerylib_0.1.4 stringi_1.7.5
[9] rmarkdown_2.11 highr_0.9 knitr_1.36 stringr_1.4.0
[13] xfun_0.26 digest_0.6.28 rlang_0.4.11 evaluate_0.14