8.1 두 군의 평균 비교
X1,X2,⋯,Xn1i.i.d∼N(μ1,σ21) 이고,
Y1,Y2,⋯,Yn2i.i.d∼N(μ2,σ22) 이고,
Xi 와 Yi 간에도 서로 독립일 때,
ˉX=∑n1i=1Xin1
S21=∑n1i=1(Xi−ˉX)2n1−1
ˉY=∑n2i=1Yin2
S22=∑n1i=1(Yi−ˉY)2n2−1 라 하면,
E(ˉX−ˉY)=μ1−μ2
V(ˉX−ˉY)=V(ˉX)+V(ˉY)=σ21n1+σ22n2 이다.
Z=(ˉX−ˉY)−(μ1−μ2)√σ21/n1+σ22/n2∼N(0,12)
따라서, μ1−μ2에 대한 100(1−α) 신뢰구간은 다음과 같다.
(ˉX−ˉY)±zα/2√σ21/n1+σ22/n2
또한, 차이가 δ0라는 귀무가설에 대한 검정통계량 Z의 식은 다음과 같다.
Z=(ˉX−ˉY)−δ0√σ21/n1+σ22/n2
모분산을 모르는 경우에는 위 공식 그 자리에 표본분산을 plug-in하고 자유도가 n1+n2−2인 t 분포를 사용하면 된다.
- 모 평균의 차(μ1−μ2)에 대한 신뢰구간
(ˉX−ˉY)±tn1+n2−2,α/2√σ21/n1+σ22/n2
과거에는 귀무가설을 등분산으로 놓고, 등분산 여부를 검정(F test)한 뒤, 귀무가설을 채택하면, 합동분산을 구해서 분모에 사용하였으나, 이제는 등분산 가정을 하지 않고, 자료를 분석하는 것이 추세이다.
다만, 위 공식에서 자유도를 Satterthwaite 방식으로 보정해준 test를 Welch’s two sample t-test라고 하며, R에서의 default 분석법이다.
두 군에서의 Satterthwaite 자유도 공식은 다음과 같다.
^V(ˉX)=S2ˉX=S21n1
^V(ˉY)=S2ˉY=S22n2
Df=(S21/n1+S22/n2)2(S21n1)2/(n1−1)+(S22n2)2/(n2−1)=(S2ˉX+S2ˉY)2( S2ˉX)2/(n1−1)+(S2ˉY)2/(n2−1)
[예제 8-1] 한 군에서의 관찰값이 1, 3, 5이고, 다른 군에서의 관찰값이 2, 4, 6, 8이었다. R을 이용하여 Welch’s two sample t-test를 시행하시오.
= c(1, 3, 5)
x = c(2, 4, 6, 8)
y = t.test(x, y) ; r1 r1
Welch Two Sample t-test
data: x and y
t = -1.1547, df = 4.9592, p-value = 0.3008
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-6.463423 2.463423
sample estimates:
mean of x mean of y
3 5
다음은 위의 결과가 어떻게 나왔는지 일일이 재현한 것이다.
= c(mean(x), mean(y)) # sample means
sampMeans = sampMeans[1] - sampMeans[2] # Point Estimate of difference
PE = c(length(x), length(y)) # sample sizes of groups
ns = c(var(x), var(y))/ns # estimates of variances of sample means
vars = sqrt(sum(vars)) # Standard Error
SE = 0 # Difference under null hypothesis
nullHypo = (PE - nullHypo)/SE ; t.val # t value t.val
[1] -1.154701
= ns - 1 # degree of freddoms of sample means
dfs = sum(vars)^2/sum(vars^2/dfs) ; Df # Satterthwaite Degree of Freedom Df
[1] 4.959184
= 2*pt(-abs(t.val), Df) # p value
p.val = 0.05 # significance level
alpha = PE + c(-1, 1)*qt(1 - alpha/2, Df)*SE ; ci # 100*(1 - alpha)% Confidence Interval ci
[1] -6.463423 2.463423
8.1.1 R t.test 함수 흉내내기
attr(t.val, "names") = "t"
attr(Df, "names") = "df"
attr(ci, "conf.level") = 1 - alpha
attr(sampMeans, "names") = c("mean of x", "mean of y")
attr(nullHypo, "names") = "difference in means"
= list()
r2 $statistic = t.val
r2$parameter = Df
r2$p.value = p.val
r2$conf.int = ci
r2$estimate = sampMeans
r2$null.value = nullHypo
r2$stderr = SE
r2$alternative = "two.sided"
r2$method = "Welch Two Sample t-test"
r2$data.name = "x and y"
r2class(r2) = "htest"
r2
Welch Two Sample t-test
data: x and y
t = -1.1547, df = 4.9592, p-value = 0.3008
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-6.463423 2.463423
sample estimates:
mean of x mean of y
3 5
위에서 자유도를 구하기 위해 sum(vars)^2/sum(vars^2/dfs)사용하였는데, 이것은 2개뿐 아니라, 더 많은 분산들을 합할 때의 자유도를 구할 때도 사용할 수 있다.
만약 두 군의 자료처럼 보이지만, 자료가 쌍으로(paired) 되어 있다면, 두 값의 차이를 구하고, 단일군에 대한 추론을 하면 된다.
[예제 8-2] 다음은 2017년 국민체력실태조사 자료에서 주로 사용하는 손과 아닌 손의 악력을 비교해 보는 R script이다. 앞 20명의 자료로 independent two-group t-test를 한 경우와 paired t-test를 한 경우를 비교하시오. 또한, 20명의 자료를 단순 무작위 추출(simple random sampling)하여 같은 작업을 반복하시오.
[풀이]
자료 불러오기
= read.csv("http://r.acr.kr/2017KoBody.csv", as.is=T) d2
전체 two-group t-test
t.test(d2$Grip, d2$GripD) # n=5200
Welch Two Sample t-test
data: d2$Grip and d2$GripD
t = 10.4, df = 10390, p-value < 2.2e-16
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
1.733074 2.538066
sample estimates:
mean of x mean of y
32.89139 30.75582
앞 20명의 independent two-group t-test와 paired t-test 비교
t.test(d2$Grip[1:20], d2$GripD[1:20])
Welch Two Sample t-test
data: d2$Grip[1:20] and d2$GripD[1:20]
t = 0.43394, df = 37.947, p-value = 0.6668
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-5.663054 8.753054
sample estimates:
mean of x mean of y
31.710 30.165
t.test(d2$Grip[1:20], d2$GripD[1:20], paired=T) # paired t-test
Paired t-test
data: d2$Grip[1:20] and d2$GripD[1:20]
t = 2.2315, df = 19, p-value = 0.03789
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
0.09590648 2.99409352
sample estimates:
mean difference
1.545
무작위 20명 추출하여 two-group t-test와 paired t-test 비교
= 20
n = sample(1:5200, n)
id0 t.test(d2$Grip[id0], d2$GripD[id0])
Welch Two Sample t-test
data: d2$Grip[id0] and d2$GripD[id0]
t = 0.8756, df = 37.833, p-value = 0.3868
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-3.359624 8.479624
sample estimates:
mean of x mean of y
34.065 31.505
t.test(d2$Grip[id0], d2$GripD[id0], paired=T)
Paired t-test
data: d2$Grip[id0] and d2$GripD[id0]
t = 5.2299, df = 19, p-value = 4.769e-05
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
1.535487 3.584513
sample estimates:
mean difference
2.56
왼손잡이와 오른손 잡이로 추정되는 사람 수
table(sign(d2$Grip - d2$GripD)) # 900, 64, 4236
-1 0 1
900 64 4236