# Latency tests when some animals never respond
# https://tidyecology.com/posts/latency-tests-with-non-responders/
#
# The R code of this Tidy Ecology tutorial, chunk by chunk, in the order of the post.
# Run it top to bottom in a fresh R session. Code the post shows but does not run
# is commented out. The prose, and the numbers it quotes, are in the post itself.
# Post last modified: 2026-10-07

## ---- setup ----
library(ggplot2)
library(patchwork)
library(survival)

te_paper  <- "#f5f4ee"
te_ink    <- "#16241d"
te_body   <- "#2c3a31"
te_forest <- "#275139"
te_rust   <- "#b5534e"
te_gold   <- "#c9b458"
te_line   <- "#dad9ca"

theme_datasheet <- function() {
  theme_minimal(base_size = 12) +
    theme(plot.background  = element_rect(fill = te_paper, colour = NA),
          panel.background = element_rect(fill = te_paper, colour = NA),
          panel.grid.major = element_line(colour = te_line, linewidth = 0.3),
          panel.grid.minor = element_blank(),
          text             = element_text(colour = te_body),
          plot.title       = element_text(colour = te_ink, face = "bold"),
          plot.subtitle    = element_text(colour = te_body),
          axis.text        = element_text(colour = te_body),
          strip.text       = element_text(colour = te_ink))
}

## ---- design ----
trial_cap <- 300
n_group   <- 30

scenarios <- data.frame(
  scenario = c("no effect", "more responders", "faster responders",
               "faster but fewer"),
  p_control = c(0.6, 0.5, 0.6, 0.8), p_treated = c(0.6, 0.8, 0.6, 0.5),
  m_control = c(60, 60, 60, 60),     m_treated = c(60, 60, 30, 20))

simulate_trial <- function(n, p_c, p_t, m_c, m_t, shape = 1, cap = trial_cap) {
  group <- rep(0:1, each = n)
  p_resp <- ifelse(group == 1, p_t, p_c)
  mean_lat <- ifelse(group == 1, m_t, m_c)
  scale_w <- mean_lat / gamma(1 + 1 / shape)
  latency <- ifelse(runif(2 * n) < p_resp, rweibull(2 * n, shape, scale_w), Inf)
  data.frame(group = group, time = pmin(latency, cap),
             status = as.numeric(latency <= cap))
}

# Exponential mixture cure model: logit share of responders, log rate among
# responders; par = (a0, b0, a1, b1), a1 and b1 are the treatment effects
cure_nll <- function(par, time, status, group) {
  p_r <- plogis(par[1] + par[3] * group)
  rate <- exp(par[2] + par[4] * group)
  surv_r <- exp(-rate * time)
  -sum(status * (log(p_r) + log(rate) - rate * time) +
         (1 - status) * log(1 - p_r + p_r * surv_r))
}
cure_grad <- function(par, time, status, group) {
  p_r <- plogis(par[1] + par[3] * group)
  rate <- exp(par[2] + par[4] * group)
  surv_r <- exp(-rate * time)
  lik_c <- 1 - p_r + p_r * surv_r
  g_a <- status * (1 - p_r) + (1 - status) * p_r * (1 - p_r) * (surv_r - 1) / lik_c
  g_b <- status * (1 - rate * time) - (1 - status) * p_r * surv_r * rate * time / lik_c
  -c(sum(g_a), sum(g_b), sum(g_a * group), sum(g_b * group))
}

# Weibull version: one shared shape, par[5] = log shape
cure_nll_weib <- function(par, time, status, group) {
  p_r <- plogis(par[1] + par[3] * group)
  rate <- exp(par[2] + par[4] * group)
  shape <- exp(par[5])
  cum_h <- (rate * time)^shape
  -sum(status * (log(p_r) + log(shape) + shape * log(rate) +
                   (shape - 1) * log(time) - cum_h) +
         (1 - status) * log(1 - p_r + p_r * exp(-cum_h)))
}

