11 · Comparing along a dimension¶
You will learn how to compare whole curves (a value per week, per SNR, per depth bin) without one test per point: estimates per coordinate, the cluster permutation test, its two ways of correcting for several pairs, and how to read and report where curves differ.
You need 08 · Questions and 10 · Tests and corrections.
from IPython.display import Markdown
import synaptome as syn
A curve per subject¶
In the longitudinal trial, every subject is weighed every two weeks. compare_along keeps week as it is,
so each subject contributes a whole curve. Only subjects weighed at every visit enter the comparison.
trial = syn.datasets.longitudinal()
q = trial.compare_along("weight", by="group", along="week", reference="treated")
q.design.n_per_pair
{('treated', 'control'): 19}
describe summarizes the analysis table, here per group and per week:
q.describe().table[["group", "week", "n", "mean", "sd"]].head(8)
| group | week | n | mean | sd | |
|---|---|---|---|---|---|
| 0 | control | 0 | 15 | 78.951053 | 5.093330 |
| 1 | control | 2 | 15 | 78.399822 | 4.535024 |
| 2 | control | 4 | 12 | 78.996965 | 5.233049 |
| 3 | control | 6 | 14 | 79.296056 | 4.253314 |
| 4 | control | 8 | 15 | 78.897192 | 4.462070 |
| 5 | control | 10 | 14 | 80.192332 | 3.944746 |
| 6 | control | 12 | 12 | 79.525098 | 3.174407 |
| 7 | treated | 0 | 15 | 79.486566 | 3.367540 |
An estimate is computed at every coordinate: the treated − control difference, week by week, with its interval. It shows how large, but seven intervals are seven chances of a false alarm.
q.estimate("mean_diff").table[["week", "estimate", "ci_low", "ci_high"]]
| week | estimate | ci_low | ci_high | |
|---|---|---|---|---|
| 0 | 0 | -0.345217 | -3.656916 | 2.966482 |
| 1 | 2 | 0.440803 | -2.394292 | 3.275898 |
| 2 | 4 | 0.551818 | -3.041345 | 4.144981 |
| 3 | 6 | -2.017220 | -4.651441 | 0.617002 |
| 4 | 8 | -3.113125 | -6.606837 | 0.380588 |
| 5 | 10 | -5.190860 | -8.203522 | -2.178199 |
| 6 | 12 | -8.021775 | -11.114865 | -4.928685 |
The cluster permutation test¶
The cluster test asks one question for the whole curve: do the groups differ anywhere along week?
Neighbouring weeks whose t statistic passes a threshold form a cluster; each cluster's mass (the sum of
its t values) is compared with the largest mass found after shuffling the group labels many times. This
controls the false-positive rate over the whole axis. The permutations are seeded, so the result is
reproducible.
clusters = q.test("cluster_permutation", n_permutations=1000)
clusters.table
| a | b | start | stop | n_coordinates | mass | p | p_corrected | significant | n_a | n_b | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | treated | control | 10 | 12 | 2 | -8.756924 | 0.026973 | 0.026973 | True | 11 | 8 |
The planted effect starts at week 6 and grows; the cluster covers the weeks where it is largest. The
mass is negative: the treated group weighs less. verdict gives one row per pair, with where it differs:
clusters.verdict()
| a | b | n_clusters | p_min | p_corrected | significant | where | |
|---|---|---|---|---|---|---|---|
| 0 | treated | control | 1 | 0.026973 | 0.026973 | True | [(10, 12)] |
The figure shows each group's mean curve with its 95% band, and under them a bar where the test found a cluster, coloured as the group that is higher there:
fig, ax = clusters.plot()
curve gives the t statistic at every week, the numbers the clusters are made from:
clusters.curve()
| a | b | week | t | |
|---|---|---|---|---|
| 0 | treated | control | 0 | -0.213360 |
| 1 | treated | control | 2 | 0.307659 |
| 2 | treated | control | 4 | 0.304276 |
| 3 | treated | control | 6 | -1.510436 |
| 4 | treated | control | 8 | -1.758442 |
| 5 | treated | control | 10 | -3.594753 |
| 6 | treated | control | 12 | -5.162171 |
Paired curves and several pairs¶
In the simulation benchmark, every method runs on the same regions at every SNR: a paired comparison,
tested by flipping the sign of each region's difference curve. With three methods, each compared with
wcset, there are two pairs.
bench = syn.datasets.simulation_benchmark()
curves = bench.compare_along("accuracy", by="method", along="snr", reduce={"run": "mean"}, reference="wcset")
paired = curves.test("cluster_permutation", n_permutations=1000)
paired.verdict()
| a | b | n_clusters | p_min | p_corrected | significant | where | |
|---|---|---|---|---|---|---|---|
| 0 | wcset | a | 1 | 0.001998 | 0.001998 | True | [(0.0, 20.0)] |
| 1 | wcset | b | 0 | 1.000000 | NaN | False | [] |
Two pairs need a correction. By default every cluster of every pair is corrected together
(family="cluster"). family="pair" corrects one p value per pair instead (its smallest cluster p, 1 when
it has no cluster), so a pair without clusters still counts in the family:
paired.correct("holm", family="pair").verdict()
| a | b | n_clusters | p_min | p_corrected | significant | where | |
|---|---|---|---|---|---|---|---|
| 0 | wcset | a | 1 | 0.001998 | 0.003996 | True | [(0.0, 20.0)] |
| 1 | wcset | b | 0 | 1.000000 | 1.000000 | False | [] |
study.plot.differences draws the paired difference curves, wcset minus each competitor:
fig, ax = bench.plot.differences(paired)
Reporting¶
Formatted tables work as for every result: one row per cluster, with the span of coordinates it covers, and a note on the method.
Markdown(paired.to_markdown())
| Pair | snr | Mass | p | n |
|---|---|---|---|---|
| wcset − a | 0–20 | 25.46 | .002 | 12 |
Note. Cluster permutation test along 'snr' (1000 sign flips, seed 0; clusters formed at cluster_alpha = .050), two-sided. Mass: sum of the t values of the cluster. No cluster formed for wcset − b. Not pre-specified.
Next: 12 · Pre-registration records questions before the data arrive, so results can show they were planned.