Exercise: from measurements to claims

Goal

By the end of this exercise you should be able to:

  • identify the unit of replication in an imaging experiment and analyse at that level,
  • compute a paired and an unpaired comparison of experiment means by hand, and explain why cells as n misleads,
  • explain a p-value and a confidence interval in words, and report an effect size with a CI,
  • count the expected false hits among many features and apply Bonferroni and Benjamini–Hochberg,
  • recognise a design that is confounded with a batch, and fix the design.

Do the pen-and-paper part first, then the two demos. Questions marked (math) are for those who want the derivations; the concept questions do not depend on them.

Background

Word Meaning
Standard error (SE) the wobble of an estimate between repeated experiments; SD/\sqrt n for a mean of n independent values
p-value how often chance alone (no effect) gives a result at least this extreme
95 % CI estimate \pm\, t_{0.975}\cdotSE (about ± 2 SE): the effects compatible with the data
Unit of replication the level at which the treatment was applied independently (experiment, animal, well)
Pseudoreplication treating dependent measurements (cells of one dish) as independent replicates
Paired design both conditions in every experiment; compare within each
Bonferroni reject when p \le \alpha/m (m tests)
Benjamini–Hochberg sort the p-values; the largest k with p_{(k)} \le k\,q/m; reject the k smallest

Critical values of the t-distribution, t_{0.975}: 4.30 (2 degrees of freedom), 2.78 (4 df), 2.07 (22 df), 1.96 (very many).

By hand

B1. Three experiments (days), four cells per condition each:

control treated
day 1 10, 11, 12, 11 12, 13, 14, 13
day 2 14, 15, 13, 14 15, 16, 14, 15
day 3 8, 9, 7, 8 10, 9, 11, 10
  1. Compute the experiment means, and the paired differences treated − control per day.
  2. Paired analysis: mean difference, SD of the differences, SE, t. Is it significant at 5 % (2 df)? Give the 95 % CI.
  3. Unpaired analysis of the experiment means (3 against 3): the two means, their SDs, the SE of the difference and t (4 df). Significant?
  4. Cells as n (12 against 12): the pooled two-sample test gives t \approx 1.65 (22 df). Now imagine 100 cells per experiment with the same spread: t becomes about 8.6. Which analyses are right, which is wrong, and why does the paired one win?

B2. 200 features, none with a real effect, each tested at \alpha = 0.05. (a) How many “hits” do you expect? (b) What is the probability of at least one (independent tests)? (c) What is the Bonferroni threshold?

B3. Ten sorted p-values: 0.001, 0.004, 0.012, 0.02, 0.03, 0.2, 0.4, 0.5, 0.7, 0.9. How many hits at \alpha = 0.05 (a) uncorrected, (b) with Bonferroni, (c) with Benjamini–Hochberg at q = 0.05?

B4. A plate: controls in columns 1–2, treatment A in columns 3–12, imaged from left to right over two hours while the focus drifts slowly. (a) What could go wrong? (b) How would you lay out and image the plate instead?

B5. Rewrite the sentence “Treated cells were significantly larger (p < 0.0001, n = 2400)” into a good report. What extra information do you need?

B1. (a) Control means 11, 14, 8; treated 13, 15, 10. Differences 2, 1, 2. (b) Mean difference 5/3 = 1.67, SD 0.58, SE = 0.58/\sqrt3 = 0.33, t = 5.0 > 4.30: significant (p = 0.038). 95 % CI: 1.67 \pm 4.30 \cdot 0.33 = 0.23 to 3.10. (c) Means 11 and 12.67 (difference 1.67); SDs 3.0 and 2.52; SE of the difference \sqrt{(9 + 6.33)/3} = 2.26; t = 0.74 < 2.78: not significant. (d) Right: the paired analysis (the design is paired: both conditions every day), and the unpaired analysis of experiment means (valid, but it throws away the pairing). Wrong: cells as n. Its t = 1.65 is not “nearly significant evidence”; it treats 12 cells as 12 independent replicates. With 100 cells per experiment the same data would give t \approx 8.6 and a tiny p-value, although nothing about the evidence changed (still three days). The paired analysis wins because the day-to-day differences (means 8 to 14) are much larger than the treatment effect; comparing within each day removes them.