fit_one <- function(dat, fix, start_par, weibull) {
  n_par <- if (weibull) 5 else 4
  free <- setdiff(seq_len(n_par), fix)
  full_par <- function(q) { par <- rep(0, n_par); par[free] <- q; par }
  fn <- function(q) {
    if (weibull) cure_nll_weib(full_par(q), dat$time, dat$status, dat$group)
    else cure_nll(full_par(q), dat$time, dat$status, dat$group)
  }
  gr <- if (weibull) NULL else function(q) {
    cure_grad(full_par(q), dat$time, dat$status, dat$group)[free]
  }
  opt <- optim(start_par[free], fn, gr, method = "BFGS", control = list(maxit = 500))
  list(value = opt$value, par = full_par(opt$par), fn = fn, gr = gr, free = free,
       conv = opt$convergence)
}

# Full fit and the two restricted fits (no incidence effect, no speed effect).
# One run can stop short of the best restricted fit, so each restricted fit
# starts from three points (the full fit, the full fit's own start, and a start
# with nearly all animals responding slowly) and keeps the best.
# A restricted fit that beats the full fit means the full fit stopped early:
# refit the full model from there.
cure_tests <- function(dat, weibull = FALSE, hessian = FALSE) {
  resp <- dat$status == 1
  start_par <- c(qlogis(min(max(mean(resp), 0.05), 0.95)),
             log(sum(resp) / sum(dat$time[resp])), 0, 0)
  if (weibull) start_par <- c(start_par, 0)
  slow_par <- start_par
  slow_par[1:2] <- c(qlogis(0.95), start_par[2] - 1.5)
  best_of <- function(fix, starts) {
    fits <- lapply(starts, function(s) fit_one(dat, fix, s, weibull))
    fits[[which.min(vapply(fits, function(f) f$value, numeric(1)))]]
  }
  full <- fit_one(dat, integer(0), start_par, weibull)
  no_inc <- best_of(3, list(full$par, start_par, slow_par))
  no_spd <- best_of(4, list(full$par, start_par, slow_par))
  best <- which.min(c(full$value, no_inc$value, no_spd$value))
  if (best > 1) {
    full <- fit_one(dat, integer(0), list(full, no_inc, no_spd)[[best]]$par, weibull)
  }
  out <- c(lrt_inc = 2 * (no_inc$value - full$value),
           lrt_spd = 2 * (no_spd$value - full$value),
           a0 = full$par[1], b0 = full$par[2], a1 = full$par[3], b1 = full$par[4],
           conv = full$conv + no_inc$conv + no_spd$conv)
  if (hessian) {
    hess <- optimHess(full$par, full$fn, full$gr)
    vc <- tryCatch(solve(hess), error = function(e) matrix(NA, 4, 4))
    safe_se <- function(v) if (is.na(v) || v <= 0) NA_real_ else sqrt(v)
    out <- c(out, se_a0 = safe_se(vc[1, 1]),
             se_at = safe_se(vc[1, 1] + vc[3, 3] + 2 * vc[1, 3]),
             se_b1 = safe_se(vc[4, 4]))
  }
  out
}

## ---- example ----
hazard_ratio_true <- function(t, p_c, p_t, m_c, m_t) {
  haz <- function(p, m) p * exp(-t / m) / m / (1 - p + p * exp(-t / m))
  haz(p_t, m_t) / haz(p_c, m_c)
}
curve_grid <- seq(0, trial_cap, by = 1)
curve_df <- do.call(rbind, lapply(2:4, function(i) {
  s <- scenarios[i, ]
  rbind(data.frame(scenario = s$scenario, time = curve_grid, group = "control",
                   still_waiting = 1 - s$p_control * (1 - exp(-curve_grid / s$m_control))),
        data.frame(scenario = s$scenario, time = curve_grid, group = "treated",
                   still_waiting = 1 - s$p_treated * (1 - exp(-curve_grid / s$m_treated))))
}))
hr_df <- do.call(rbind, lapply(2:4, function(i) {
  s <- scenarios[i, ]
  data.frame(scenario = s$scenario, time = curve_grid,
             hr = hazard_ratio_true(curve_grid, s$p_control, s$p_treated,
                                    s$m_control, s$m_treated))
}))
hr_at <- function(scen, t) hr_df$hr[hr_df$scenario == scen & hr_df$time == t]
odds_ratio_more <- (0.8 / 0.2) / (0.5 / 0.5)

