

두 표본의 분포는 분산이 같은 경우와 분산이 같지 않은 경우를 나누어
생각

가설수립

# data 입력
sampleA <- c(2.1, 5.3, 1.4, 4.6, 0.9)
sampleB <- c(1.9, 0.5, 2.8, 3.1)
# 등분산 여부를 알기 위한 F검정
var.test(sampleA, sampleB)
F test to compare two variances
data: sampleA and sampleB
F = 2.8499, num df = 4, denom df = 3, p-value = 0.416
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.1887234 28.4398003
sample estimates:
ratio of variances
2.849908
# 등분산일 경우의 t-test
t.test(sampleA, sampleB, paired=F, var.equal=T)
Two Sample t-test
data: sampleA and sampleB
t = 0.69899, df = 7, p-value = 0.5071
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-1.870604 3.440604
sample estimates:
mean of x mean of y
2.860 2.075
다음 자료는 남학생과 여학생의 신장을 측정한 자료이다 이를 기초로 남녀간에 신장에 사이가 있는지 검정하라

# sleep data
?sleep
str(sleep)
head(sleep)
# 등분산검정
# t-test 검정

• t검정에서는 직접적으로 두 집단에 대한 사이를 비교









# data 입력
score01 <- c(80,82,80,82,77,85,83,81,83) #1학년 성적
score02 <- c(76,77,78,79,78,77,76,78,79) #2학년 성적
score03 <- c(65,67,66,63,62,67,61,65,64) #3학년 성적
scoreAll <- c(score01,score02,score03)
grade <- c(rep(1,9),rep(2,9),rep(3,9)) # 학년별로 각각 9 번 반복
# grade는 현재 수지 data의 요인으로 변경
grade <- factor(grade)
# 학년과 성적으로 table 생성
sampleData <- data.frame(scoreAll,grade)
sampleData
# 정규성 검정을 위해 package 설치 및 불러오는 과정
install.packages(“nortest”)
library(nortest)
# Shapiro-Wilk normality test를 이용한 정규성 검정
tapply(scoreAll,grade,shapiro.test)
tapply(scoreAll,grade,shapiro.test)
$`1`
Shapiro-Wilk normality tes t
da ta: X[[i]]
W = 0.96156, p -value = 0.8145
$`2`
Shapiro-Wilk normality tes t
da ta: X[[i]]
W = 0.89939, p -value = 0.2485
$`3`
Shapiro-Wilk normality tes t
da ta: X[[i]]
W = 0.94253, p -value = 0.6088
# Shapiro-Wilk 정규성 검정 결과
1 학년의 p-value= 0.8145; 2 학년의 p-value=0.2485; 3학년의 p-value=0.6088
→ 유의수준 0.05수준에서 귀무가설을 채택
→ 학년별 성적은 정규성을 만족한다.
# 등분산 검정을 위해 library 설치 및 불러오는 과정
install.packages("car")
library(car)
# Levene's Test를 이용하여 등분산 검정
leveneTest(scoreAll ~ grade,data=sampleData)
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr( >F)
group 2 1.12 0.3427
24
# Levene 검정 결과: p-value=0.3427이므로 유의수준 0.05에서 귀무가설
을채택 → 학년별 분산은 서로 같다고 볼 수 있다.
# 학년별 평균
aggregate(scoreAll~grade, sampleData, mean)
aggregate(list(meanScore = sampleData$scoreAll),
list(grade=sampleData$grade),mean)
grade meanScore
1 1 81.44444
2 2 77.55556
3 3 64.44444
# 학년별 표준편차
aggregate(scoreAll~grade, sampleData, sd)
aggregate(list(stdScore = sampleData$scoreAll),
list(grade=sampleData$grade),sd)
grade stdScore
1 1 2.297341
2 2 1.130388
3 3 2.127858
# 분산분석
result <- aov(scoreAll~grade, data= sampleData )
summary(result)
Df Sum Sq Mean Sq F value Pr( >F)
grade 2 1428.1 714.0 193.3 1.59e-15 ***
Residuals 24 88.7 3.7
---
Signif. codes : 0 ‘*** ’ 0.001 ‘**’ 0.01 ‘* ’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 사후비교
TukeyHSD(result)
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov( formula = scoreAll ~ grade, da ta = sampleData)
$grade
diff lwr upr p adj
2-1 -3.888889 -6.15164 -1.626138 0.0007102
3-1 -17.000000 -19.26275 -14.737249 0.0000000
3-2 -13.111111 -15.37386 -10.848360 0.0000000
