非母數檢定

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 符號等級檢定
  • 由 Frank Wilcoxon 於 1945 年提出
  • 最早的非母數方法之一
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

無法拒絕 $H_0$,因為 0.03125 > 0.01

Python 中的假設檢定

一起來練習吧!

Python 中的假設檢定

Preparing Video For Download...