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.7 — Inventory of statistical tests

This notebook contains the code examples from Section 3.7 Inventory of statistical tests from 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

Assumptions

Normality assumption

Here is a useful function for generating synthetic data to use with the parametric tests below.

NHST procedure

Categorization of statistical test recipes

Z-Tests

One-sample zz-test

See the examples/one_sample_z-test.ipynb notebook.

Proportion tests

One-sample zz-test for a proportion

z = 1.4638501094227998
0.14323490752466972
(1.4638501094227996, 0.1432349075246697)

Confidence interval

[0.23963062588979633, 0.9603693741102035]
(0.23963062588979633, 0.9603693741102036)

Binomial test

cf. Binomial test

0.16307999999999992
0.16308
0.16308

Two-sample zz-test for proportions

T-tests

One sample tt-test

See the examples/one_sample_t-test.ipynb notebook.

Welch’s two-sample tt-test

Student’s two-sample tt-test

(explain pooled variance as special case “Two sample t-test”, but inferior)

Paired tt-test

Chi-square tests

Chi-square test for goodness of fit

Example: are the digits of π\pi random?

(5.51852, 0.7869706202650394)

See original blog post for useful historical context about this https://probabilityandstats.wordpress.com/2017/03/14/are-digits-of-pi-random/

Example: birds foraging

Bird foraging behavior was investigated in an old-growth forest of Oregon. In the forest, 44% of the canopy volume was Douglas fir, 24% was ponderosa pine, 29% was grand fir, and 3% was western larch. The authors observed the behavior of several species of birds, one of which was the red-breasted nuthatch. They made 189 observations of this species foraging, recording 43 of observations in Douglas fir, 52 in ponderosa pine, 54 in grand fir, and 40 in western larch.

Using a chi-square test, we can test the null hypothesis that the proportions of foraging events are equal to the proportions of canopy volume. Considered a pp-value less than 1% to be significant.

cf. https://docs.scipy.org/doc/scipy/tutorial/stats/hypothesis_chisquare.html

(228.23515947653874, 3.3295585338846486e-49)

Chi-square test of independence

Example 1: starting from counts

(16.666666666666668, 2, 0.00024036947641951404)
(16.666666666666668, 0.00024036947641951404)

Example 2: starting from categorical observations

col_0  no  yes
row_0         
A       4    8
B       8    4
(2.6666666666666665, 1, 0.10247043485974996)
(2.6666666666666674, 1, 0.10247043485974905)

Chi-square test for homogeneity

(12.995726495726496, 4, 0.011296693934973229)

Chi-square test for the population variance

Analysis of variance (ANOVA) tests

One-way analysis of variance (ANOVA)

Example: doctors workplace

0.5526627461285655
<Figure size 1000x300 with 1 Axes>

Example: synthetic dataset with one group differenrt

0.002268927763807039

Correctly rejects H0H_0.

Example: synthetic dataset with all groups roughly the same

0.22426174367680077

Correctly fails to reject H0H_0.

Nonparametric tests

Use when assumptions for other tests not valid

BinomTestResult(k=6, n=15, alternative='two-sided', statistic=0.4, pvalue=0.6072387695312499)
BinomTestResult(k=9, n=15, alternative='two-sided', statistic=0.6, pvalue=0.6072387695312499)

One-sample Wilcoxon signed-rank test

Kruskal-Wallis analysis of variance by ranks

Resampling methods

Simulation tests

def simulation_test(sample, rvH0, estfunc, alt="two-sided"): """ Compute the p-value of the observed estimate `estfunc(sample)` under H0 described by the random variable `rvH0`. """ # 1. Compute the observed value of `estfunc` obsest = estfunc(sample) n = len(sample) # 2. Get sampling distribution of `estfunc` under H0 sampl_dist_H0 = gen_sampling_dist(rvH0, estfunc, n) # 3. Compute the p-value tails = tailvalues(sampl_dist_H0, obsest, alt=alt) pvalue = len(tails) / len(sampl_dist_H0) return pvalue

Two-sample permutation test

def permutation_test(xsample, ysample, estfunc, P=10000): """ Compute the p-value of the observed estimate `estfunc(xsample,ysample)` under the null hypothesis where the group membership is randomized. """ # 1. Compute the observed value of `estfunc` obsest = estfunc(xsample, ysample) # 2. Get sampling dist. of `estfunc` under H0 pestimates = [] for i in range(0, P): rsx, rsy = resample_under_H0(xsample, ysample) pestimate = estfunc(rsx, rsy) pestimates.append(pestimate) # 3. Compute the p-value tails = tailvalues(pestimates, obsest) pvalue = len(tails) / len(pestimates) return pvalue

Permutation ANOVA

def Fstat(*samples): I = len(samples) n = len(np.concatenate(samples)) xbar = np.mean(np.concatenate(samples)) SSbetween = sum(len(xi)*(np.mean(xi) - xbar)**2 for xi in samples) SSwithin = sum(np.sum((xi - np.mean(xi))**2) for xi in samples) MSbetween = SSbetween / (I - 1) MSwithin = SSwithin / (n - I) return MSbetween / MSwithin
def permutation_anova(samples, P=10000, alt="greater"): """ Compute the p-value of the observed F-statistic for `samples` list under the null hypothesis where the group membership is randomized. """ ns = [len(sample) for sample in samples] # 1. Compute the observed F-statistic obsfstat = Fstat(*samples) # 2. Get sampling dist. of F-statistic under H0 pfstats = [] for i in range(0, P): values = np.concatenate(samples) pvalues = np.random.permutation(values) psamples = [] nstart = 0 for nstep in ns: psample = pvalues[nstart:nstart+nstep] psamples.append(psample) nstart = nstart + nstep pfstat = Fstat(*psamples) pfstats.append(pfstat) # 3. Compute the p-value tails = tailvalues(pfstats, obsfstat, alt=alt) pvalue = len(tails) / len(pfstats) return pvalue
F = 3.6808227678358847
0.0297
(3.6808227678358847, 0.0298)
(3.6808227678358865, 0.029206733498721386)

Equivalence tests

See examples/two_sample_equivalence_test.ipynb for an example.

Distribution checks

Kolmogorov-Smirnov test

Shapiro-Wilk normality test

Discussion

Discussion

Exercises