B2. (a) 200 \cdot 0.05 = 10. (b) 1 - 0.95^{200} = 0.99996: practically certain. (c) 0.05/200 = 0.00025.

B3. (a) Five p-values \le 0.05. (b) Bonferroni threshold 0.005: 0.001 and 0.004, 2 hits. (c) BH lines k \cdot 0.005: 0.005, 0.010, 0.015, 0.020, 0.025, … The largest k with p_{(k)} \le its line: k = 4 (0.02 \le 0.020; 0.03 > 0.025, and no later p-value is below its line). 4 hits.

B4. (a) The treatment is confounded with the plate position (edge columns, evaporation) and with time (focus drift, bleaching, cells changing during two hours): any difference between columns 1–2 and the rest could be position or time, not treatment. (b) Spread controls over the whole plate (e.g. in every row, also in the middle), randomise or balance the positions of the conditions, avoid or fill the edge wells, and image in a randomised (or interleaved) order; replicate on several plates and days, with controls on every plate.

B5. For example: “Treated cells were 12 % larger than controls (95 % CI 4 % to 20 %; mean of per-experiment medians, n = 4 independent experiments, 600 cells each; paired t-test, p = 0.02).” Needed: the effect size and its CI; the unit of replication and its n (experiments, not cells); how values were aggregated; the test; the number of features tested if this was one of many (and the correction).

Math questions

B6. (math) In the two-level model y_{ec} = \mu + u_e + \varepsilon_{ec} (E experiments, m cells each, u_e \sim N(0, \sigma_u^2), \varepsilon_{ec} \sim N(0, \sigma^2), independent), derive the variance of the grand mean and the design effect 1 + (m-1)\rho with \rho = \sigma_u^2/(\sigma_u^2 + \sigma^2). What is n_{\text{eff}} for m = 500, \rho = 0.2, E = 3?

B7. (math) Derive the false-positive rate of the cells-as-n z-test under no effect, 2\,\Phi\big(-1.96/\sqrt{1 + (m-1)\rho}\big), and evaluate it for m = 10, 100, 1000 at \rho = 0.1.

B8. (math) Show that the paired difference of experiment means does not contain u_e. When does pairing lose power compared with the unpaired test of experiment means?

B9. (math) Prove that Bonferroni controls the family-wise error rate without any independence assumption. Show with B3 that Benjamini–Hochberg can reject a hypothesis with p > \alpha/m.

B6. \bar y_e = \mu + u_e + \bar\varepsilon_e with \text{Var}(\bar\varepsilon_e) = \sigma^2/m, so \text{Var}(\bar y_e) = \sigma_u^2 + \sigma^2/m. The grand mean \bar y = \frac1E\sum_e \bar y_e averages E independent experiment means: \text{Var}(\bar y) = \sigma_u^2/E + \sigma^2/(Em). If all Em cells were independent with variance \sigma_u^2 + \sigma^2, it would be (\sigma_u^2 + \sigma^2)/(Em). Ratio: \frac{m\sigma_u^2 + \sigma^2}{\sigma_u^2 + \sigma^2} = 1 + (m - 1)\frac{\sigma_u^2}{\sigma_u^2 + \sigma^2} = 1 + (m-1)\rho. With m = 500, \rho = 0.2: design effect 1 + 499 \cdot 0.2 = 100.8; n_{\text{eff}} = 1500/100.8 = 14.9 cells’ worth of information, close to the limit E/\rho = 15. 1500 cells from 3 experiments are worth about 15 independent cells.

B7. Under no effect, \bar y_1 - \bar y_0 has variance 2(\sigma_u^2/E + \sigma^2/(Em)) = \frac{2(\sigma_u^2 + \sigma^2)}{Em}\,(1 + (m-1)\rho) by B6. The cells-as-n statistic divides by \sqrt{2(\sigma_u^2 + \sigma^2)/(Em)}, so z \sim N(0, 1 + (m-1)\rho). It rejects when |z| > 1.96: P = 2\,P\big(Z > 1.96/\sqrt{1 + (m-1)\rho}\big) = 2\Phi\big(-1.96/\sqrt{1 + (m-1)\rho}\big). At \rho = 0.1: m = 10: \sqrt{1.9} = 1.38, 2\Phi(-1.42) = 0.155; m = 100: \sqrt{10.9} = 3.30, 2\Phi(-0.594) = 0.553; m = 1000: \sqrt{100.9} = 10.04, 2\Phi(-0.195) = 0.845.

