पेयरड t-टेस्ट्स

Python में Hypothesis Testing

James Chapman

Curriculum Manager, DataCamp

US रिपब्लिकन प्रेसिडेंट्स डेटासेट

         state       county  repub_percent_08  repub_percent_12
0      Alabama         Hale         38.957877         37.139882
1     Arkansas       Nevada         56.726272         58.983452
2   California         Lake         38.896719         39.331367
3   California      Ventura         42.923190         45.250693
..         ...          ...               ...               ...
96   Wisconsin    La Crosse         37.490904         40.577038
97   Wisconsin    Lafayette         38.104967         41.675050
98     Wyoming       Weston         76.684241         83.983328
99      Alaska  District 34         77.063259         40.789626

[100 rows x 4 columns]

100 पंक्तियाँ; हर पंक्ति में राष्ट्रपति चुनाव के लिए काउंटी-स्तर के वोट हैं.

1 https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/VOQCHQ
Python में Hypothesis Testing

हाइपोथीसिस

प्रश्न: क्या 2008 में रिपब्लिकन उम्मीदवार के वोट प्रतिशत 2012 से कम थे?

$H_{0}$: $\mu_{2008} - \mu_{2012} = 0$

$H_{A}$: $\mu_{2008} - \mu_{2012} < 0$

$\alpha = 0.05$ का significance level लें.

  • डेटा paired है → हर वोट प्रतिशत उसी काउंटी से जुड़ा है
    • मॉडल में वोटिंग पैटर्न कैप्चर करना चाहते हैं
Python में Hypothesis Testing

दो सैंपल से एक तक

sample_data = repub_votes_potus_08_12
sample_data['diff'] = sample_data['repub_percent_08'] - sample_data['repub_percent_12']
import matplotlib.pyplot as plt
sample_data['diff'].hist(bins=20)

diff वैरिएबल का हिस्टोग्राम - ज्यादातर मान -10 से 10 के बीच, कुछ आउट्लायर्स हैं.

Python में Hypothesis Testing

अंतर के सैंपल आँकड़े निकालें

xbar_diff = sample_data['diff'].mean()
-2.877109041242944
Python में Hypothesis Testing

संशोधित हाइपोथीसिस

पुरानी हाइपोथीसिस:

$H_{0}$: $\mu_{2008} - \mu_{2012} = 0$

$H_{A}$: $\mu_{2008} - \mu_{2012} < 0$

 

नई हाइपोथीसिस:

$H_{0}$: $\mu_{\text{diff}} = 0$

$H_{A}$: $ \mu_{\text{diff}} < 0$

$t = \dfrac{\bar{x}_{\text{diff}} - \mu_{\text{diff}}}{\sqrt{\dfrac{s_{diff}^2}{n_{\text{diff}}}}}$

$df = n_{diff} - 1$

Python में Hypothesis Testing

p-value की गणना

n_diff = len(sample_data)
100
s_diff = sample_data['diff'].std()
t_stat = (xbar_diff-0) / np.sqrt(s_diff**2/n_diff)
-5.601043121928489
degrees_of_freedom = n_diff - 1
99

$t = \dfrac{\bar{x}_{\text{diff}} - \mu_{\text{diff}}}{\sqrt{\dfrac{s_{\text{diff}}^2}{n_{\text{diff}}}}}$

$df = n_{\text{diff}} - 1$

 

from scipy.stats import t
p_value = t.cdf(t_stat, df=n_diff-1)
9.572537285272411e-08
Python में Hypothesis Testing

ttest() से दो means के अंतर की जाँच

import pingouin

pingouin.ttest(x=sample_data['diff'],
y=0,
alternative="less")
               T  dof alternative         p-val          CI95%   cohen-d  \
T-test -5.601043   99        less  9.572537e-08  [-inf, -2.02]  0.560104   

             BF10  power  
T-test  1.323e+05    1.0
1 pingouin.ttest() से मिलने वाले Returns का विवरण pingouin की API docs में उपलब्ध है: https://pingouin-stats.org/generated/pingouin.ttest.html#pingouin.ttest.
Python में Hypothesis Testing

paired=True के साथ ttest()

pingouin.ttest(x=sample_data['repub_percent_08'],
               y=sample_data['repub_percent_12'],
               paired=True,
               alternative="less")
               T  dof alternative         p-val          CI95%   cohen-d  \
T-test -5.601043   99        less  9.572537e-08  [-inf, -2.02]  0.217364   

             BF10     power  
T-test  1.323e+05  0.696338
Python में Hypothesis Testing

Unpaired ttest()

pingouin.ttest(x=sample_data['repub_percent_08'],
               y=sample_data['repub_percent_12'],
               paired=False, # The default
               alternative="less")
               T  dof alternative     p-val         CI95%   cohen-d   BF10  \
T-test -1.536997  198        less  0.062945  [-inf, 0.22]  0.217364  0.927   

           power  
T-test  0.454972  
  • पेयरड डेटा पर unpaired t-test करने से false negative errors की संभावना बढ़ती है
Python में Hypothesis Testing

अभ्यास करते हैं!

Python में Hypothesis Testing

Preparing Video For Download...