10 · Tests and corrections¶
You will learn how to ask "is there a difference?" of a question: run a test once per pair, read its effect sizes, test one-sided or against a margin, correct for multiple comparisons (within one result and across several), and turn results into tables for a paper. You will also see which tests synaptome refuses, and why.
You need 08 · Questions and 09 · Estimates.
from IPython.display import Markdown
import synaptome as syn
A first test¶
In the crossover trial, each subject took the drug and the placebo: a paired comparison. The Wilcoxon
signed-rank test asks whether the drug − placebo differences are centred on zero. Each row is one pair
of the question; p_corrected and significant come from the correction (Holm by default, which
changes nothing with a single pair).
crossover = syn.datasets.crossover()
q = crossover.compare("score", by="condition", reference="drug")
r = q.test("wilcoxon")
r.table
| a | b | statistic | p | p_corrected | significant | n | rank_biserial | hodges_lehmann | hodges_lehmann_ci_low | hodges_lehmann_ci_high | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | drug | placebo | 23.0 | 0.000076 | 0.000076 | True | 24 | 0.846667 | 5.069312 | 3.05072 | 7.048694 |
A p value says whether there is a difference, not how large it is, so every test also reports effect
sizes. For the Wilcoxon test these are the rank-biserial correlation and the Hodges–Lehmann shift, the
latter with its 95% interval. They are computed by the same code as q.estimate, so they are always
identical:
q.estimate("hodges_lehmann").table[["estimate", "ci_low", "ci_high"]]
| estimate | ci_low | ci_high | |
|---|---|---|---|
| 0 | 5.069312 | 3.05072 | 7.048694 |
effect_sizes= picks others: any pair estimator that suits the design (() for none).
q.test("paired_ttest", effect_sizes=("mean_diff", "cohens_dz")).table
| a | b | statistic | df | p | p_corrected | significant | n | mean_diff | mean_diff_ci_low | mean_diff_ci_high | cohens_dz | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | drug | placebo | 5.240201 | 23.0 | 0.000026 | 0.000026 | True | 24 | 4.964068 | 3.004419 | 6.923718 | 1.069652 |
One-sided tests and margins¶
In the method comparison, a lower error is better. "Is the proposed method better than the baseline?"
is one-sided: alternative="less" asks whether proposed − baseline is below zero. With offset, the
question becomes "better by more than a margin": below −0.02 here. Only the p value moves; the effect
sizes still describe the raw difference.
mc = syn.datasets.method_comparison()
eeg = mc.compare(
"error",
by="method",
reference="proposed",
subset={"sensors": "eeg"},
reduce={"source": "median"},
higher_is_better=False,
title="Proposed vs other methods, EEG",
)
eeg.test("wilcoxon", alternative="less", offset=-0.02).table
| a | b | statistic | p | p_corrected | significant | n | rank_biserial | hodges_lehmann | hodges_lehmann_ci_low | hodges_lehmann_ci_high | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | proposed | alternative | 0.0 | 0.000488 | 0.000488 | True | 11 | -1.0 | -0.101011 | -0.111785 | -0.088651 |
| 1 | proposed | baseline | 0.0 | 0.000015 | 0.000031 | True | 16 | -1.0 | -0.049625 | -0.059307 | -0.040703 |
Refused tests¶
Each test states what it needs. A Mann–Whitney test is for two independent groups, but here every subject was measured with every method. synaptome refuses and lists the tests that fit:
try:
eeg.test("mannwhitneyu")
except ValueError as err:
print(err)
mannwhitneyu is for independent comparisons, but 'method' is a within-subject factor (paired). Tests for this design: ['wilcoxon', 'paired_ttest', 'bootstrap'].
The same goes for a binary outcome (use mcnemar), an ordinal one (a Wilcoxon test ranks
differences, which need an interval scale), or an effect size of the wrong design.
All groups at once, then pairs¶
"Do the three methods differ at all?" is one omnibus question: the Friedman test, for matched groups, with Kendall's W as its effect size (1: every subject ranks the methods the same way). Pairwise tests are a separate call: synaptome never runs them behind your back.
eeg.test("friedman").table
| statistic | df | p | p_corrected | significant | n | k | kendalls_w | |
|---|---|---|---|---|---|---|---|---|
| 0 | 22.0 | 2.0 | 0.000017 | 0.000017 | True | 11 | 3 | 1.0 |
Before a t-test, its assumption can be checked. A paired t-test assumes the differences are
normal, so a normality test on a paired question tests each pair's differences. A small p is evidence
against normality. describe gives the usual summary per group.
eeg.test("shapiro").table
| a | b | statistic | p | p_corrected | significant | n | |
|---|---|---|---|---|---|---|---|
| 0 | proposed | alternative | 0.977309 | 0.949354 | 0.949354 | False | 11 |
| 1 | proposed | baseline | 0.900973 | 0.083304 | 0.083304 | False | 16 |
eeg.describe().to_frame(pretty=True)
| method | n | Mean (SD) | Median [IQR] | Range | |
|---|---|---|---|---|---|
| 0 | alternative | 11 | 0.30 (0.02) | 0.30 [0.29, 0.31] | 0.28 to 0.33 |
| 1 | baseline | 16 | 0.25 (0.01) | 0.25 [0.24, 0.26] | 0.23 to 0.27 |
| 2 | proposed | 16 | 0.20 (0.02) | 0.20 [0.19, 0.21] | 0.16 to 0.23 |
Several pairs: one family¶
The question has two pairs (proposed vs baseline, proposed vs alternative). Two tests mean two chances of
a false positive, so their p values are corrected together: Holm by default. correct gives a new result
with another method; the original is left as it was.
r = eeg.test("wilcoxon", alternative="less")
r.correct("fdr_bh").table[["a", "b", "p", "p_corrected", "significant"]]
| a | b | p | p_corrected | significant | |
|---|---|---|---|---|---|
| 0 | proposed | alternative | 0.000488 | 0.000488 | True |
| 1 | proposed | baseline | 0.000015 | 0.000031 | True |
The figure of a test shows the units it compared, each subject's values joined by a line (a paired
design), and a bracket for each pair the test finds different after correction. annotate chooses the text:
here the stars, the effect size and how many subjects favour the reference (better / tied / worse).
fig, ax = r.plot(annotate=("stars", "effect", "counts"))
One family across several results¶
The main claim is tested once per sensor type: EEG, magnetometers and gradiometers. These are three
questions but one family of tests, so syn.correct pools every pair of every result before correcting.
Each corrected result remembers the family it was corrected in.
results = []
for sensors in ["eeg", "mag", "grad"]:
q = mc.compare(
"error",
by="method",
reference="proposed",
subset={"sensors": sensors},
reduce={"source": "median"},
higher_is_better=False,
title=f"Proposed vs other methods, {sensors.upper()}",
)
results.append(q.test("wilcoxon", alternative="less"))
corrected = syn.correct(results, "holm")
corrected[0].method["correction"]
{'method': 'holm',
'alpha': 0.05,
'family': 'results',
'size': 6,
'members': ['Proposed vs other methods, EEG',
'Proposed vs other methods, MAG',
'Proposed vs other methods, GRAD']}
Reading results from the reference's side¶
vs() keeps the pairs with the reference and orients them so that a positive effect means the
reference is better, using higher_is_better. The hypothesis is turned with it ("less" becomes
"greater"), so the p values don't change. The planted effect is in EEG and magnetometers, not in
gradiometers:
for result in corrected:
print(result.title)
print(result.vs()[["competitor", "alternative", "p_corrected", "significant", "hodges_lehmann"]], "\n")
Proposed vs other methods, EEG
competitor alternative p_corrected significant hodges_lehmann
0 alternative greater 0.001953 True 0.101011
1 baseline greater 0.000092 True 0.049625
Proposed vs other methods, MAG
competitor alternative p_corrected significant hodges_lehmann
0 alternative greater 0.001953 True 0.095035
1 baseline greater 0.000092 True 0.043895
Proposed vs other methods, GRAD
competitor alternative p_corrected significant hodges_lehmann
0 alternative greater 0.001953 True 0.092691
1 baseline greater 0.316086 False 0.002172
A forest plot puts the three sensor types side by side: one row per result and pair, the Hodges–Lehmann shift with its interval, oriented so that positive means the reference (proposed) is better.
fig, ax = mc.plot.estimate(corrected, effect_size="hodges_lehmann")
The data behind a test¶
differences gives the per-subject differences a paired test read, and the bootstrap test, which works
with any pair estimator, keeps its resampled values (draws). Its p value and interval come from the
same draws, so they always agree.
r.differences("proposed", "baseline").head()
| subject | proposed | baseline | difference | |
|---|---|---|---|---|
| 0 | s01 | 0.232176 | 0.272878 | -0.040703 |
| 1 | s02 | 0.209745 | 0.245476 | -0.035731 |
| 2 | s03 | 0.207087 | 0.245563 | -0.038476 |
| 3 | s04 | 0.189133 | 0.228448 | -0.039315 |
| 4 | s05 | 0.189120 | 0.250476 | -0.061356 |
study.plot.differences draws them, with the test's own estimate and interval:
fig, ax = mc.plot.differences(r)
boot = eeg.test("bootstrap", estimator="hodges_lehmann", n_boot=500)
boot.table[["a", "b", "p", "hodges_lehmann", "hodges_lehmann_ci_low", "hodges_lehmann_ci_high"]]
| a | b | p | hodges_lehmann | hodges_lehmann_ci_low | hodges_lehmann_ci_high | |
|---|---|---|---|---|---|---|
| 0 | proposed | alternative | 0.004983 | -0.101011 | -0.109927 | -0.089093 |
| 1 | proposed | baseline | 0.003992 | -0.049625 | -0.060367 | -0.041978 |
boot.draws().head()
| a | b | draw | value | |
|---|---|---|---|---|
| 0 | proposed | alternative | 0 | -0.097401 |
| 1 | proposed | alternative | 1 | -0.102395 |
| 2 | proposed | alternative | 2 | -0.098428 |
| 3 | proposed | alternative | 3 | -0.101511 |
| 4 | proposed | alternative | 4 | -0.099101 |
Serially correlated observations¶
Within one recording, time samples can be compared across methods (unit="time"). Neighbouring samples
are alike, so 500 samples carry much less information than 500 independent observations. A test that
assumes independence would give p values far too small: synaptome refuses, and reports the measured
effective sample size.
tp = syn.datasets.timepoint_pairing()
series = tp.compare("correlation", by="method", subset={"subject": "s01"}, unit="time", reference="improved")
try:
series.test("wilcoxon")
except ValueError as err:
print(err)
wilcoxon assumes independent observations, but the observations are positions along 'time', which are serially correlated (effective sample size 21.1 of 500 positions). Use ttest_ess (it uses the effective sample size), or pass assume_independent=True if the positions are independent (it is recorded in the result).
ttest_ess is made for this case: a paired t-test that uses the effective sample size. The
matched method has no planted difference with improved, and the test finds none.
series.test("ttest_ess").table
| a | b | statistic | df | n_eff | p | p_corrected | significant | n | mean_diff | mean_diff_ci_low | mean_diff_ci_high | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | improved | matched | 1.641007 | 27.659894 | 28.659894 | 0.112120 | 0.112120 | False | 500 | 0.024095 | -0.005998 | 0.054188 |
| 1 | improved | standard | 2.987090 | 20.063875 | 21.063875 | 0.007267 | 0.014534 | True | 500 | 0.041325 | 0.012473 | 0.070178 |
If you know the positions are independent (synaptome can't check it), assume_independent=True lets
any test run. The choice is recorded in the result and printed under its table. Here the assumption is
wrong, and it shows: matched now looks different from improved at p < .001, a false positive.
print(series.test("wilcoxon", assume_independent=True).to_markdown())
| Pair | W | p | p (holm) | n | Rank-biserial r | HL shift [95% CI] | | :--- | ---: | ---: | ---: | ---: | ---: | ---: | | improved − matched | 41803.00 | < .001 | < .001 | 500 | 0.33 | 0.02 [0.02, 0.03] | | improved − standard | 22721.00 | < .001 | < .001 | 500 | 0.64 | 0.04 [0.04, 0.05] | *Note.* Wilcoxon signed-rank test, two-sided. p values corrected with Holm across 2 comparisons. Positions along 'time' treated as independent observations (assume_independent=True). Not pre-specified.
Tables for a paper¶
to_frame(pretty=True) gives a table ready to read: p values in APA style (< .001), statistics
rounded, each effect with its interval in one cell, and n per row. to_markdown() adds a note on the
method, for a Quarto or Jupyter document; vs=True orients it on the reference.
corrected[0].to_frame(pretty=True)
| Pair | W | p | p (holm) | n | Rank-biserial r | HL shift [95% CI] | |
|---|---|---|---|---|---|---|---|
| 0 | proposed − alternative | 0.00 | < .001 | .002 | 11 | −1.00 | −0.10 [−0.11, −0.09] |
| 1 | proposed − baseline | 0.00 | < .001 | < .001 | 16 | −1.00 | −0.05 [−0.06, −0.04] |
Markdown(corrected[0].to_markdown(vs=True))
| Reference | Competitor | H1 | p | p (holm) | n | Rank-biserial r | HL shift [95% CI] |
|---|---|---|---|---|---|---|---|
| proposed | alternative | > 0.00 | < .001 | .002 | 11 | 1.00 | 0.10 [0.09, 0.11] |
| proposed | baseline | > 0.00 | < .001 | < .001 | 16 | 1.00 | 0.05 [0.04, 0.06] |
Note. Wilcoxon signed-rank test; H1 per row, oriented on the reference. p values corrected with Holm across 6 comparisons of 3 results. Oriented on the reference: positive differences mean the reference is better (lower is better). Not pre-specified.
Estimates format the same way:
Markdown(eeg.estimate("median").to_markdown())
| method | Median [95% CI] | n |
|---|---|---|
| alternative | 0.30 [0.29, 0.33] | 11 |
| baseline | 0.25 [0.24, 0.26] | 16 |
| proposed | 0.20 [0.19, 0.21] | 16 |
Note. Analytic intervals. Not pre-specified.
Next: 11 · Comparing along a dimension compares whole curves, with a cluster permutation test.