비모수 검정

Python으로 배우는 가설 검정

James Chapman

Curriculum Manager, DataCamp

모수 검정

  • z-검정, t-검정, ANOVA는 모두 모수 검정
  • 정규 분포를 가정
  • 충분히 큰 표본 크기 필요
Python으로 배우는 가설 검정

공화당 득표율 소규모 데이터

print(repub_votes_small)
            state      county  repub_percent_08  repub_percent_12
80          Texas   Red River         68.507522         69.944817
84          Texas      Walker         60.707197         64.971903
33       Kentucky      Powell         57.059533         61.727293
81          Texas  Schleicher         74.386503         77.384464
93  West Virginia      Morgan         60.857614         64.068711
Python으로 배우는 가설 검정

pingouin.ttest() 결과

  • 5쌍은 대응표본 t-검정의 표본 크기 조건을 충족하지 못합니다:
  • 각 표본에 최소 30쌍의 관측값 필요.
alpha = 0.01

import pingouin pingouin.ttest(x=repub_votes_potus_08_12_small['repub_percent_08'], y=repub_votes_potus_08_12_small['repub_percent_12'], paired=True, alternative="less")
               T  dof alternative     p-val          CI95%   cohen-d    BF10     power
T-test -5.875753    4        less  0.002096  [-inf, -2.11]  0.500068  26.468  0.239034
Python으로 배우는 가설 검정

비모수 검정

  • 비모수 검정은 모수적 가정과 조건을 피합니다
  • 많은 비모수 검정은 데이터의 순위를 사용
x = [1, 15, 3, 10, 6]
from scipy.stats import rankdata
rankdata(x)
array([1., 5., 2., 4., 3.])
Python으로 배우는 가설 검정

비모수 검정

  • 비모수 검정은 표본 크기가 작거나 데이터가 정규 분포를 따르지 않을 때 모수 검정보다 더 신뢰할 수 있습니다
Python으로 배우는 가설 검정

비모수 검정

  • 비모수 검정은 표본 크기가 작거나 데이터가 정규 분포를 따르지 않을 때 모수 검정보다 더 신뢰할 수 있습니다

 

Wilcoxon 부호 순위 검정
  • 1945년 Frank Wilcoxon이 개발
  • 최초의 비모수 방법 중 하나
Python으로 배우는 가설 검정

Wilcoxon 부호 순위 검정 (1단계)

  • 쌍별 데이터의 순위화된 절댓값 차이를 기반으로 계산
repub_votes_small['diff'] = repub_votes_small['repub_percent_08'] -
                            repub_votes_small['repub_percent_12']
print(repub_votes_small)
            state      county  repub_percent_08  repub_percent_12      diff
80          Texas   Red River         68.507522         69.944817 -1.437295
84          Texas      Walker         60.707197         64.971903 -4.264705
33       Kentucky      Powell         57.059533         61.727293 -4.667760
81          Texas  Schleicher         74.386503         77.384464 -2.997961
93  West Virginia      Morgan         60.857614         64.068711 -3.211097
Python으로 배우는 가설 검정

Wilcoxon 부호 순위 검정 (2단계)

  • 쌍별 데이터의 순위화된 절댓값 차이를 기반으로 계산
repub_votes_small['abs_diff'] = repub_votes_small['diff'].abs()
print(repub_votes_small)
            state      county  repub_percent_08  repub_percent_12      diff  abs_diff
80          Texas   Red River         68.507522         69.944817 -1.437295  1.437295
84          Texas      Walker         60.707197         64.971903 -4.264705  4.264705
33       Kentucky      Powell         57.059533         61.727293 -4.667760  4.667760
81          Texas  Schleicher         74.386503         77.384464 -2.997961  2.997961
93  West Virginia      Morgan         60.857614         64.068711 -3.211097  3.211097
Python으로 배우는 가설 검정

Wilcoxon 부호 순위 검정 (3단계)

  • 쌍별 데이터의 순위화된 절댓값 차이를 기반으로 계산
from scipy.stats import rankdata
repub_votes_small['rank_abs_diff'] = rankdata(repub_votes_small['abs_diff'])
print(repub_votes_small)
            state      county  repub_percent_08  repub_percent_12      diff  abs_diff  rank_abs_diff
80          Texas   Red River         68.507522         69.944817 -1.437295  1.437295            1.0
84          Texas      Walker         60.707197         64.971903 -4.264705  4.264705            4.0
33       Kentucky      Powell         57.059533         61.727293 -4.667760  4.667760            5.0
81          Texas  Schleicher         74.386503         77.384464 -2.997961  2.997961            2.0
93  West Virginia      Morgan         60.857614         64.068711 -3.211097  3.211097            3.0
Python으로 배우는 가설 검정

Wilcoxon 부호 순위 검정 (4단계)

            state      county  repub_percent_08  repub_percent_12      diff  abs_diff  rank_abs_diff
80          Texas   Red River         68.507522         69.944817 -1.437295  1.437295            1.0
84          Texas      Walker         60.707197         64.971903 -4.264705  4.264705            4.0
33       Kentucky      Powell         57.059533         61.727293 -4.667760  4.667760            5.0
81          Texas  Schleicher         74.386503         77.384464 -2.997961  2.997961            2.0
93  West Virginia      Morgan         60.857614         64.068711 -3.211097  3.211097            3.0
  • 음수 및 양수 차이의 순위 합산
T_minus = 1 + 4 + 5 + 2 + 3

T_plus = 0
W = np.min([T_minus, T_plus])
0
Python으로 배우는 가설 검정

pingouin.wilcoxon() 구현

alpha = 0.01
pingouin.wilcoxon(x=repub_votes_potus_08_12_small['repub_percent_08'],
                  y=repub_votes_potus_08_12_small['repub_percent_12'],
                  alternative="less")
          W-val alternative    p-val  RBC  CLES
Wilcoxon    0.0        less  0.03125 -1.0  0.72

0.03125 > 0.01이므로 $H_0$ 기각 실패

Python으로 배우는 가설 검정

연습해 봅시다!

Python으로 배우는 가설 검정

Preparing Video For Download...