본문 바로가기

■ 프로그래밍/R

[R] Paired Test

[ R : Paired Test ]


패키지 설치 및 로딩하기

install.packages("readxl")
library(readxl)


데이터 불러오기

sat <- readxl::read_excel(path = "SAT.xlsx",
                          sheet = 1,
                          col_names = TRUE)


SAT.xlsx


Paired Test

  • 언제 사용 하는가?
    A. 동일한 대상자에게 사전/사후 간의 양적 자료에 변화가 있는지를 분석하는 통계적인 방법

  • 자료: 양적자료 2개
    같은 대상의 양적자료인데 사전, 사후여야 함
    ex. 다이어트 전 A의 몸무게, 다이어트 후 A의 몸무게


방법

Two Sampel T-tset의 가설검정 단계는 아래와 같다.

1단계: 정규성 검정
2단계: 등분산 검정(Equality of Variance Test)
3단계: Two Sample T-test


가설 1

  • 귀무가설: 강의 만족도에 변동이 없다 (mu1 = mu2)
  • 대립가설: 강의 만족도가 변동이 있다 (mu1 < mu2)

1단계 : 정규성 검정

  • 귀무가설: (사전-사후)를 뺀 값은 정규분포를 따른다
  • 대립가설: (사전-사후)를 뺀 값은 정규분포를 따르지 않는다
sat$diff <- sat$pre - sat$post
shapiro.test(sat$diff)
## 
##  Shapiro-Wilk normality test
## 
## data:  sat$diff
## W = 0.88984, p-value = 0.02671

결론: 유의확률이 0.027이므로 유의수준 0.05에서 정규성 가정이 깨짐.
즉, 대립가설 채택.


2단계 : Wilcoxon’s signed rank test(윌콕슨의 부호 순위 검정)

wilcox.test(sat$pre, sat$post,    # 사전 양적자료, 사후 양적자료
            alternative = "less",
            paired = TRUE)
## Warning in wilcox.test.default(sat$pre, sat$post, alternative = "less", :
## cannot compute exact p-value with ties
## Warning in wilcox.test.default(sat$pre, sat$post, alternative = "less", :
## cannot compute exact p-value with zeroes
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  sat$pre and sat$post
## V = 5.5, p-value = 0.0004835
## alternative hypothesis: true location shift is less than 0

혹은 바로 diff(사후-사전)으로 wilcox.test를 돌릴 수 있다.

wilcox.test(sat$diff,
            alternative = "less")

결론: 유의확률이 0.000이므로 유의수준 0.05에서 대립가설 채택.
즉, 강의 만족도는 통계적으로 변동이 있다.


윌콕슨의 tie warning 해결
위에만 보더라도 “cannot compute exact p-value with ties”와 같은 tie warning이 뜬다.
이는 윌콕슨이 “순위 검정”이기 때문이다. 데이터를 순서대로 정렬 후, 그 것을 순위형(등수형)으로 바꾸기 때문에, 같은 값이 존재하면 tie warning이 뜬다.
즉, tie는 데이터 안에 완전히 똑같은 값이 있을 때 발생한다.
ex. 맨 처음 값이 0 0이라면, 각각의 등수는 1.5등 으로 표시된다는 것을 알려주기 위한 warning이다.

install.package("exactRankTests")
library(exactRankTests)
##  Package 'exactRankTests' is no longer under development.
##  Please consider using package 'coin' instead.
exactRankTests::wilcox.exact(sat$diff, 
                             alternative = "less")
## 
##  Exact Wilcoxon signed rank test
## 
## data:  sat$diff
## V = 5.5, p-value = 0.0001678
## alternative hypothesis: true mu is less than 0



만약 1단계에서 정규성 가정이 만족 되었다면,

2단계: t.test

1단계에서 정규성이 만족 되었다면, 대응 2표본 t검정 = 쌍체비교 = Paried T-test를 시행한다.

t.test(sat$pre, sat$post,
       alternative = "less",
       paired = TRUE)        # paired = TRUE를 안 쓰면, 단순 two sample t-test로 인식
## 
##  Paired t-test
## 
## data:  sat$pre and sat$post
## t = -4.3333, df = 19, p-value = 0.0001791
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
##        -Inf -0.7812602
## sample estimates:
## mean of the differences 
##                    -1.3

결론: 유의확률이 0.000이므로 유의수준 0.05에서 대립가설 채택.
즉, 이부일 강사의 강의는 통계적으로 매우 의미 있는 효과가 나타남.


실습

첨부 데이터로 아래 ’가설 2’를 따라해보자. 

weight.xlsx


가설 2

  • 귀무가설: 다이어트는 효과가 없다(사전 몸무게 평균과 사후 몸무게 평균이 같다)
  • 대립가설: 다이어트는 효과가 있다(사전 몸무게 평균이 사후 몸무게 평균보다 크다)