Niet-parametrische toetsen

Hypothesetoetsen in Python

James Chapman

Curriculum Manager, DataCamp

Parametrische toetsen

  • z-test, t-test en ANOVA zijn allemaal parametrische toetsen
  • Veronderstellen een normale verdeling
  • Vereisen voldoende grote steekproeven
Hypothesetoetsen in Python

Kleinere dataset Republikeinse stemmen

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
Hypothesetoetsen in Python

Resultaten met pingouin.ttest()

  • 5 paren is te weinig voor de steekproefvoorwaarde van de gepaarde t-toets:
  • Minstens 30 paren waarnemingen over de steekproeven.
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
Hypothesetoetsen in Python

Niet-parametrische toetsen

  • Niet-parametrische toetsen vermijden de aannames en voorwaarden van parametrische toetsen
  • Veel niet-parametrische toetsen gebruiken rangen van de data
x = [1, 15, 3, 10, 6]
from scipy.stats import rankdata
rankdata(x)
array([1., 5., 2., 4., 3.])
Hypothesetoetsen in Python

Niet-parametrische toetsen

  • Niet-parametrische toetsen zijn betrouwbaarder dan parametrische bij kleine steekproeven en wanneer data niet normaal verdeeld is
Hypothesetoetsen in Python

Niet-parametrische toetsen

  • Niet-parametrische toetsen zijn betrouwbaarder dan parametrische bij kleine steekproeven en wanneer data niet normaal verdeeld is

 

Wilcoxon-signed-ranktoets
  • Ontwikkeld door Frank Wilcoxon in 1945
  • Een van de eerste niet-parametrische procedures
Hypothesetoetsen in Python

Wilcoxon-signed-ranktoets (Stap 1)

  • Werkt met de gerangschikte absolute verschillen tussen paren
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
Hypothesetoetsen in Python

Wilcoxon-signed-ranktoets (Stap 2)

  • Werkt met de gerangschikte absolute verschillen tussen paren
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
Hypothesetoetsen in Python

Wilcoxon-signed-ranktoets (Stap 3)

  • Werkt met de gerangschikte absolute verschillen tussen paren
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
Hypothesetoetsen in Python

Wilcoxon-signed-ranktoets (Stap 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
  • Neem de som van de rangen voor negatieve en positieve verschillen
T_minus = 1 + 4 + 5 + 2 + 3

T_plus = 0
W = np.min([T_minus, T_plus])
0
Hypothesetoetsen in Python

Implementatie met 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

Verwerp H0 niet, want 0.03125 > 0.01

Hypothesetoetsen in Python

Laten we oefenen!

Hypothesetoetsen in Python

Preparing Video For Download...