B8. With both conditions in experiment e: \bar y_{e,T} = \mu + \tau + u_e + \bar\varepsilon_{e,T} and \bar y_{e,C} = \mu + u_e + \bar\varepsilon_{e,C}; the difference is \tau + \bar\varepsilon_{e,T} - \bar\varepsilon_{e,C}, without u_e, with variance 2\sigma^2/m instead of 2(\sigma_u^2 + \sigma^2/m). Pairing loses only when \sigma_u is about 0 (nothing to remove): then it has E - 1 degrees of freedom instead of 2E - 2, a larger critical value (t_{0.975} = 4.30 instead of 2.78 for E = 3), and slightly less power. It also loses if the “pairs” do not really share a day effect.

B9. Let N be the set of true null hypotheses, |N| \le m. Each null p-value is uniform, so P(p_i \le \alpha/m) = \alpha/m. P(\text{any false hit}) = P\big(\bigcup_{i \in N}\{p_i \le \alpha/m\}\big) \le \sum_{i \in N} \alpha/m \le \alpha, by the union bound, which needs no independence. In B3, BH rejects p = 0.012 and p = 0.02, both above \alpha/m = 0.005.

Explore: cells, experiments, pairs

A simulated experiment with two conditions: E experiments, m cells per experiment and condition, a day-to-day SD (shared by all cells of an experiment), a cell-to-cell SD and a true effect. Designs: independent (each experiment does one condition), paired (both conditions in every experiment), confounded (all treated experiments in a later batch, shifted by the batch shift). The SuperPlot shows cells as small dots coloured by experiment (at most 80 drawn per experiment) and experiment means as big markers. Three analyses: cells as n, experiment means as n, paired experiment means, each with the estimate, 95 % CI and p. The bars show the share of p < 0.05 over 1000 repetitions (the false-positive rate when the true effect is 0, the power otherwise). The last plot shows the SE of the effect against m: the true one, and what cells-as-n claims.

P1. Preset 1000 cells, no effect. Read the estimate, CI and p of the three analyses, and the shares of p < 0.05. Which analysis would you believe?

P2. Keep everything but set the cells per experiment to 10, then 100. Read the false-positive rate of cells as n. Compare with the formula of B7: here \rho = 1/(1 + 4) = 0.2.

P3. Preset more cells does not help (true effect 1, 1000 cells per experiment), then more experiments does (10 experiments of 20 cells). Compare the power of the experiments-as-n analysis, and look at the SE plot.

P4. Preset paired design wins. Compare the paired and the unpaired analysis of the same experiment means (p and CI), and their power over 1000 repetitions. What do the lines in the SuperPlot show?

P5. Preset confounded with the day: no treatment effect, but every treated experiment was done in a later batch. Read the estimate and the p-values. Increase the number of experiments to 10: does it help? What would?

P6. With the independent design and no effect, set the day-to-day SD to 0. What happens to the false-positive rate of cells as n? Why is this no excuse to analyse cells as n?

P1. Cells as n: estimate 0.65 [0.54, 0.75], p = 2.6 \cdot 10^{-31}. Experiments as n: the same estimate, CI −2.27 to 3.57, p = 0.49. Over 1000 repetitions with no effect, cells as n gives p < 0.05 in 89 % (88.5 %) of them, experiments as n in 4.0 %. Believe the experiment-level analysis: the estimate is the same, but only its CI reflects the day-to-day variation.

P2. m = 10: 25 % false positives (25.1 %); m = 100: 69 % (68.8 %); m = 1000 (P1): 89 %. The formula with \rho = 0.2: 0.24, 0.67 and 0.89. It fits well even with only 3 experiments per condition.

P3. 1000 cells per experiment, 3 experiments per condition: power 12.5 %. 10 experiments of 20 cells (far fewer cells in total): 46 %. In the SE plot the true SE flattens out at about 0.82 beyond ~100 cells (for 3 experiments), while cells as n claims 0.06 at 1000 cells.

