12 · Pre-registration¶
You will learn how to write down the questions of a study before its data arrive, keep them in the study file, and get results that say whether their question was fixed before the data. You will also see how a pre-specified question is amended, and why that is logged.
You need 05 · Save and share and 10 · Tests and corrections.
import tempfile
from pathlib import Path
import pandas as pd
import synaptome as syn
A study with no data yet¶
The plan comes first: the units and the variables, defined without values. A question only needs the names it uses, so it can be written now.
study = syn.Study("Weight trial")
study.units.define("subject")
study.variables.define("group", level="subject", dtype="category", categories=("control", "treated"))
study.variables.define("weight", level="subject", dims=["week"], measurement_unit="kg", scale="ratio")
q = study.compare(
"weight",
by="group",
reduce={"week": "slope"},
reference="treated",
higher_is_better=False,
title="Slope by group",
description="Does the diet lower the 12-week weight slope?",
)
add_question registers it in the protocol. synaptome records the time itself: it can't be set by hand,
so it can later vouch for when the question was fixed.
study.protocol.add_question(q, status="primary")
study.protocol.registrations()
| title | kind | outcome | status | registered_at | |
|---|---|---|---|---|---|
| 0 | Slope by group | compare | weight | primary | 2026-11-27 08:23:26.093892+00:00 |
The registered questions are saved with the study, one small JSON file each:
tmp = tempfile.TemporaryDirectory()
path = study.save(Path(tmp.name) / "trial.syn")
syn.peek(path)["questions"]
{'Slope by group': 'primary'}
The data arrive¶
Weeks later, the study is loaded and the measurements are added. Here they come from the toy longitudinal study, renamed to match the plan.
study = syn.load(path)
toy = syn.datasets.longitudinal()
study.add_units("subject", toy.to_df("group"))
study.add("weight", toy.to_df("weight"))
study
Study('Weight trial'; levels: subject(30); variables: 2)
The registered question is rebuilt on the current data. Its result carries a timing: the question was registered before the first value of every variable it reads, so it is "before data".
registered = study.protocol.questions["Slope by group"]
r = registered.test("welch")
r.timing
{'timing': 'before data',
'status': 'primary',
'registered_at': '2026-11-27T08:23:26.093892+00:00',
'first_data_at': '2026-11-27T08:23:26.162061+00:00',
'variables': ['group', 'weight']}
The timing is printed under every formatted table:
print(r.to_markdown(vs=True))
| Reference | Competitor | df | p | n | Mean difference [95% CI] | Hedges' g | | :--- | :--- | ---: | ---: | ---: | ---: | ---: | | treated | control | 24.75 | < .001 | 15 / 15 | 0.69 [0.60, 0.78] | 5.68 | *Note.* Welch's t-test, two-sided. Oriented on the reference: positive differences mean the reference is better (lower is better). Pre-specified (primary) on 2026-11-27, before data.
Questions asked afterwards¶
A question asked now, not in the protocol, is "not pre-specified". So is a registered title asked differently: here, only the first eight weeks. Rewording the description doesn't count; changing what is analysed does.
later = study.compare(
"weight",
by="group",
reduce={"week": "slope"},
subset={"week": [0, 2, 4, 6, 8]},
reference="treated",
higher_is_better=False,
title="Slope by group",
)
later.test("welch").timing["timing"]
'not pre-specified'
Amending a pre-specified question¶
Plans change. Replacing a registered question needs a reason; the old version stays in the amendment log, and the new one gets a new registration time. Registered after the data, it no longer shows "before data".
study.protocol.add_question(later, replace=True, reason="weeks 10-12 were lost to a scale fault")
study.protocol.questions["Slope by group"].test("welch").timing["timing"]
'after data'
pd.DataFrame(study.protocol.amendments)[["action", "title", "reason", "at"]]
| action | title | reason | at | |
|---|---|---|---|---|
| 0 | replace | Slope by group | weeks 10-12 were lost to a scale fault | 2026-11-27T08:23:26.262707+00:00 |
tmp.cleanup()
Next: 13 · Plotting draws a study's values.