09 · Estimates¶
You will learn how to answer "how large?": the size of a difference, or a typical value per group, with a confidence interval. Estimates are separate from tests: they make no hypothesis, and you can ask as many as you like of one question. synaptome refuses estimates that don't suit the data and says which ones do. You will also see how a result is checked against the study, and saved.
You need 08 · Questions.
import tempfile
from pathlib import Path
import synaptome as syn
A difference, with its interval¶
In the crossover trial, each subject took the drug and the placebo. mean_diff is the mean of the
differences, drug − placebo (reference puts the drug first). The interval says which values of the
true difference are compatible with these data, at 95% confidence.
crossover = syn.datasets.crossover()
q = crossover.compare("score", by="condition", reference="drug")
q.estimate("mean_diff")
EstimateResult: score by condition estimator=mean_diff, family=pair, design=paired, ci=analytic, confidence=0.95 timing: not pre-specified a b estimate ci_low ci_high n drug placebo 4.964068 3.004419 6.923718 24
The planted effect is 5 points: it lies inside the interval. One question can take any number of
estimates. A standardized one, hedges_gz (the difference in standard deviations of the differences),
has no formula for its interval, so it is computed by bootstrap: the data are resampled many times. The
seed is fixed (and recorded), so the same call always gives the same interval.
q.estimate("hedges_gz", ci="bootstrap", n_boot=1000)
EstimateResult: score by condition estimator=hedges_gz, family=pair, design=paired, ci=bootstrap, confidence=0.95, interval=bca, n_boot=1000, seed=0 timing: not pre-specified a b estimate ci_low ci_high bias se bias_se_ratio z0 n drug placebo 1.034323 0.522808 1.605243 0.03619 0.282943 0.127906 -0.037608 24
The extra columns describe the bootstrap: bias and se of the resampled values. A bias_se_ratio
above about 0.25 means the interval should be read with care.
confidence sets the coverage:
q.estimate("mean_diff", confidence=0.99).table
| a | b | estimate | ci_low | ci_high | n | |
|---|---|---|---|---|---|---|
| 0 | drug | placebo | 4.964068 | 2.304665 | 7.623471 | 24 |
Values per group¶
mean, median and proportion give one value per group. In the method comparison, the median error of
each method, for EEG:
mc = syn.datasets.method_comparison()
errors = mc.compare(
"error",
by="method",
reference="proposed",
subset={"sensors": "eeg"},
reduce={"source": "median"},
higher_is_better=False,
)
errors.estimate("median").table
| method | estimate | ci_low | ci_high | n | |
|---|---|---|---|---|---|
| 0 | alternative | 0.297308 | 0.285259 | 0.325607 | 11 |
| 1 | baseline | 0.249244 | 0.236664 | 0.263408 | 16 |
| 2 | proposed | 0.197275 | 0.189120 | 0.209745 | 16 |
Refused estimates¶
Each estimator states what it needs. A difference of means needs values whose differences mean something (an interval or ratio scale); a 1–5 rating is only ordinal. A Cohen's d is for independent groups; a paired design has its own version. synaptome refuses, and lists the estimators that fit:
try:
crossover.compare("score", by="condition").estimate("cohens_d")
except ValueError as err:
print(err)
cohens_d is for independent comparisons, but 'condition' is a within-subject factor (paired). Estimators for this design: ['mean_diff', 'hodges_lehmann', 'cohens_dz', 'hedges_gz', 'rank_biserial'].
try:
q.estimate("cohens_dz")
except ValueError as err:
print(err)
cohens_dz has no analytic interval: use ci='bootstrap' (seeded), or ci=None.
Who is better: vs()¶
Each row of a pair estimate is a − b. vs() turns the rows around the reference so that positive
means the reference is better, using higher_is_better. For errors, lower is better, so a positive
value below says that proposed has the smaller error:
hl = errors.estimate("hodges_lehmann")
hl.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 |
hl.vs()
| reference | competitor | estimate | ci_low | ci_high | n | |
|---|---|---|---|---|---|---|
| 0 | proposed | alternative | 0.101011 | 0.088651 | 0.111785 | 11 |
| 1 | proposed | baseline | 0.049625 | 0.040703 | 0.059307 | 16 |
Note the different n: alternative failed in 5 subjects, so its pair has 11 complete subjects.
Upstream bootstrap draws¶
In the simulation benchmark, each run's accuracy also comes with bootstrap draws computed upstream (a
replicate variable, tutorial 02). With ci="bootstrap", synaptome uses them instead of resampling, and
records it:
bench = syn.datasets.simulation_benchmark()
acc = bench.compare("accuracy", by="method", reference="wcset", subset={"snr": 10}, reduce={"run": "mean"})
upstream = acc.estimate("mean_diff", ci="bootstrap", interval="percentile")
upstream.method["replicates"], upstream.table[["a", "b", "estimate", "ci_low", "ci_high"]]
('accuracy_boot',
a b estimate ci_low ci_high
0 wcset a 0.033463 0.021333 0.047083
1 wcset b 0.001195 -0.010000 0.011917)
Checking and saving a result¶
A result is a value: the study never stores it. It carries the fingerprint of the data it read, so it can be checked against the study at any time. Change a value, and the check fails:
crossover.verify(hl), mc.verify(hl)
(False, True)
changed = mc.copy()
changed.add(
"error",
changed.to_df("error").head(1).assign(error=0.0),
overwrite=True,
)
changed.verify(hl)
False
A result can be saved on its own, as one .synr file, and loaded back with its study:
tmp = tempfile.TemporaryDirectory()
path = hl.save(Path(tmp.name) / "proposed_vs_others.synr")
again = syn.load_result(path, study=mc)
again.table.equals(hl.table), mc.verify(again)
(True, True)
tmp.cleanup()
Next: 10 · Tests and corrections adds hypothesis tests, corrections for multiple comparisons and formatted tables.