การทดสอบแบบ Non-parametric

การทดสอบสมมติฐานด้วย Python

James Chapman

Curriculum Manager, DataCamp

การทดสอบแบบ parametric

  • z-test, t-test และ ANOVA ล้วนเป็นการทดสอบแบบ parametric
  • ต้องการการแจกแจงปกติ
  • ต้องการขนาดตัวอย่างที่ใหญ่พอ
การทดสอบสมมติฐานด้วย Python

ข้อมูลคะแนนเสียงพรรค Republican ชุดเล็ก

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 คู่ไม่เพียงพอสำหรับเงื่อนไขขนาดตัวอย่างของ paired t-test:
  • ต้องมีอย่างน้อย 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

การทดสอบแบบ Non-parametric

  • การทดสอบแบบ non-parametric ไม่ต้องอาศัยข้อสมมติแบบ parametric
  • การทดสอบส่วนใหญ่ใช้ ลำดับที่ ของข้อมูล
x = [1, 15, 3, 10, 6]
from scipy.stats import rankdata
rankdata(x)
array([1., 5., 2., 4., 3.])
การทดสอบสมมติฐานด้วย Python

การทดสอบแบบ Non-parametric

  • การทดสอบแบบ non-parametric เชื่อถือได้มากกว่าแบบ parametric เมื่อ ขนาดตัวอย่างเล็ก หรือข้อมูล ไม่ได้แจกแจงแบบปกติ
การทดสอบสมมติฐานด้วย Python

การทดสอบแบบ Non-parametric

  • การทดสอบแบบ non-parametric เชื่อถือได้มากกว่าแบบ parametric เมื่อ ขนาดตัวอย่างเล็ก หรือข้อมูล ไม่ได้แจกแจงแบบปกติ

 

การทดสอบ Wilcoxon-signed rank
  • พัฒนาโดย Frank Wilcoxon ในปี 1945
  • เป็นหนึ่งในกระบวนการ non-parametric กลุ่มแรก
การทดสอบสมมติฐานด้วย Python

การทดสอบ Wilcoxon-signed rank (ขั้นตอนที่ 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-signed rank (ขั้นตอนที่ 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-signed rank (ขั้นตอนที่ 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-signed rank (ขั้นตอนที่ 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

ไม่สามารถปฏิเสธ $H_0$ ได้ เนื่องจาก 0.03125 > 0.01

การทดสอบสมมติฐานด้วย Python

มาฝึกกันเถอะ!

การทดสอบสมมติฐานด้วย Python

Preparing Video For Download...