Techbrad

R, Python을 활용한 Independence two Sample t-test (독립 표본 t 검정) 본문

AI & BigData/Basic

R, Python을 활용한 Independence two Sample t-test (독립 표본 t 검정)

brad.min 2020. 12. 13. 20:47
반응형

Independent two sample t-test (독립 표본)

표본의 두 집단이 서로 관계가 없는 가정 하에 두 집단의 평균의 차이가 유의한지 검증하는 방법이다. 만약 A와 B 비타민을 먹은 사람들의 건강 수치의 평균을 비교하고자 할 때 20명을 뽑아 A와 B 비타민을 먹은 경우 독립 되었다고 할 수 없다. A 비타민을 복용 후 B 비타민에 영향을 주었을 수도 있기 때문이다. 따라서 20 명씩 두 집단을 뽑아 A와 B 비타민을 각각 복용하는 경우 독립되었다라고 할 수 있다.

 

실습

흡연자와 비흡연자의 단기 기억력을 비교하면서 실습을 진행하였다. 

 

H0(귀무가설): 흡연자와 비흡연자의 단기 기억력은 같다.

H1(대립가설): 흡연자와 비흡연자의 단기 기억력은 다르다.

 

R 활용

# 데이터
nonsmokers = c(18, 22, 21, 17, 20, 17, 23, 20, 22, 21)
smokers = c(16, 20, 14, 21, 20, 18, 13, 15, 17, 21)

# 정규성 검정 (Shapiro-Wilk normality test)
shapiro.test(nonsmokers)
shapiro.test(smokers)


# 정규성 검정 결과
data:  nonsmokers
W = 0.91199, p-value = 0.295

data:  smokers

W = 0.91941, p-value = 0.3521

# 등분산성 검정
var.test(nonsmokers, smokers)


# 등분산성 검정
data:  nonsmokers and smokers
F = 0.52102, num df = 9, denom df = 9, p-value = 0.3456
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
0.1294138 2.0976199
sample estimates:
ratio of variances 
         0.5210191

독립 표본 t검증을 위해서는 두개의 집단이 정규성인지와 분산이 같은지를 먼저 확인해야한다. 두 집단이 정규성을 띄지 않거나 분산이 너무 다르면 결과의 신뢰가 떨어진다.

위의 데이터에서 정규성의 결과가 0.295, 0.3521 나왔으므로 모두 0.05 보다 크므로 정규성을 갖고 있다고 할 수 있다. 또한 등분산성 검정 결과는  0.5210191 나왔고 0.05 보다 크므로 분산이 비슷하다고 할 수 있다.

 

이와 같이 정규성과 등분산성을 검정하고나서 독립 t검정을 진행한다.

# t.test 함수 사용 (표본, 등분산성)
t.test(smokers, nonsmokers, var.equal=TRUE)

# Independent two sample t-test 결과 

data:  smokers and nonsmokers 
t = -2.2573, df = 18, p-value = 0.03665 
alternative hypothesis: true difference in means is not equal to 0 
95 percent confidence interval: 
 -5.0198634 -0.1801366 
sample estimates: 
mean of x mean of y  
     17.5      20.1 

결과 해석

p-value = 0.03665 이므로 대립 가설을 채택할 수 있다. 따라서 흡연자와 비흡연자와의 단기 기억력은 다르다. (단지 샘플 데이터의 결과이다..)

 

 

Python 활용

from scipy import stats
 
nonsmokers = [18, 22, 21, 17, 20, 17, 23, 20, 22, 21]
smokers = [16, 20, 14, 21, 20, 18, 13, 15, 17, 21]
 
# 정규성 검정 (Shapiro-Wilk normality test)
normal1 = stats.shapiro(nonsmokers)
normal2 = stats.shapiro(smokers)
print(normal1)
print(normal2)
 
#정규성 검정 결과
ShapiroResult(statistic=0.9119927883148193, pvalue=0.2949617803096771)
ShapiroResult(statistic=0.9194088578224182, pvalue=0.35205063223838806)


# 등분산성 검정 (levene test)
levene = stats.levene(nonsmokers, smokers)
print(levene)

LeveneResult(statistic=1.945945945945946, pvalue=0.18000074963498305)


# 등분산성 검정 (bartlett test)
bartlett = stats.bartlett(nonsmokers, smokers)
print(bartlett)

BartlettResult(statistic=0.8904487989220573, pvalue=0.3453557166446204)

 

반응형