P4. Paired: estimate 0.87, CI 0.30 to 1.45, p = 0.023. Unpaired experiment means: the same estimate, CI −1.90 to 3.64, p = 0.43. Power over 1000 repetitions: 84 % paired, 14 % unpaired. The lines join the control and treated means of each experiment: they all go up by about the same amount, although the experiments sit at very different levels. Pairing compares the slopes, not the levels.

P5. Estimate 2.53 (no real effect; the batch shift is 1.5 and this run is a little lucky), and even the experiment-level analysis gives p = 0.029. Over 1000 repetitions it rejects in about 21 % of them instead of 5 %, and the estimate is always biased by the batch shift. More experiments do not help: with 10 experiments the estimate is 1.51 (the batch shift, now measured precisely), p = 0.005, and the experiment-level test rejects in 88 % of repetitions. More data make the batch difference more certain, not the treatment effect. Only the design helps: every batch must contain both conditions (or pair within a batch).

P6. With day-to-day SD 0, cells as n rejects in about 4 % of the null experiments, like the experiment-level test: cells are then really independent. But you cannot know beforehand that days do not differ (in practice they nearly always do), and aggregating first costs little when they do not. Analyse at the level of the unit of replication.

Explore: many features on a plate

A 384-well plate (or 96), half treated, half control, with m features measured per well. A share of the features carries a real treatment effect (in SDs). Plate effects: a left–right and a top–bottom gradient and edge wells, which each feature feels with its own sensitivity. Layouts: treated on the left half, random, or random per row. Left: the plate map of one feature (treated wells outlined). Middle: the histogram of all p-values (grey: the null features). Right: the sorted p-values against their rank on log axes, with the uncorrected line \alpha, the Bonferroni line \alpha/m and the BH line k\alpha/m; green dots are real features, red dots null features (known from the simulation). The table counts the hits of each method and how many are false.

F1. Preset 300 null features. Read the hits of each method. Describe the histogram of p-values.

F2. Set m to 50 and to 500. How many uncorrected hits, and how many does \alpha m predict?

F3. Preset 10 % real: BH finds most. Read hits, false hits and true hits for each method. Where are the real features in the rank plot?

F4. Preset gradient + treated on the left. Read the hits. Look at the plate map of the smallest-p feature. Why does Bonferroni not help?

F5. Preset randomised layout fixes it (the same gradient). Read the hits. Then try random per row.

F6. Preset edge wells, row-blocked layout: edge effect, a top–bottom gradient and 10 % real features, with a balanced layout. How do the three methods do? Then untick 384 wells (96 wells, same settings): what happens, and why?

F1. Uncorrected 15 hits (all false), Bonferroni 0, BH 0. The histogram is flat at about m/20 = 15 per bin: under no effect every p-value is equally likely.

F2. m = 50: 1 hit (\alpha m = 2.5); m = 500: 27 hits (\alpha m = 25). The number of false hits grows in proportion to the number of features.

F3. 30 real features. Uncorrected: 43 hits, 14 false, 29 true. Bonferroni: 13 hits, all true. BH: 24 hits, 2 false, 22 true. The real features have the smallest p-values (green dots at low ranks, below the BH line); the null ones continue as the uniform curve. BH finds most real features with few false ones; Bonferroni is safe but misses more than half.

F4. 281 of 300 features “significant” uncorrected, 249 after Bonferroni, 281 with BH; none of them real. The plate map shows the left–right gradient, and the treated wells are all on one side. Every feature that feels the gradient differs between the halves; the p-values are tiny, so no correction for the number of tests can remove them. The problem is bias (confounding), not chance.

F5. Random layout: 37 uncorrected hits, 0 after Bonferroni or BH. The gradient now hits both conditions alike on average; it only adds noise (and a little chance imbalance, which pushes many features together, hence 37 instead of 15 uncorrected). Random per row: 13 uncorrected hits, 0 corrected.

F6. 384 wells: uncorrected 32 hits (8 false), Bonferroni 8 (all true), BH 19 (1 false). With 96 wells: uncorrected 19 hits (6 false, 13 true); Bonferroni and BH find only 1. With a quarter of the wells the SE of each comparison doubles, and an effect of 0.35 SD hardly stands out from 300 tests.