## ---- fig-curves ----
lev <- scenarios$scenario[2:4]
curve_df$scenario <- factor(curve_df$scenario, levels = lev)
hr_df$scenario <- factor(hr_df$scenario, levels = lev)
p_surv <- ggplot(curve_df, aes(time, still_waiting, colour = group)) +
  geom_line(linewidth = 0.9) +
  facet_wrap(~ scenario) +
  scale_colour_manual(values = c(control = te_ink, treated = te_rust), name = NULL) +
  scale_y_continuous(limits = c(0, 1)) +
  scale_x_continuous(breaks = c(0, 100, 200, 300)) +
  labs(x = NULL, y = "share not yet responded",
       title = "Three treatments, three shapes of plateau",
       subtitle = "expected curves; responders' latency exponential; 300 s cap") +
  theme_datasheet() +
  theme(legend.position = "top")
p_hr <- ggplot(hr_df, aes(time, hr)) +
  geom_hline(yintercept = 1, linetype = "dashed", colour = te_body, linewidth = 0.4) +
  geom_line(colour = te_forest, linewidth = 0.9) +
  facet_wrap(~ scenario) +
  scale_y_log10(breaks = c(0.01, 0.1, 1, 10), labels = c("0.01", "0.1", "1", "10")) +
  coord_cartesian(ylim = c(0.01, 10)) +
  scale_x_continuous(breaks = c(0, 100, 200, 300)) +
  labs(x = "seconds since the object was placed", y = "true hazard ratio",
       subtitle = "treated / control hazard, log scale") +
  theme_datasheet()
(p_surv / p_hr) + plot_annotation(theme = theme_datasheet())

## ---- scenarios ----
n_rep <- 1000
one_experiment <- function(s) {
  dat <- simulate_trial(n_group, s$p_control, s$p_treated, s$m_control, s$m_treated)
  cox_fit <- coxph(Surv(time, status) ~ group, data = dat)
  resp <- dat[dat$status == 1, ]
  p_drop <- if (min(table(factor(resp$group, 0:1))) >= 2) {
    t.test(log(time) ~ group, data = resp)$p.value
  } else NA
  cure <- cure_tests(dat, hessian = TRUE)
  c(t_capped = t.test(log(time) ~ group, data = dat)$p.value,
    t_responders = p_drop,
    mann_whitney = wilcox.test(time ~ group, data = dat, exact = FALSE)$p.value,
    log_rank = pchisq(survdiff(Surv(time, status) ~ group, data = dat)$chisq, 1,
                      lower.tail = FALSE),
    cox = summary(cox_fit)$coefficients[1, 5],
    ph_test = cox.zph(cox_fit)$table[1, 3],
    cure_inc = pchisq(cure[["lrt_inc"]], 1, lower.tail = FALSE),
    cure_spd = pchisq(cure[["lrt_spd"]], 1, lower.tail = FALSE),
    hr = exp(coef(cox_fit)[[1]]), cure)
}

set.seed(1952)
sim_main <- lapply(seq_len(nrow(scenarios)), function(i) {
  t(replicate(n_rep, one_experiment(scenarios[i, ])))
})
names(sim_main) <- scenarios$scenario

test_cols <- c("t_capped", "t_responders", "mann_whitney", "log_rank", "cox",
               "ph_test", "cure_inc", "cure_spd")
test_labels <- c("t test, capped log latency", "t test, responders only",
                 "Mann-Whitney, capped latency", "log-rank", "Cox",
                 "PH test (cox.zph)", "cure model: incidence LRT",
                 "cure model: speed LRT")
