Hypothesis test

The ASTA team

Statistical inference: Hypothesis and test

Concept of hypothesis

Significance test

Null and alternative hypothesis

Test statistic

\(P\)-value

Significance level

Significance test for mean

Two-sided \(t\)-test for mean:

Example: Two-sided \(t\)-test

from scipy.stats import t
1 - t.cdf(1.89, df = 3)
## np.float64(0.0775772517893365)

One-sided \(t\)-test for mean

The book also discusses one-sided \(t\)-tests for the mean, but we will not use those in the course.

Agresti: Overview of \(t\)-test

Significance test for proportion

Approximate test

Example: Approximate test

from statsmodels.stats.proportion import proportions_ztest, proportion_confint

nobs = 1200
count = nobs * 0.52 # number of individuals preferring tax increase

stat, p_value = proportions_ztest(count = count, nobs = nobs, value = 0.5)

ci_low, ci_high = proportion_confint(count, nobs, alpha = 0.05, method = 'normal')

print(f"95% CI: ({ci_low:.4f}, {ci_high:.4f})")
## 95% CI: (0.4917, 0.5483)
print(f"sample estimate: {count/nobs:.4f}")
## sample estimate: 0.5200
print(f"p-value: {p_value:.4g}")
## p-value: 0.1655

Binomial (exact) test

Example: Binomial test

from scipy.stats import binom
lower_tail = binom.cdf(k = 13, n = 30, p = 0.3)
1 - lower_tail
## np.float64(0.04005254768213129)

Binomial test in R

import pandas as pd
from scipy.stats import binomtest

chile = pd.read_csv("https://asta.math.aau.dk/datasets?file=Chile.txt", sep="\t")
counts = chile['sex'].value_counts()
counts
## sex
## F    1379
## M    1321
## Name: count, dtype: int64
successes = counts.iloc[0]
n = counts.sum()
result = binomtest(successes, n, p = 0.5, alternative='two-sided')
print("Estimated probability of success:", result.statistic)
## Estimated probability of success: 0.5107407407407407
print("p-value:", result.pvalue)
## p-value: 0.27265346580284056
print("95% CI:", result.proportion_ci(confidence_level = 0.95))
## 95% CI: ConfidenceInterval(low=0.49169713495924583, high=0.5297610330103562)
from statsmodels.stats.proportion import proportions_ztest, proportion_confint

stat, pval = proportions_ztest(count = successes, nobs = n, value=0.5, alternative = 'two-sided')
ci_low, ci_high = proportion_confint(count = successes, nobs = n, alpha = 0.05, method = 'normal')
print("Z statistic:", stat)
## Z statistic: 1.1164681495304731
print("p-value:", pval)
## p-value: 0.26422179636401866
print("95% CI:", (ci_low, ci_high))
## 95% CI: (0.49188533046505395, 0.5295961510164274)

Agresti: Overview of tests for mean and proportion