Performing t-tests

Hypothesis Testing in Python

James Chapman

Curriculum Manager, DataCamp

Two-sample problems

  • Compare sample statistics across groups of a variable
  • converted_comp is a numerical variable
  • age_first_code_cut is a categorical variable with levels ("child" and "adult")
  • Are users who first programmed as a child compensated higher than those that started as adults?
Hypothesis Testing in Python

Hypotheses

$H_{0}$: The mean compensation (in USD) is the same for those that coded first as a child and those that coded first as an adult.

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

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

$H_{A}$: The mean compensation (in USD) is greater for those that coded first as a child compared to those that coded first as an adult.

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

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

Hypothesis Testing in Python

Calculating groupwise summary statistics

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
Hypothesis Testing in Python

Test statistics

  • Sample mean estimates the population mean
  • $\bar{x}$ - a sample mean
  • $\bar{x}_{child}$ - sample mean compensation for coding first as a child
  • $\bar{x}_{adult}$ - sample mean compensation for coding first as an adult
  • $\bar{x}_{child} - \bar{x}_{adult}$ - a test statistic
  • z-score - a (standardized) test statistic
Hypothesis Testing in Python

Standardizing the test statistic

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

Hypothesis Testing in Python

Standard error

$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 the standard deviation of the variable

$n$ is the sample size (number of observations/rows in sample)

Hypothesis Testing in Python

Assuming the null hypothesis is true

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

Hypothesis Testing in Python

Calculations assuming the null hypothesis is true

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
Hypothesis Testing in Python

Calculating the test statistic

$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
Hypothesis Testing in Python

Let's practice!

Hypothesis Testing in Python

Preparing Video For Download...