rej_tab <- do.call(rbind, lapply(names(sim_main), function(sc) {
  m <- sim_main[[sc]]
  data.frame(scenario = sc, test = test_labels,
             rate = colMeans(m[, test_cols] < 0.05, na.rm = TRUE),
             n_used = colSums(!is.na(m[, test_cols])))
}))
rej <- function(sc, col) mean(sim_main[[sc]][, col] < 0.05, na.rm = TRUE)
med_hr <- function(sc) median(sim_main[[sc]][, "hr"])
share_hr_up <- function(sc) mean(sim_main[[sc]][, "hr"] > 1)
mc_se_05 <- sqrt(0.05 * 0.95 / n_rep)
mc_se_max <- sqrt(0.25 / n_rep)
n_drop_na <- sum(is.na(unlist(lapply(sim_main, function(m) m[, "t_responders"]))))
n_nonconv <- sum(unlist(lapply(sim_main, function(m) m[, "conv"])) > 0)

coverage_of <- function(sc) {
  m <- sim_main[[sc]]; s <- scenarios[scenarios$scenario == sc, ]
  a_true <- qlogis(s$p_control); at_true <- qlogis(s$p_treated)
  b1_true <- log(s$m_control / s$m_treated)
  cover <- function(est, se, truth) mean(!is.na(se) & abs(est - truth) <= 1.96 * se)
  c(p_control = cover(m[, "a0"], m[, "se_a0"], a_true),
    p_treated = cover(m[, "a0"] + m[, "a1"], m[, "se_at"], at_true),
    rate_ratio = cover(m[, "b1"], m[, "se_b1"], b1_true),
    bias_pc = median(plogis(m[, "a0"])) - s$p_control,
    bias_pt = median(plogis(m[, "a0"] + m[, "a1"])) - s$p_treated,
    rr_med = median(exp(m[, "b1"])), rr_true = exp(b1_true))
}
cov_tab <- sapply(scenarios$scenario, coverage_of)

## ---- fig-rejection ----
rej_plot <- rej_tab
rej_plot$test <- factor(rej_plot$test, levels = rev(test_labels))
rej_plot$scenario <- factor(rej_plot$scenario, levels = scenarios$scenario)
rej_plot$kind <- ifelse(grepl("cure", rej_plot$test), "cure model",
                        ifelse(grepl("PH test", rej_plot$test), "assumption check",
                               "one-number test"))
ggplot(rej_plot, aes(rate, test, colour = kind, shape = kind)) +
  geom_vline(xintercept = 0.05, linetype = "dashed", colour = te_body, linewidth = 0.4) +
  geom_point(size = 2.8) +
  facet_wrap(~ scenario, nrow = 2) +
  scale_colour_manual(values = c("one-number test" = te_ink, "assumption check" = te_gold,
                                 "cure model" = te_rust), name = NULL) +
  scale_shape_manual(values = c("one-number test" = 16, "assumption check" = 17,
                                "cure model" = 15), name = NULL) +
  scale_x_continuous(limits = c(0, 1), breaks = c(0, 0.25, 0.5, 0.75, 1),
                     labels = c("0", "0.25", "0.5", "0.75", "1")) +
  labs(x = "share of experiments with p < 0.05", y = NULL,
       title = "One hazard ratio, two effects",
       subtitle = "30 animals per group, 300 s cap, 1000 experiments per panel") +
  theme_datasheet() +
  theme(legend.position = "bottom")

## ---- leak ----
n_leak <- 1000
leak_grid <- expand.grid(n = c(20, 30, 60, 120),
                         pair = c("0.5 and 0.5", "0.5 and 0.8", "0.8 and 0.8"),
                         stringsAsFactors = FALSE)
pair_p <- list("0.5 and 0.5" = c(0.5, 0.5), "0.5 and 0.8" = c(0.5, 0.8),
               "0.8 and 0.8" = c(0.8, 0.8))
