Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Section 3.6 — Statistical design and error analysis

This notebook contains the code examples from Section 3.6 Statistical design and error analysis of the No Bullshit Guide to Statistics.

Notebook setup

Note: you may need to restart the kernel to use updated packages.
<Figure size 640x480 with 0 Axes>
datasets/ directory already exists.

Definitions

Design params: n = 9 , alpha = 0.05 , beta = 0.9106637458903489 , Delta = 0.2 , d = 0.1 , CV = 1.0965690846343146
<Figure size 500x150 with 1 Axes>
Design params: n = 9 , alpha = 0.05 , beta = 0.22508058066585585 , Delta = 1.6 , d = 0.8 , CV = 1.0965690846343146
<Figure size 600x160 with 1 Axes>

Hypothesis decision rules

Decision rule based on pp-values

# pre-data
alpha = ...   # chosen in advance

# post-data
obst = ...    # calculated from sample
rvT0 = ...    # sampling distribution under H0
pvalue = ...  # prob. of obst under rvT0

# make decision based on p-value
if pvalue <= alpha:
    decision = "Reject H0"
else:
    decision = "Fail to reject H0"

Simplified decision rule

# pre-data
alpha = ...     # chosen in advance
rvT0 = ...      # sampling distribution under H0
CV_alpha = ...  # calculated from alpha-quantile of rvT0

# post-data
obst = ...      # calculated from sample
# make decision based on test statistic
if obst >= CV_alpha:
    decision = "Reject H0"
else:
    decision = "Fail to reject H0"

Statistical design

Design params: n = 15 , alpha = 0.05 , beta = 0.8079200023112518 , Delta = 0.4 , d = 0.2 , CV = 0.8493987605509223
Design params: n = 15 , alpha = 0.05 , beta = 0.2868036281956132 , Delta = 1.14 , d = 0.57 , CV = 0.8493987605509223
Design params: n = 15 , alpha = 0.05 , beta = 0.07303790512845218 , Delta = 1.6 , d = 0.8 , CV = 0.8493987605509223
Design params: n = 15 , alpha = 0.05 , beta = 0.0003494316033385531 , Delta = 2.6 , d = 1.3 , CV = 0.8493987605509223
<Figure size 600x300 with 4 Axes>
Design params: n = 15 , alpha = 0.05 , beta = 0.2485890864965961 , Delta = 1.2 , d = 0.6 , CV = 0.8493987605509223
Design params: n = 30 , alpha = 0.05 , beta = 0.05034872950555784 , Delta = 1.2 , d = 0.6 , CV = 0.6006156235170057
<Figure size 700x160 with 2 Axes>
Design params: n = 15 , alpha = 0.05 , beta = 0.2485890864965961 , Delta = 1.2 , d = 0.6 , CV = 0.8493987605509223
Design params: n = 15 , alpha = 0.1 , beta = 0.14865057213685162 , Delta = 1.2 , d = 0.6 , CV = 0.6617903827547039
<Figure size 700x160 with 2 Axes>

Cohen’s d standardized effect size

The effect size Δ\Delta we use in statistical design is usually expressed as a standardized effect size like Cohen’s dd, which is defined as the observed “difference” divided by a standard deviation of the theoretical model under the null hypothesis.

One-sample case

For a one-sample test of the mean relative to a null model with mean μ0\mu_0, Cohen’s dd is calculated as:

d=x‾−μ0sx,d = \frac{\overline{\mathbf{x}} - \mu_0}{ s_{\mathbf{x}} },

where sxs_{\mathbf{x}} is the sample standard deviation.

When doing statistical design, we haven’t obtained the sample x\mathbf{x} yet so we don’t know what its standard deviation sxs_{\mathbf{x}} will be, so we have to make an educated guess of this value, which usually means using the standard deviation of the theoretical model σX0\sigma_{X_0}.

Two-sample case

For a two-sample test for the difference between means, Cohen’s dd is defined as the difference between sample means divided by the pooled standard deviation:

d=x‾−y‾sp.d = \frac{\overline{\mathbf{x}} - \overline{\mathbf{y}} }{ s_{p} }.

where the formula for the pooled variance is sp2=[(n−1)sx2+(m−1)sy2]/(n+m−2)s_{p}^2 = [(n -1)s_{\mathbf{x}}^2 + (m -1)s_{\mathbf{y}}^2]/(n + m -2).

When doing statistical design, we don’t know the standard deviations sxs_{\mathbf{x}} and sys_{\mathbf{y}} so we replace them with the theoretical standard deviations σX0\sigma_{X_0} and σY0\sigma_{Y_0} under the null hypothesis.

Recall table of reference values for Cohen’s dd standardized effect sizes, suggested by Cohen in (Cohen YYYY TODO).

