8.1 두 군의 평균 비교

\(X_1, X_2, \cdots, X_{n_1} \stackrel{i.i.d}{\sim} N(\mu_1, \sigma_1^2)\) 이고,

\(Y_1, Y_2, \cdots, Y_{n_2} \stackrel{i.i.d}{\sim} N(\mu_2, \sigma_2^2)\) 이고,

\(X_i\)\(Y_i\) 간에도 서로 독립일 때,

\(\bar{X} = \frac{\sum_{i = 1}^{n_1} X_i}{n_1}\)

\(S_1^2 = \frac{\sum_{i = 1}^{n_1} (X_i - \bar{X})^2}{n_1 - 1}\)

\(\bar{Y} = \frac{\sum_{i = 1}^{n_2} Y_i}{n_2}\)

\(S_2^2 = \frac{\sum_{i = 1}^{n_1} (Y_i - \bar{Y})^2}{n_2 - 1}\) 라 하면,

\(E(\bar{X} - \bar{Y}) = \mu_1 - \mu_2\)

\(V(\bar{X} - \bar{Y}) = V(\bar{X}) + V(\bar{Y}) = \frac{\sigma_1^2}{n_1} + \frac{\sigma_2^2}{n_2}\) 이다.

\(Z = \frac{(\bar{X} - \bar{Y}) - (\mu_1 - \mu_2)}{\sqrt{\sigma_1^2/n_1 + \sigma_2^2/n_2}} \sim N(0, 1^2)\)

따라서, \(\mu_1 - \mu_2\)에 대한 \(100(1 - \alpha) %\) 신뢰구간은 다음과 같다.

\((\bar{X} - \bar{Y}) \pm z_{\alpha/2} \sqrt{\sigma_1^2/n_1 + \sigma_2^2/n_2}\)

또한, 차이가 \(\delta_0\)라는 귀무가설에 대한 검정통계량 \(Z\)의 식은 다음과 같다.

\(Z = \frac{(\bar{X} - \bar{Y}) - \delta_0}{\sqrt{\sigma_1^2/n_1 + \sigma_2^2/n_2}}\)

모분산을 모르는 경우에는 위 공식 그 자리에 표본분산을 plug-in하고 자유도가 \(n_1 + n_2 - 2\)인 t 분포를 사용하면 된다.

  • 모 평균의 차(\(\mu_1 - \mu_2\))에 대한 신뢰구간

\((\bar{X} - \bar{Y}) \pm t_{n_1 + n_2 - 2, \alpha/2} \sqrt{\sigma_1^2/n_1 + \sigma_2^2/n_2}\)

과거에는 귀무가설을 등분산으로 놓고, 등분산 여부를 검정(F test)한 뒤, 귀무가설을 채택하면, 합동분산을 구해서 분모에 사용하였으나, 이제는 등분산 가정을 하지 않고, 자료를 분석하는 것이 추세이다.

다만, 위 공식에서 자유도를 Satterthwaite 방식으로 보정해준 test를 Welch’s two sample t-test라고 하며, R에서의 default 분석법이다.

두 군에서의 Satterthwaite 자유도 공식은 다음과 같다.

\(\hat{V(\bar{X})} = S_{\bar{X}}^2 = \frac{S_1^2}{n_1}\)

\(\hat{V(\bar{Y})} = S_{\bar{Y}}^2 = \frac{S_2^2}{n_2}\)

\(Df = \frac{(S_1^2/n_1 + S_2^2/n_2)^2}{ \left(\frac{S_1^2}{n_1}\right)^2/(n_1 - 1) + \left(\frac{S_2^2}{n_2}\right)^2/(n_2 - 1)} = \frac{(S_{\bar{X}}^2 + S_{\bar{Y}}^2)^2}{ \left(\ S_{\bar{X}}^2 \right)^2/(n_1 - 1) + \left( S_{\bar{Y}}^2 \right)^2/(n_2 - 1)}\)

[예제 8-1] 한 군에서의 관찰값이 1, 3, 5이고, 다른 군에서의 관찰값이 2, 4, 6, 8이었다. R을 이용하여 Welch’s two sample t-test를 시행하시오.

x = c(1, 3, 5)
y = c(2, 4, 6, 8)
r1 = t.test(x, y) ; 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 

다음은 위의 결과가 어떻게 나왔는지 일일이 재현한 것이다.

sampMeans = c(mean(x), mean(y))                # sample means
PE = sampMeans[1] - sampMeans[2]               # Point Estimate of difference
ns = c(length(x), length(y))                   # sample sizes of groups
vars = c(var(x), var(y))/ns                    # estimates of variances of sample means
SE = sqrt(sum(vars))                           # Standard Error
nullHypo = 0                                   # Difference under null hypothesis
t.val = (PE - nullHypo)/SE ; t.val             # t value
[1] -1.154701
dfs = ns - 1                                   # degree of freddoms of sample means
Df = sum(vars)^2/sum(vars^2/dfs) ; Df          # Satterthwaite Degree of Freedom
[1] 4.959184
p.val = 2*pt(-abs(t.val), Df)                  # p value
alpha = 0.05                                   # significance level
ci = PE + c(-1, 1)*qt(1 - alpha/2, Df)*SE ; ci # 100*(1 - alpha)% Confidence Interval
[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"

r2 = 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"
class(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)하여 같은 작업을 반복하시오.

[풀이]

자료 불러오기

d2 = read.csv("http://r.acr.kr/2017KoBody.csv", as.is=T)

전체 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 비교

n = 20
id0 = sample(1:5200, n)
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