t-testleri uygulama

Python'da Hipotez Testi

James Chapman

Curriculum Manager, DataCamp

İki örneklemli problemler

  • Bir değişkenin örneklem istatistiklerini gruplar arasında karşılaştırın
  • converted_comp sayısal bir değişkendir
  • age_first_code_cut düzeyleri ("child" ve "adult") olan kategorik bir değişkendir
  • İlk kez çocukken kodlayanlar, yetişkinken başlayanlara göre daha yüksek ücret alıyor mu?
Python'da Hipotez Testi

Hipotezler

$H_{0}$: İlk kez çocukken kodlayanlar ile ilk kez yetişkinken kodlayanların ortalama ücreti (USD) aynıdır.

$H_{0}$: $\mu_{child} = \mu_{adult}$

$H_{0}$: $\mu_{child} - \mu_{adult} = 0$

$H_{A}$: İlk kez çocukken kodlayanların ortalama ücreti (USD), ilk kez yetişkinken kodlayanlardan daha büyüktür.

$H_{A}$: $\mu_{child} > \mu_{adult}$

$H_{A}$: $\mu_{child} - \mu_{adult} > 0$

Python'da Hipotez Testi

Gruplara göre özet istatistikleri hesaplama

stack_overflow.groupby('age_first_code_cut')['converted_comp'].mean()
age_first_code_cut
adult    111313.311047
child    132419.570621
Name: converted_comp, dtype: float64
Python'da Hipotez Testi

Test istatistikleri

  • Örneklem ortalaması, anakütle ortalamasını tahmin eder
  • $\bar{x}$ - örneklem ortalaması
  • $\bar{x}_{child}$ - ilk kez çocukken kodlayanların ortalama ücreti
  • $\bar{x}_{adult}$ - ilk kez yetişkinken kodlayanların ortalama ücreti
  • $\bar{x}_{child} - \bar{x}_{adult}$ - bir test istatistiği
  • z-skoru - (standartlaştırılmış) test istatistiği
Python'da Hipotez Testi

Test istatistiğini standartlaştırma

$z = \dfrac{\text{örneklem istatistiği} - \text{anakütle parametresi}}{\text{standart hata}}$

$t = \dfrac{\text{örneklem istatistikleri farkı} - \text{anakütle parametreleri farkı}}{\text{standart hata}}$

$t = \dfrac{(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}}) - (\mu_{\text{child}} - \mu_{\text{adult}})}{SE(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}})}$

Python'da Hipotez Testi

Standart hata

$SE(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}}) \approx \sqrt{\dfrac{s_{\text{child}}^2}{n_{\text{child}}} + \dfrac{s_{\text{adult}}^2}{n_{\text{adult}}}}$

$s$ değişkenin standart sapmasıdır

$n$ örneklem büyüklüğüdür (örneklemdeki gözlem/satır sayısı)

Python'da Hipotez Testi

Sıfır hipotezinin doğru olduğunu varsaymak

$t = \dfrac{(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}}) - (\mu_{\text{child}} - \mu_{\text{adult}})}{SE(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}})}$

$H_{0}$: $\mu_{\text{child}} - \mu_{\text{adult}} = 0$    $\rightarrow$     $t = \dfrac{(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}}) }{SE(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}})}$

$t = \dfrac{(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}})}{\sqrt{\dfrac{s_{\text{child}}^2}{n_{\text{child}}} + \dfrac{s_{\text{adult}}^2}{n_{\text{adult}}}}}$

Python'da Hipotez Testi

Sıfır hipotezi doğru kabul edilerek hesaplamalar

xbar = stack_overflow.groupby('age_first_code_cut')['converted_comp'].mean()
adult    111313.311047
child    132419.570621
Name: converted_comp, dtype: float64 age_first_code_cut
s = stack_overflow.groupby('age_first_code_cut')['converted_comp'].std()
adult    271546.521729
child    255585.240115
Name: converted_comp, dtype: float64 age_first_code_cut
n = stack_overflow.groupby('age_first_code_cut')['converted_comp'].count()
adult    1376
child     885
Name: converted_comp, dtype: int64
Python'da Hipotez Testi

Test istatistiğini hesaplama

$t = \dfrac{(\bar{x}_{\text{child}} - \bar{x}_{\text{adult}})}{\sqrt{\dfrac{s_{\text{child}}^2}{n_{\text{child}}} + \dfrac{s_{\text{adult}}^2}{n_{\text{adult}}}}}$

import numpy as np
numerator = xbar_child - xbar_adult
denominator = np.sqrt(s_child ** 2 / n_child + s_adult ** 2 / n_adult)
t_stat = numerator / denominator
1.8699313316221844
Python'da Hipotez Testi

Passons à la pratique !

Python'da Hipotez Testi

Preparing Video For Download...