# Kaplan-Meier survival curves and the log-rank test
# https://tidyecology.com/posts/kaplan-meier-survival-curves/
#
# 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-15

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

## ---- theme ----
te_ink <- "#16241d"; te_body <- "#2c3a31"; te_forest <- "#275139"
te_label <- "#46604a"; te_sage <- "#93a87f"; te_paper <- "#f5f4ee"
te_line <- "#dad9ca"; te_gold <- "#c9b458"; te_deep <- "#1d5b4e"
te_brick <- "#b5534e"; te_faint <- "#5d6b61"
theme_te <- function(base_size = 12) {
  theme_minimal(base_size = base_size) +
    theme(text = element_text(colour = te_body),
          plot.title = element_text(colour = te_ink, face = "bold"),
          plot.subtitle = element_text(colour = te_faint),
          axis.title = element_text(colour = te_label),
          axis.text = element_text(colour = te_body),
          panel.grid.minor = element_blank(),
          panel.grid.major = element_line(colour = te_line, linewidth = 0.3),
          plot.background = element_rect(fill = te_paper, colour = NA),
          panel.background = element_rect(fill = te_paper, colour = NA),
          legend.background = element_rect(fill = te_paper, colour = NA),
          legend.position = "none")
}

## ---- data ----
set.seed(71)
n <- 60
t_ctrl <- rweibull(n, shape = 1.3, scale = 17)     # riparian: shorter lives
t_trt  <- rweibull(n, shape = 1.3, scale = 30)     # upland: longer lives
cens_lt <- rexp(2 * n, rate = 1 / 70)              # collar failure or emigration
admin <- 36                                        # study closes at 36 months
tt  <- c(t_ctrl, t_trt)
grp <- factor(rep(c("Riparian", "Upland"), each = n))
ctime  <- pmin(cens_lt, admin)
time   <- pmin(tt, ctime)
status <- as.integer(tt <= ctime)                  # 1 = death seen, 0 = censored
d <- data.frame(time = round(time, 2), status = status, habitat = grp)

table(d$habitat, ifelse(d$status == 1, "death", "censored"))

## ---- fit ----
fit <- survfit(Surv(time, status) ~ habitat, data = d)
fit

## ---- fixed-times ----
st <- summary(fit, times = c(12, 24))
data.frame(habitat = sub("habitat=", "", st$strata), month = st$time,
           survival = round(st$surv, 3),
           lower = round(st$lower, 3), upper = round(st$upper, 3))

## ---- fig-km ----
grid <- seq(0, 36, by = 0.2)
band <- lapply(levels(d$habitat), function(g) {
  s <- summary(fit[paste0("habitat=", g)])
  data.frame(habitat = g, time = grid,
             surv  = stepfun(s$time, c(1, s$surv))(grid),
             lower = stepfun(s$time, c(1, s$lower))(grid),
             upper = stepfun(s$time, c(1, s$upper))(grid))
}) |> bind_rows()

ggplot(band, aes(time, surv, colour = habitat, fill = habitat)) +
  geom_ribbon(aes(ymin = lower, ymax = upper), colour = NA, alpha = 0.18) +
  geom_line(linewidth = 0.9) +
  scale_colour_manual(values = c(Riparian = te_brick, Upland = te_forest)) +
  scale_fill_manual(values = c(Riparian = te_brick, Upland = te_forest)) +
  annotate("text", x = 34, y = 0.10, label = "Riparian", colour = te_brick, hjust = 1, size = 3.2) +
  annotate("text", x = 34, y = 0.62, label = "Upland", colour = te_forest, hjust = 1, size = 3.2) +
  coord_cartesian(ylim = c(0, 1)) +
  labs(title = "Survival differs between habitats",
       subtitle = "Kaplan-Meier estimate with 95 per cent confidence bands",
       x = "Months since tagging", y = "Survival probability") +
  theme_te()

## ---- logrank ----
lr <- survdiff(Surv(time, status) ~ habitat, data = d)
lr
c(chisq = round(lr$chisq, 3),
  p_value = signif(1 - pchisq(lr$chisq, length(lr$n) - 1), 3))

## ---- naive ----
naive <- tapply(d$time, d$habitat, mean)
rmean <- summary(fit, rmean = 36)$table[, "rmean"]
data.frame(habitat = names(naive),
           naive_mean_time = round(naive, 2),
           km_restricted_mean = round(rmean, 2),
           median = round(summary(fit)$table[, "median"], 2))

## ---- fig-summary ----
med <- summary(fit)$table[, "median"]
cmp <- data.frame(
  habitat = rep(c("Riparian", "Upland"), each = 3),
  metric = factor(rep(c("Naive mean", "KM restricted mean", "Median"), 2),
                  levels = c("Naive mean", "KM restricted mean", "Median")),
  value = c(naive["Riparian"], rmean["habitat=Riparian"], med["habitat=Riparian"],
            naive["Upland"],   rmean["habitat=Upland"],   med["habitat=Upland"]))

ggplot(cmp, aes(habitat, value, fill = metric)) +
  geom_col(position = position_dodge(0.8), width = 0.72) +
  geom_text(aes(label = round(value, 1)), position = position_dodge(0.8),
            vjust = -0.4, size = 3, colour = te_body) +
  scale_fill_manual(values = c("Naive mean" = te_brick,
                               "KM restricted mean" = te_forest, "Median" = te_gold)) +
  labs(title = "The naive mean understates survival",
       subtitle = "Naive mean, restricted mean and median by habitat",
       x = NULL, y = "Months", fill = NULL) +
  theme_te() +
  theme(legend.position = "top")
