#oneway Anova & 獨立樣本t檢定 & 卡方檢定
load("StudentsPerformance.Rdata")
str(sp)
## 'data.frame': 1000 obs. of 8 variables:
## $ gender : Factor w/ 2 levels "female","male": 1 1 1 2 2 1 1 2 2 1 ...
## $ race.ethnicity : Factor w/ 5 levels "group A","group B",..: 2 3 2 1 3 2 2 2 4 2 ...
## $ parental.level.of.education: Factor w/ 6 levels "associate's degree",..: 2 5 4 1 5 1 5 5 3 3 ...
## $ lunch : Factor w/ 2 levels "free/reduced",..: 2 2 2 1 2 2 2 1 1 1 ...
## $ test.preparation.course : Factor w/ 2 levels "completed","none": 2 1 2 2 2 2 1 2 1 2 ...
## $ math.score : int 72 69 90 47 76 71 88 40 64 38 ...
## $ reading.score : int 72 90 95 57 78 83 95 43 64 60 ...
## $ writing.score : int 74 88 93 44 75 78 92 39 67 50 ...
#欄位名稱------------------------------------------
# gender 性別
# race.ethnicity 種族分群
# parental.level.of.education 父母教育程度
# lunch 營養午餐類型(free/reduced免費或減免餐費,standard為一般類別)
# test.preparation.course
# math.score 數學成績
# reading.score 閱讀成績
# writing.score 寫作成績
#題目
#一、父母教育程度是否會顯著影響學生的數學成績?
#二、不同的午餐類型是否會顯著影響學生的數學成績?
#三、性別是否會影響營養午餐類型?
#解答
#一、父母教育程度是否會顯著影響學生的數學成績?
## 撰寫分析步驟
#Step1:計算(分組)樣本平均數或建立交叉表
library(dplyr)
##
## 載入套件:'dplyr'
## 下列物件被遮斷自 'package:stats':
##
## filter, lag
## 下列物件被遮斷自 'package:base':
##
## intersect, setdiff, setequal, union
sp %>%
group_by(parental.level.of.education) %>%
summarise(人數= n(), 平均數學成績 = mean(math.score), 標準差 = sd(math.score))
## # A tibble: 6 × 4
## parental.level.of.education 人數 平均數學成績 標準差
## <fct> <int> <dbl> <dbl>
## 1 associate's degree 222 67.9 15.1
## 2 bachelor's degree 118 69.4 14.9
## 3 high school 196 62.1 14.5
## 4 master's degree 59 69.7 15.2
## 5 some college 226 67.1 14.3
## 6 some high school 179 63.5 15.9
# Answer
# A tibble: 6 × 4
# parental.level.of.education 人數 平均數學成績 標準差
# <fct> <int> <dbl> <dbl>
# 1 associate's degree 222 67.9 15.1
# 2 bachelor's degree 118 69.4 14.9
# 3 high school 196 62.1 14.5
# 4 master's degree 59 69.7 15.2
# 5 some college 226 67.1 14.3
# 6 some high school 179 63.5 15.9
#Step2:決定顯著水準( 0.1, 0.05, 0.01, 0.001)
#p<0.05
#Step3:寫出虛無假設與對立假設
#H0:不同父母教育程度的學生數學成績沒有顯著差異。
#H1:不同父母教育程度的學生數學成績有顯著差異。
#Step4:決定檢定方法
#單因子變異數分析one-way ANOVA
#Step5:計算檢定值。
oneway<-aov(sp$math.score~sp$parental.level.of.education)
summary(oneway)
## Df Sum Sq Mean Sq F value Pr(>F)
## sp$parental.level.of.education 5 7296 1459.1 6.522 5.59e-06 ***
## Residuals 994 222394 223.7
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#F = 6.522
# Step6:計算p值
# p=5.59e-06
# Step7:下結論
# 因為p=5.59e-06, p<0.05,所以拒絕虛無假設,對立假設成立。
# 結論是:不同父母教育程度的學生數學成績有顯著差異。
#二、不同的午餐類型是否會顯著影響學生的數學成績?
## 撰寫分析步驟
#Step1:計算(分組)樣本平均數或建立交叉表
library(dplyr)
sp %>%
group_by(lunch) %>% #依教育程度分組
summarise(人數= n(),數學平均成績= mean(math.score), 標準差 = sd(math.score))#計算分組後的人數與平均均數學成績
## # A tibble: 2 × 4
## lunch 人數 數學平均成績 標準差
## <fct> <int> <dbl> <dbl>
## 1 free/reduced 355 58.9 15.2
## 2 standard 645 70.0 13.7
# A tibble: 2 × 4
# lunch 人數 數學平均成績 標準差
# <fct> <int> <dbl> <dbl>
# 1 free/reduced 355 58.9 15.2
# 2 standard 645 70.0 13.7
#free/reduced為58.9分,standard為70.0分
#Step2:決定顯著水準( 0.1, 0.05, 0.01, 0.001)
#p<0.05
#Step3:寫出虛無假設與對立假設
#H0:不同的午餐類型的學生數學成績沒有顯著差異。
#H1:不同的午餐類型的學生數學成績有顯著差異。
#Step4:決定檢定方法
#獨立樣本T檢定
#Step5:計算檢定值。
#5-1檢定兩組樣本變異數是否相等
#var.test(依變數~自變數, data = )
var.test(math.score ~ lunch, data = sp)
##
## F test to compare two variances
##
## data: math.score by lunch
## F = 1.2328, num df = 354, denom df = 644, p-value = 0.02346
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
## 1.028569 1.485040
## sample estimates:
## ratio of variances
## 1.232843
#結果為變異數不相等
# F = 1.2328, num df = 354, denom df = 644, p-value = 0.02346
# alternative hypothesis: true ratio of variances is not equal to 1
#5-2再做獨立樣本T檢定(var.equal = FALSE)
t.test(math.score ~ lunch, data = sp, var.equal = FALSE)
##
## Welch Two Sample t-test
##
## data: math.score by lunch
## t = -11.484, df = 667.5, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group free/reduced and group standard is not equal to 0
## 95 percent confidence interval:
## -13.01305 -9.21291
## sample estimates:
## mean in group free/reduced mean in group standard
## 58.92113 70.03411
#t = -11.484
# Step6:計算p值
# p=2.2e-16
# Step7:下結論
# 因為p=2.2e-16, p<0.05,所以拒絕虛無假設,對立假設成立。
# 結論是:不同的營養午餐類型的學生數學成績有顯著差異。
#三、性別是否會影響營養午餐類型?
#先畫圖瞭解資料
count <- table(sp$lunch, sp$gender)
count
##
## female male
## free/reduced 189 166
## standard 329 316
barplot(count)

## 撰寫分析步驟
#Step1:計算(分組)樣本平均數或建立交叉表
table(sp$lunch, sp$gender)
##
## female male
## free/reduced 189 166
## standard 329 316
# female male
# free/reduced 189 166
# standard 329 316
# Step2:決定顯著水準( 0.1, 0.05, 0.01, 0.001)
# p<0.05
# Step3:寫出虛無假設與對立假設
# H0:不同性別的營養午餐類型沒有顯著差異。
# H1:不同性別的營養午餐類型有顯著差異。
# Step4:決定檢定方法
# 卡方檢定
# Step5:計算檢定值。
chisq.test(sp$lunch, sp$gender)
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: sp$lunch and sp$gender
## X-squared = 0.37174, df = 1, p-value = 0.5421
# x squared=0.37174
# Step6:計算p值
# p=0.5421
# Step7:下結論
# 因為p=0.5421, p>0.05,所以虛無假設成立,拒絕對立假設。
# 結論是:不同性別的營養午餐類型沒有顯著差異