Power of a test

Foundations of Inference in Python

Paul Savala

Assistant Professor of Mathematics

What determines power?

Sample size Effect size Alpha
An image showing a large group of people. An image showing a patient receiving medication. An image of a ninety five percent confidence interval.
Foundations of Inference in Python

Simulating weight loss data

from scipy.stats import norm


# Control group: Mean 0 pounds, std 1 pound
control = norm.rvs(loc=0,
scale=1,
size=100)
# Treatment group: Mean -2 pounds, std 1 pound
treatment = norm.rvs(loc=-2, scale=1, size=100)
Foundations of Inference in Python

T-test

$H_0$: No difference in weight loss (wrong)

$H_a$: Treatment group loses weight (correct)

Conclusion: Correctly reject $H_0$ in favor of $H_a$.

from scipy.stats import ttest_ind


# Conduct a t-test alpha = 0.05 t_test = ttest_ind(treatment, control, alternative='less')
# Check the significance print(t_test.pvalue < alpha)
TRUE
Foundations of Inference in Python

Small sample size

control = norm.rvs(loc=0, scale=1, size=5)
treatment = norm.rvs(loc=-2, scale=1, size=5)

# Conduct a t-test tt = ttest_ind(treatment, control, alternative='less')
print(tt.pvalue < 0.05)
FALSE

Conclusion: Fail to reject H0 (wrong)

Foundations of Inference in Python

Small effect size

# Weight loss = 0.2 pounds, sample size = 100

treatment = norm.rvs(loc=-0.2, scale=1, size=100)
# Conduct a t-test t_test = ttest_ind(treatment, control, alternative='less')
print(t_test.pvalue < 0.05)
FALSE
Foundations of Inference in Python

Small effect? Big sample!

A small group of people standing next to a large group of people.

Foundations of Inference in Python

Defining the power of a test

If there is a significant effect, will our test detect it?

A magnifying glass showing germs on a cartoon hand.

Power of a test: If the alternative hypothesis (Ha) is true, how likely is our test to reject the null (H0) given the data we've supplied it?

Calculate power before collecting a sample

Foundations of Inference in Python

Calculating power

from statsmodels.stats import power
# Power function
tt_power = power.TTestIndPower()


# Calculate power pwr = tt_power.power(effect_size=0.2,
nobs1=100,
alpha=0.05)
print(pwr)
0.291

Low chance of detection!

Foundations of Inference in Python

Solving for power

nobs1 = TTestIndPower().solve_power(effect_size=-0.2, 
                                    nobs1=None, # Solve for
                                    alpha=0.05,
                                    power=0.8)

print(nobs1)
13735.26
Foundations of Inference in Python

Let's practice!

Foundations of Inference in Python

Preparing Video For Download...