Cohen’s dEffect size
0.01very small
0.20small
0.50medium
0.80large

The formulas for sample size planning and power calculations we’ll present below are based on Cohen’s dd.

This means you’ll have to express your guess about the effect size Δ\Delta as d=Δσ0d = \frac{\Delta}{\sigma_0} where σ0\sigma_0 is your best about the standard deviation of the theoretical distribution.

Note Cohen’s dd is based on the standard deviations of the “raw” theoretical population, and not standard error.

Example 1: detect kombuncha volume deviation from theory

Design Type A: choose α=0.05\alpha=0.05, β=0.2\beta=0.2, Δmin=4  ml\Delta_{\textrm{min}} = 4\;\text{ml} then calculate required sample size nn. We’ll assume σ=10\sigma=10.

38.64098270012354

Using statsmodels

To use the statsmodels sample size estimation function, we’ll need to express the effect size Δ=4\Delta=4 in terms of Cohen’s dd.

0.4
40.02907613995641
1.684844586046098
<Figure size 600x240 with 1 Axes>
<Figure size 600x240 with 1 Axes>

Data collection

We can select a random sample of size n=41n=41 from the populaiton kpop55 by calling the .sample method.

41
1001.6541463414634
1.6541463414633881

Statistical analysis on the sample from Batch 55

1.684844586046098
1.1024261132512938

We can now apply the simplified decision rule that compares the test statistic t55 to the cutoff value CV_alpha.

'Fail to reject H0'

Alternatively, we can make the decision whether to reject H0H_0 or not using the “old” decision rule that compares the pp-value to the cutoff α=0.05\alpha=0.05. We can compute the p-value from ttest_mean function we defined in Section 3.4, see 34_analytical_approx.ipynb.

p-value = 0.27686546884474605
'Fail to reject H0'
0.2917

Example 2: comparison of East vs. West electricity prices

Loading...

Need a guess the effect size, which we’ll express in terms of Cohen’s dd. We choose d=1d=1, which corresponds to a difference of one cent Δmin=1\Delta_{\text{min}} = 1, assuming the standard deviation of the prices is around σ=1\sigma=1.

Solving for a desired power

The function solve_power takes as argument the chosen level of power, and two of the three other design parameters alpha, nobs1, and effect_size, and calculates the value of the third parameter required to achieve the chosen level of power =(1−β)=(1-\beta).

0.5133625331067566

The Type II error rate β\beta of this test is

0.4866374668931537
<Figure size 600x240 with 1 Axes>

If Bob wants to achieve 80% power for future price comparison tests, he needs to collect samples of size n=1n=1 or larger.

16.714722446954013

Calculate minimum effect size

1.406924652680373

(optional) Calculate sample size needed to achieve 0.8 power

16.714722446954013

Alternative calculation methods

Using G*Power

TODO: add sceenshots

Using pingouin

Sample size for the one-sample tt-test

40.029076225855356

We can also verify that n=41n=41 produces a test with 80%80\% power.

0.8085822361693652

Power of Bob’s two-sample tt-test

0.5133625331067566

Explanations

One-sided and two-sided rejection regions

-1.8331129326562372
1.8331129326562368
(-2.262157162798206, 2.262157162798205)
<Figure size 700x160 with 3 Axes>

Derivation of the sample size planning formula

MAYBE show math steps

Discussion

A conceptual amalgam of two ideas

Steps of the Neyman—Pearson hypothesis test

  1. Calculate the cutoff value CVα\text{CV}_{\alpha}.

  2. Calculate the test statistic t55t_{55} from the sample k55\mathbf{k}_{55}.

  3. Apply the simplified decision rule that compares the test statistic to the cutoff value.

1.6838510133356523
1.1024261132512938
'Fail to reject H0'

Steps of the Fisherian hypothesis test

  1. Calculate the test statistic t55t_{55}.

  2. Calculate the pp-value of the statistic from the sampling distribution under H0H_0.

  3. Make a decision whether to reject H0H_0 by comparing the pp-value to the cutoff α=0.05\alpha=0.05.

1.1024261132512938
0.13843273442237303

By the way, we can also obtain the pp-value by calling the helper function ttest_mean that we defined in Section 3.4, see 34_analytical_approx.ipynb.

0.13843273442237303
'Fail to reject H0'

We see the results of the Neyman–Pearson and Fisherian decision rules are the same. Indeed, we’ll always come to the same decision, whether we use criterion p<αp < \alpha in the space of pp-values, or the t55≥CVαt_{55} \geq \text{CV}_{\alpha} in the space of tt-values.

Post-hoc power analysis

1.6541463414633881
0.1654146341463388

The post-hoc power for the test we performed in Example~1 is:

0.2730623700889532

It’s not clear what this power means...

Unique value proposition of hypothesis testing

Exercises