# Statistical test chooser, correction of 11 November 2026: false positives with several cells per animal
# in a within-animal design (both conditions in every mouse), with no true effect.
# 1,000 simulated experiments: 5 mice, 15 cells per mouse in each condition; mouse SD 1, mouse-by-condition SD 0.5,
# cell SD 1. Compares a mixed model with a random intercept per mouse only (the earlier first step) with a paired
# t-test on the per-mouse, per-condition means (the current first step).
# R 4.3.3, lmerTest 3.1-3. Output: within-animal-simulation-output.txt
suppressMessages(library(lmerTest))
set.seed(1); R <- 1000; p_ri <- p_pt <- numeric(R)
for (r in 1:R) {
  m <- 5; k <- 15
  d <- expand.grid(cell = 1:k, cond = c("veh", "drug"), mouse = factor(1:m))
  mu <- rnorm(m, 0, 1)[d$mouse]; mc <- rnorm(2 * m, 0, 0.5)[interaction(d$mouse, d$cond)]
  d$y <- mu + mc + rnorm(nrow(d), 0, 1)
  f <- suppressMessages(lmer(y ~ cond + (1 | mouse), data = d))
  p_ri[r] <- coef(summary(f))[2, "Pr(>|t|)"]
  a <- aggregate(y ~ mouse + cond, d, mean); w <- reshape(a, idvar = "mouse", timevar = "cond", direction = "wide")
  p_pt[r] <- t.test(w$y.drug, w$y.veh, paired = TRUE)$p.value
}
cat(sprintf("random intercept only (earlier first step): %.3f false positives; paired t on per-mouse means (current first step): %.3f\n", mean(p_ri < 0.05), mean(p_pt < 0.05)))
