본문 바로가기
데이터분석과 AI/빅데이터 분석기사

[빅데이터분석기사 실기][작업형3] 비모수 검정 wilcoxon 부호순위 테스트 (단일표본, 대응표본)

by 우공80 2023. 6. 24.
728x90

wilcoxon 비모수 검정

단일표본 t-검정이나, 대응표본 t-검정은 데이터의 정규성을 가정하고 있습니다. 따라서 데이터의 정규성이 확보되지 않은 경우에는 비모수 검정 방법으로 Wilcoxon 부호 순위 테스트를 이용합니다.

 

1. 단일 표본

아래에서 참가자 그룹의 반응 시간이 200ms로 설정한 중위값과  다른지 확인하려고 한다고 가정합니다. 

우선 Shapiro-Wilk 테스트로 정규성 검정을 합니다.

from scipy.stats import shapiro, wilcoxon

# Data for the single sample
reaction_times = pd.Series([180, 210, 190, 220, 210, 211,195, 120, 130])

hypothesized_median = 200

statistic, p_value=stats.shapiro(reaction_times)
print("Shapiro-Wilk test statistics:", statistic)
print("p-value:", p_value)

Output: 

Shapiro-Wilk test statistics: 0.8194005489349365
p-value: 0.03392498195171356

p_value <0.05로 정규성을 만족하지 않습니다. 이 경우에 비모수 검정으로서 Wilcoxon 테스트를 진행합니다.

# Perform one-sample Wilcoxon signed-rank test
statistic, p_value = stats.wilcoxon(reaction_times - hypothesized_mean)

# Print the test statistic and p-value
print("Wilcoxon signed-rank test statistic:", statistic)
print("p-value:", p_value)

Output:

Wilcoxon signed-rank test statistic: 17.5
p-value: 0.5703125

p_value > 0.05 로서 귀무가설을 기각하지 못하고, 이 집단의 중위값은 200이라고 할 수 있습니다.

 

2. 대응 표본

from scipy.stats import shapiro, wilcoxon

# Data for the single sample
first_reaction_times = [180, 210, 190, 220, 210, 211,195, 120, 130]
second_reaction_times = [225, 235, 220, 240, 195, 182, 230, 110, 239]

hypothesized_median = 200

statistic, p_value=stats.shapiro(first_reaction_times)
print("Shapiro-Wilk test statistics 1:", statistic)
print("p-value:", p_value)
statistic, p_value=stats.shapiro(second_reaction_times)
print("Shapiro-Wilk test statistics 2:", statistic)
print("p-value:", p_value)

Output:

Shapiro-Wilk test statistics 1: 0.8194005489349365
p-value: 0.03392498195171356
Shapiro-Wilk test statistics 2: 0.7666259407997131
p-value: 0.0084445271641016

p_value <0.05로 정규성을 만족하지 않습니다. 따라서 비모수 검정으로서 Wilcoxon 테스트를 진행합니다.

# Perform one-sample Wilcoxon signed-rank test
statistic, p_value = stats.wilcoxon(first_reaction_times,second_reaction_times)

# Print the test statistic and p-value
print("Wilcoxon signed-rank test statistic:", statistic)
print("p-value:", p_value)

Output:

Wilcoxon signed-rank test statistic: 8.0
p-value: 0.09765625

p_value > 0.05 로서 귀무가설을 기각하지 못하므로 1차와 2차의 중위값은 같다고 할 수 있습니다. 

 

※ 위 내용은 "빅데이터 분석기사 실기 준비를 위한 캐글 놀이터"  및 "데이터마님 scipy tutorial"을 따라가며 공부한 내용입니다.

https://www.kaggle.com/datasets/agileteam/bigdatacertificationkr

 

Big Data Certification KR

빅데이터 분석기사 실기 (Python, R tutorial code)

www.kaggle.com

 

https://www.datamanim.com/dataset/97_scipy/scipy.html#t-one-sample

 

 

 

728x90

댓글