08 · Questions¶
You will learn how to ask a question of a study: what is compared, on which data. A question holds no numbers and no choice of test; it works out the design (paired or independent, the groups, the pairs, how many units) and the table every later test, estimate and plot will read. You will see how synaptome tells paired from independent comparisons on its own, why it refuses to guess reductions, and how missing values are handled pair by pair.
You need 02 · Units and variables (units vs dimensions),
04 · Selecting and reshaping (reduce=) and
07 · Toy studies.
import synaptome as syn
A first question¶
In the case-control study, patients and controls each have one score. The question "do the groups
differ in score?" is written with compare:
study = syn.datasets.case_control()
q = study.compare("score", by="group")
q
Compare('score', by='group', higher_is_better=True) -- 'score by group'
q.design says what will be compared. group is stored at the subject level: each subject belongs to
one group, so the two groups are made of different subjects. In statistical words, group is a
between-subject factor, and the comparison is independent.
q.design
Design: compare 'score' by 'group' (independent) factor: 'group', a between-subject factor (a variable at level 'subject'); 2 levels: control, patient unit: subject, 60 observed pairs: control - patient (n=60) outcome: 'score', interval scale
q.data is the analysis table: one row per subject, with its group and its score. Tests, estimates and
plots will all read this exact table, so they can never disagree about the data.
q.data.head()
| subject | group | score | |
|---|---|---|---|
| 0 | s01 | control | 45.635648 |
| 1 | s02 | control | 38.301981 |
| 2 | s03 | control | 67.393679 |
| 3 | s04 | control | 45.040893 |
| 4 | s05 | control | 53.289696 |
Paired or independent: read from where the factor lives¶
In the crossover trial, condition is a dimension of score: every subject was measured under both
conditions. It is a within-subject factor, and the comparison is paired. You never pass
paired=True: synaptome reads it from where the factor is stored.
crossover = syn.datasets.crossover()
crossover.compare("score", by="condition").design
Design: compare 'score' by 'condition' (paired) factor: 'condition', a within-subject factor (a dimension of 'score'); 2 levels: drug, placebo unit: subject, 24 observed pairs: drug - placebo (n=24) outcome: 'score', interval scale
| Statistical term | In synaptome | Comparison |
|---|---|---|
| between-unit factor | a variable at the unit's level (group at subject) |
independent |
| within-unit factor | a dimension of the outcome (condition of score) |
paired |
| levels of a factor | the variable's categories, or the dimension's coordinates | the groups compared |
Reductions are never guessed¶
In the ERP study, erp is measured on every trial, and trials sit inside subjects. Comparing groups
trial by trial would count each trial as an independent observation, although trials of one subject share
its group (pseudo-replication). synaptome refuses, and says how to fix it:
erp = syn.datasets.repeated_trials()
try:
print(erp.compare("erp", by="group", subset={"time": 300}).design)
except ValueError as err:
print(err)
'group' is a subject-level factor: all trials of one subject share it, so they aren't independent observations of it. Compare subjects: reduce={'trial': 'mean'}.
reduce={"trial": "mean"} averages each subject's trials (its evoked response). Now the subjects are
the units, one value each:
q = erp.compare("erp", by="group", subset={"time": 300}, reduce={"trial": "mean"})
q.design
Design: compare 'erp' by 'group' (independent) factor: 'group', a between-subject factor (a variable at level 'subject'); 2 levels: control, patient unit: subject, 24 observed pairs: control - patient (n=24) outcome: 'erp', interval scale
The same rule applies to dimensions the question doesn't use. In the method comparison, error has
three dimensions: method (the factor), sensors and source. Each of the others must be narrowed with
subset or collapsed with reduce:
mc = syn.datasets.method_comparison()
try:
print(mc.compare("error", by="method", reduce={"source": "median"}).design)
except ValueError as err:
print(err)
'error' has dimension 'sensors' with 3 coordinates ('eeg', 'grad', 'mag'), which the question doesn't use: keep one with subset={'sensors': 'eeg'}, or reduce it, e.g. reduce={'sensors': 'mean'}.
Complete cases, pair by pair¶
With EEG only, and one median error per subject: reference puts proposed first in every pair, and
higher_is_better=False records that a lower error is better (results will use it to say who wins).
q = mc.compare(
"error",
by="method",
reference="proposed",
subset={"sensors": "eeg"},
reduce={"source": "median"},
higher_is_better=False,
title="Proposed vs other methods, EEG",
)
q.design
Design: compare 'error' by 'method' (paired) factor: 'method', a within-subject factor (a dimension of 'error'); 3 levels: alternative, baseline, proposed unit: subject, 16 observed pairs: proposed - alternative (n=11), proposed - baseline (n=16) lower is better outcome: 'error', ratio scale
alternative failed in 5 subjects. Each pair keeps its own complete cases: 16 subjects compare
proposed with baseline, 11 compare it with alternative. Nobody is dropped from a pair they are
complete for. pair_data shows the table of one pair:
q.pair_data("proposed", "alternative").head()
| subject | proposed | alternative | |
|---|---|---|---|
| 0 | s01 | 0.232176 | 0.325607 |
| 1 | s02 | 0.209745 | 0.297308 |
| 2 | s03 | 0.207087 | 0.291157 |
| 3 | s04 | 0.189133 | 0.297430 |
| 4 | s06 | 0.164183 | 0.279457 |
A question is answered as many times as you like, and every answer reads this same table. Two estimates of the same pairs, one per pair each (tutorial 09 explains them):
q.estimate("hodges_lehmann").table
| a | b | estimate | ci_low | ci_high | n | |
|---|---|---|---|---|---|---|
| 0 | proposed | alternative | -0.101011 | -0.111785 | -0.088651 | 11 |
| 1 | proposed | baseline | -0.049625 | -0.059307 | -0.040703 | 16 |
q.estimate("rank_biserial", ci="bootstrap", n_boot=500).table
| a | b | estimate | ci_low | ci_high | bias | se | bias_se_ratio | z0 | n | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | proposed | alternative | -1.0 | -1.0 | -1.0 | 0.0 | 0.0 | NaN | 0.0 | 11 |
| 1 | proposed | baseline | -1.0 | -1.0 | -1.0 | 0.0 | 0.0 | NaN | 0.0 | 16 |
Tests read it too: a Wilcoxon test and a paired t-test of the same pairs, each with its own effect sizes (tutorial 10 explains them).
q.test("wilcoxon", alternative="less").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 |
q.test("paired_ttest", alternative="less").table
| a | b | statistic | df | p | p_corrected | significant | n | mean_diff | mean_diff_ci_low | mean_diff_ci_high | hedges_gz | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | proposed | alternative | -18.698910 | 10.0 | 2.069036e-09 | 2.069036e-09 | True | 11 | -0.100769 | -0.112776 | -0.088761 | -5.202378 |
| 1 | proposed | baseline | -13.382582 | 15.0 | 4.803770e-10 | 9.607540e-10 | True | 16 | -0.050673 | -0.058744 | -0.042603 | -3.175043 |
Crossed designs¶
In the crossed study, condition belongs to the stimulus, and every subject sees every stimulus. Seen
from the subjects, condition varies within each of them: paired. Seen from the stimuli, each stimulus
has one condition: independent. The unit says which units are the independent observations:
crossed = syn.datasets.crossed()
by_subject = crossed.compare("rt", by="condition", reduce={"trial": "mean"}, unit="subject")
by_stimulus = crossed.compare("rt", by="condition", reduce={"trial": "mean"}, unit="stimulus")
by_subject.design.paired, by_stimulus.design.paired
(True, False)
Curves, samples in time, distributions¶
compare_along keeps an ordered dimension unreduced, to compare whole curves (here, weight week by week):
weights = syn.datasets.longitudinal()
weights.compare_along("weight", by="group", along="week").design
Design: compare_along 'weight' by 'group' (independent) factor: 'group', a between-subject factor (a variable at level 'subject'); 2 levels: control, treated unit: subject, 30 observed along: 'week', 7 coordinates pairs: control - treated (n=19) outcome: 'weight', ratio scale
Only subjects weighed at every visit can enter a comparison of whole curves: 19 of the 30 here, since
some visits are missing. To keep more subjects, compare fewer weeks (subset={"week": [0, 6, 12]}) or
one number per subject (reduce={"week": "slope"}).
unit= can also be an ordered dimension: within one recording, time samples are paired across methods.
The design notes that neighbouring samples are serially correlated, which a test must account for (tutorial 10 shows how):
tp = syn.datasets.timepoint_pairing()
tp.compare("correlation", by="method", subset={"subject": "s01"}, unit="time").design
Design: compare 'correlation' by 'method' (paired) factor: 'method', a dimension of 'correlation', paired on the coordinates of 'time'; 3 levels: improved, matched, standard unit: positions along 'time' (serially correlated), 500 observed pairs: improved - matched (n=500), improved - standard (n=500), matched - standard (n=500) outcome: 'correlation', interval scale
distribution asks how an outcome is spread, overall or per group:
study.distribution("score", by="group").design
Design: distribution 'score' by 'group' (distribution) factor: 'group', a between-subject factor (a variable at level 'subject'); 2 levels: control, patient unit: subject, 60 observed n: control=30, patient=30 outcome: 'score', interval scale
Recording a question¶
A question can be written down as plain data, with no values in it, and asked again later, on this
study or on a new version of it. data_key fingerprints the data the question read: the same question
on the same data gives the same key.
spec = q.to_dict()
spec
{'kind': 'compare',
'title': 'Proposed vs other methods, EEG',
'description': '',
'outcome': 'error',
'subset': {'sensors': 'eeg'},
'reduce': {'source': 'median'},
'unit': None,
'status': 'exploratory',
'specified_at': '2026-11-27T08:23:13.492838+00:00',
'by': 'method',
'reference': 'proposed',
'pairs': None,
'higher_is_better': False}
again = mc.question(spec)
again.data_key == q.data_key
True
Next: 09 · Estimates answers questions with estimates: how large is each difference, with a confidence interval.