# False-positive rate of the RMST(60) difference test (normal approximation) with no true difference,
# same null scenario as simulation.R Part 1. R 4.3.3, survival. Seed 20261111, fixed before running.
library(survival)
set.seed(20261111)
rate <- log(2) / 35
rmst_p <- function(t, ev, g, tau = 60) {
  tab <- summary(survfit(Surv(t, ev) ~ g), rmean = tau)$table
  d <- tab[2, "rmean"] - tab[1, "rmean"]; se <- sqrt(sum(tab[, "se(rmean)"]^2))
  2 * pnorm(-abs(d / se))
}
for (n in c(10, 20, 40)) {
  hits <- logical(5000); lrh <- logical(5000)
  for (i in 1:5000) {
    t <- rexp(2 * n, rate); ev <- as.integer(t <= 60); t <- pmin(t, 60); g <- rep(0:1, each = n)
    hits[i] <- rmst_p(t, ev, g) < 0.05
    lrh[i] <- pchisq(survdiff(Surv(t, ev) ~ g)$chisq, 1, lower.tail = FALSE) < 0.05
  }
  cat(sprintf("n = %2d per group, no true difference, 5000 runs: RMST(60) test %.4f  log-rank %.4f\n", n, mean(hits), mean(lrh)))
}