set.seed(1982)
leak_raw <- lapply(seq_len(nrow(leak_grid)), function(i) {
  pp <- pair_p[[leak_grid$pair[i]]]
  t(replicate(n_leak, cure_tests(simulate_trial(leak_grid$n[i], pp[1], pp[2], 60, 60))
              [c("lrt_inc", "lrt_spd", "conv")]))
})
leak_grid$spd_rate <- sapply(leak_raw, function(m) mean(m[, "lrt_spd"] > qchisq(0.95, 1)))
leak_grid$spd_mean <- sapply(leak_raw, function(m) mean(m[, "lrt_spd"]))
leak_grid$inc_rate <- sapply(leak_raw, function(m) mean(m[, "lrt_inc"] > qchisq(0.95, 1)))
pooled_by <- function(col, value) {
  idx <- which(leak_grid[[col]] == value)
  all_stat <- unlist(lapply(leak_raw[idx], function(m) m[, "lrt_spd"]))
  c(rate = mean(all_stat > qchisq(0.95, 1)), n = length(all_stat),
    mean_stat = mean(all_stat))
}
leak_pair <- sapply(names(pair_p), function(v) pooled_by("pair", v))
leak_n <- sapply(c(20, 30, 60, 120), function(v) pooled_by("n", v))
leak_all <- unlist(lapply(leak_raw, function(m) m[, "lrt_spd"]))
leak_rate_all <- mean(leak_all > qchisq(0.95, 1))
leak_se_all <- sqrt(leak_rate_all * (1 - leak_rate_all) / length(leak_all))
leak_mean_all <- mean(leak_all)
leak_mean_se <- sd(leak_all) / sqrt(length(leak_all))
leak_nonconv <- sum(unlist(lapply(leak_raw, function(m) m[, "conv"])) > 0)
inc_power <- function(n) leak_grid$inc_rate[leak_grid$n == n & leak_grid$pair == "0.5 and 0.8"]

## ---- shape ----
n_shape <- 600
shape_cells <- data.frame(
  cell = c("no effect, shape 0.5", "no effect, shape 1", "no effect, shape 1.5",
           "more responders, shape 0.5", "faster responders, shape 0.5"),
  p_c = c(0.6, 0.6, 0.6, 0.5, 0.6), p_t = c(0.6, 0.6, 0.6, 0.8, 0.6),
  m_c = 60, m_t = c(60, 60, 60, 60, 30), shape = c(0.5, 1, 1.5, 0.5, 0.5))
set.seed(2000)
shape_raw <- lapply(seq_len(nrow(shape_cells)), function(i) {
  s <- shape_cells[i, ]
  t(replicate(n_shape, {
    dat <- simulate_trial(n_group, s$p_c, s$p_t, s$m_c, s$m_t, s$shape)
    e_fit <- cure_tests(dat)
    w_fit <- cure_tests(dat, weibull = TRUE)
    c(e_inc = e_fit[["lrt_inc"]], e_spd = e_fit[["lrt_spd"]],
      w_inc = w_fit[["lrt_inc"]], w_spd = w_fit[["lrt_spd"]],
      conv = e_fit[["conv"]] + w_fit[["conv"]])
  }))
})
crit <- qchisq(0.95, 1)
shape_tab <- do.call(rbind, lapply(seq_len(nrow(shape_cells)), function(i) {
  m <- shape_raw[[i]]
  data.frame(cell = shape_cells$cell[i],
             model = rep(c("exponential latency", "Weibull latency"), each = 2),
             test = rep(c("incidence LRT", "speed LRT"), 2),
             rate = c(mean(m[, "e_inc"] > crit), mean(m[, "e_spd"] > crit),
                      mean(m[, "w_inc"] > crit), mean(m[, "w_spd"] > crit)))
}))
sh <- function(cell, model, test) {
  shape_tab$rate[shape_tab$cell == cell & shape_tab$model == model & shape_tab$test == test]
}
shape_nonconv <- sum(unlist(lapply(shape_raw, function(m) m[, "conv"])) > 0)
mc_se_shape <- sqrt(0.05 * 0.95 / n_shape)
reached_shape05 <- 1 - exp(-(trial_cap / (60 / gamma(3)))^0.5)