Code

Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in evaluation/src/replicates/exercise.rs.

C1. mean_sd(x): the mean and the standard deviation (divide by n - 1). The check is B1.

C2. paired_t(control, treated) and unpaired_t(x, y): the two t-tests of the unit. Paired: a one-sample test on the differences, SE = SD/\sqrt n, n - 1 degrees of freedom. Unpaired: SE = \sqrt{s_x^2/n_x + s_y^2/n_y}, n_x + n_y - 2 degrees of freedom. The result knows whether it is significant and its 95 % interval (a table of t_{0.975} is in the download). The checks are B1(b), B1(c) and B1(d) (cells as n: t \approx 1.65).

C3. false_positive_rates(experiments, cells, sd_day, sd_cell, runs, seed): simulate studies with no treatment effect and count how often each analysis finds one:

repeat runs times:
    for each condition, for each of its experiments:
        day = sd_day · (a random normal number)            # shared by all cells of the experiment
        cells: day + sd_cell · (a random normal number), each
        experiment mean = mean of its cells
    test (a) all cells as n, (b) the experiment means as n; count the significant ones
return the two shares

The check is the lecture’s number: 3 experiments of 100 cells, day SD 1, cell SD 2: about 69 % for cells as n, about 5 % for experiment means.

C4. bonferroni(p, alpha) and benjamini_hochberg(p, q): the number of hits.

Benjamini–Hochberg:
sort the m p-values: p(1) ≤ p(2) ≤ … ≤ p(m)
k = the largest k with p(k) ≤ k · q / m      (0 if there is none)
the k smallest p-values are hits

The check is B3.

Then cargo run -p evaluation -- replicates. The program first images six dishes of synthetic nuclei (out/replicates/experiment.png): control on days 1–3 (top row), treated on days 4–6 (bottom row). Every day has its own brightness (staining, lamp); the treatment does nothing. It measures the mean intensity of every nucleus and tests treated against control with cells as n and with experiment means as n. Then it prints the false-positive rates of C3 for 1 to 1000 cells per experiment, and the hits of the three rules for 300 simulated features, 30 of them with a real effect.

C5. Read the two analyses of the six dishes. Which one finds an “effect”, and which would you believe? Where does the effect come from?

C6. Compare the false-positive rates with the formula of B7, for \rho = 1/(1 + 4) = 0.2 (and with demo question P2). Why do the two analyses agree for one cell per experiment?

C7. Read the hits of the three rules for the 300 features. How many false hits does each make, and how many of the 30 real features does it find?

The model solution is evaluation/src/replicates/solution.rs in the download.

C5. Cells as n (about 117 nuclei per condition): effect +0.060, 95 % CI +0.023 to +0.096, t = 3.19 with 233 degrees of freedom: “significant”. Experiment means as n: the same effect, but CI -0.226 to +0.346, t = 0.58 with 4 degrees of freedom: not significant. Believe the second. The experiment means vary from 0.590 to 0.864 between days, because every day has its own brightness; the treated days happen to be a little brighter on average. Cells as n treats 117 nuclei of three dishes as 117 independent replicates and takes this difference between days for a treatment effect.

C6. Cells as n: 25.9 % at 10 cells, 69.6 % at 100, 89.4 % at 1000; the formula gives 2\Phi(-1.96/\sqrt{1 + 9 \cdot 0.2}) = 0.24, 0.67 and 0.89. The experiment means stay at 5.2–5.6 %, as they should. With one cell per experiment the cells are the experiment means: both analyses are the same test (5.3 %).

C7. Uncorrected: 43 hits, of which 24 real and 19 false (about 0.05 \cdot 270 = 13.5 false hits expected; this run is a little unlucky). Bonferroni: 7 hits, all real, but 23 of the 30 real features missed. Benjamini–Hochberg: 11 hits, all real: more found than Bonferroni, still no false one here. The same picture as demo question F3.

Where this leads

The evaluation of a method (units 1–3) and the analysis of an experiment (this unit) share one lesson: count what is independent, and decide before you look. The modern deep learning day reuses the object matching and AP of unit 1 for detection, and the comparison of cell networks on the networks-for-cells day uses all four units: matching, leakage, and images rather than nuclei as the independent unit.