t-toetsen uitvoeren

Hypothesetoetsen in Python

James Chapman

Curriculum Manager, DataCamp

Twee-steekproefproblemen

  • Vergelijk steekproefstatistieken tussen groepen van een variabele
  • converted_comp is een numerieke variabele
  • age_first_code_cut is een categorische variabele met niveaus ("child" en "adult")
  • Worden gebruikers die als kind begonnen hoger betaald dan wie als volwassene begon?
Hypothesetoetsen in Python

Hypothesen

$H_{0}$: De gemiddelde beloning (in USD) is hetzelfde voor wie eerst als kind codeerde en wie eerst als volwassene codeerde.

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

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

$H_{A}$: De gemiddelde beloning (in USD) is hoger voor wie eerst als kind codeerde dan voor wie eerst als volwassene codeerde.

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

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

Hypothesetoetsen in Python

Groepsgewijze samenvattingen berekenen

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

Toetsingsgrootheden

  • De steekproefgemiddelde schat het populatiegemiddelde
  • $\bar{x}$ - een steekproefgemiddelde
  • $\bar{x}_{child}$ - steekproefgemiddelde beloning voor eerst als kind coderen
  • $\bar{x}_{adult}$ - steekproefgemiddelde beloning voor eerst als volwassene coderen
  • $\bar{x}_{child} - \bar{x}_{adult}$ - een toetsingsgrootheid
  • z-score - een (gestandaardiseerde) toetsingsgrootheid
Hypothesetoetsen in Python

De toetsingsgrootheid standaardiseren

$z = \dfrac{\text{sample stat} - \text{population parameter}}{\text{standard error}}$

$t = \dfrac{\text{difference in sample stats} - \text{difference in population parameters}}{\text{standard error}}$

$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}})}$

Hypothesetoetsen in Python

Standaardfout

$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$ is de standaardafwijking van de variabele

$n$ is de steekproefgrootte (aantal observaties/rijen in de steekproef)

Hypothesetoetsen in Python

Uitgaande van de nulhypothese

$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}}}}}$

Hypothesetoetsen in Python

Berekeningen onder de nulhypothese

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

De toetsingsgrootheid berekenen

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

Laten we oefenen!

Hypothesetoetsen in Python

Preparing Video For Download...