## ---- fig-shape ----
shape_plot <- shape_tab
shape_plot$cell <- factor(shape_plot$cell, levels = rev(shape_cells$cell))
ggplot(shape_plot, aes(rate, cell, colour = model, shape = model)) +
  geom_vline(xintercept = 0.05, linetype = "dashed", colour = te_body, linewidth = 0.4) +
  geom_point(size = 3, position = position_dodge(width = 0.5)) +
  facet_wrap(~ test) +
  scale_colour_manual(values = c("exponential latency" = te_rust,
                                 "Weibull latency" = te_forest), name = "cure model with") +
  scale_shape_manual(values = c("exponential latency" = 16, "Weibull latency" = 17),
                     name = "cure model with") +
  scale_x_continuous(limits = c(0, 1), breaks = c(0, 0.25, 0.5, 0.75)) +
  labs(x = "share of experiments with p < 0.05", y = NULL,
       title = "A wrong latency shape miscalibrates the speed test",
       subtitle = "30 animals per group; true Weibull shape in the row label") +
  theme_datasheet() +
  theme(legend.position = "bottom")

## ---- followup ----
n_follow <- 800
follow_means <- c(30, 60, 100, 150, 200, 300)
set.seed(2011)
follow_raw <- lapply(follow_means, function(m) {
  t(replicate(n_follow, cure_tests(simulate_trial(n_group, 0.5, 0.8, m, m),
                                   hessian = TRUE)))
})
follow_tab <- data.frame(
  mean_latency = follow_means,
  reached = 1 - exp(-trial_cap / follow_means),
  inc_power = sapply(follow_raw, function(m) mean(m[, "lrt_inc"] > crit)),
  spd_false = sapply(follow_raw, function(m) mean(m[, "lrt_spd"] > crit)),
  cover_pc = sapply(follow_raw, function(m) {
    mean(!is.na(m[, "se_a0"]) & abs(m[, "a0"] - qlogis(0.5)) <= 1.96 * m[, "se_a0"])
  }),
  med_pc = sapply(follow_raw, function(m) median(plogis(m[, "a0"]))),
  q90_pc = sapply(follow_raw, function(m) quantile(plogis(m[, "a0"]), 0.9, names = FALSE)),
  se_na = sapply(follow_raw, function(m) mean(is.na(m[, "se_a0"]))),
  wid_pc = sapply(follow_raw, function(m) {
    se <- ifelse(is.na(m[, "se_a0"]), Inf, m[, "se_a0"])
    median(plogis(m[, "a0"] + 1.96 * se) - plogis(m[, "a0"] - 1.96 * se))
  }))
fo <- function(m, col) follow_tab[follow_tab$mean_latency == m, col]
follow_nonconv <- sum(unlist(lapply(follow_raw, function(m) m[, "conv"])) > 0)
mc_se_follow <- sqrt(0.05 * 0.95 / n_follow)

## ---- fig-followup ----
follow_long <- rbind(
  data.frame(reached = follow_tab$reached, value = follow_tab$inc_power,
             what = "incidence LRT rejects (true effect)"),
  data.frame(reached = follow_tab$reached, value = follow_tab$cover_pc,
             what = "95% interval covers control share"),
  data.frame(reached = follow_tab$reached, value = follow_tab$spd_false,
             what = "speed LRT rejects (no effect)"))
ggplot(follow_long, aes(reached, value, colour = what, shape = what)) +
  geom_hline(yintercept = c(0.05, 0.95), linetype = "dashed", colour = te_body,
             linewidth = 0.4) +
  geom_line(linewidth = 0.8) +
  geom_point(size = 2.6) +
  scale_colour_manual(values = c(te_rust, te_forest, te_gold), name = NULL) +
  scale_shape_manual(values = c(16, 17, 15), name = NULL) +
  scale_x_reverse(breaks = c(1, 0.95, 0.86, 0.78, 0.63)) +
  scale_y_continuous(limits = c(0, 1)) +
  labs(x = "share of eventual responders who respond before the cap",
       y = "share of experiments",
       title = "Short follow-up leaves the plateau unidentified",
       subtitle = "control 0.5, treated 0.8 respond; same latency in both groups") +
  theme_datasheet() +
  theme(legend.position = "bottom") +
  guides(colour = guide_legend(nrow = 2), shape = guide_legend(nrow = 2))
