dta <- read.csv("https://stats.idre.ucla.edu/stat/data/binary.csv")
dta <- dta[, c("gpa", "gre")]400位學生GPA和GRE的前六筆資料展示
head(dta) gpa gre
1 3.61 380
2 3.67 660
3 4.00 800
4 3.19 640
5 2.93 520
6 3.00 760
400位學生GRE(X軸)與GPA(Y軸)分數的散佈圖
plot(dta, type = "p", xlab = "GRE Score", ylab = "GPA Score",col="black")
grid()\[y_i = \beta_0 + \beta_1 x_i + \epsilon_i ,~~ \epsilon_i \sim N(0, \sigma^2) \] GPA Score=截距參數+斜率參數×GRE Score+殘差(便假設為常態分配)
小數點4位,去掉星星.
options(digits = 4, show.signif.stars = FALSE)
summary(m0 <- lm(gpa~gre, data =dta))
Call:
lm(formula = gpa ~ gre, data = dta)
Residuals:
Min 1Q Median 3Q Max
-1.0867 -0.2244 -0.0002 0.2481 0.7618
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2.645898 0.091310 29.0 < 2e-16
gre 0.001266 0.000152 8.3 1.6e-15
Residual standard error: 0.352 on 398 degrees of freedom
Multiple R-squared: 0.148, Adjusted R-squared: 0.146
F-statistic: 68.9 on 1 and 398 DF, p-value: 1.6e-15
根據這份數據, GPA分數每增加1分時,GRE會增加0.0012分(標準誤為0.0001)。 殘差標準誤\((\widehat{\sigma})\)為0.3520
anova(m0)Analysis of Variance Table
Response: gpa
Df Sum Sq Mean Sq F value Pr(>F)
gre 1 8.5 8.53 69 1.6e-15
Residuals 398 49.3 0.12
plot(dta,xlab = "GRE Score", ylab = "GPA Score")
abline(m0,lty=2)
grid()plot(resid(m0) ~ fitted(m0), xlab = "Fitted values",
ylab = "Residuals", ylim = c(-2, 2))
grid()
abline(h = 0, lty = 2) ##驗證殘差常態分佈
qqnorm(resid(m0))
qqline(resid(m0))
grid()顯示演練單元信息
sessionInfo()R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.3
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
locale:
[1] zh_TW.UTF-8/zh_TW.UTF-8/zh_TW.UTF-8/C/zh_TW.UTF-8/zh_TW.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.4.3 backports_1.1.2 magrittr_1.5 rprojroot_1.3-2
[5] tools_3.4.3 htmltools_0.3.6 yaml_2.1.17 Rcpp_0.12.15
[9] stringi_1.1.6 rmarkdown_1.9 knitr_1.20 stringr_1.3.0
[13] digest_0.6.15 evaluate_0.10.1