# Cormack-Jolly-Seber survival models in R
# https://tidyecology.com/posts/cormack-jolly-seber-survival/
#
# 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-14

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

## ---- 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.position = "none")
}

## ---- data ----
set.seed(519)
k <- 7; N <- 400
phi_true <- 0.72; p_true <- 0.42
alive <- matrix(0L, N, k); alive[, 1] <- 1L
for (t in 2:k) alive[, t] <- alive[, t - 1] * (runif(N) < phi_true)
capt <- matrix(0L, N, k)
capt[alive == 1] <- (runif(sum(alive == 1)) < p_true)
ch <- capt[rowSums(capt) > 0, , drop = FALSE]        # keep animals caught at least once
c(ever_caught = nrow(ch), total_captures = sum(ch), occasions = k)

## ---- marray ----
Rrel <- integer(k); m <- matrix(0L, k, k)
for (r in seq_len(nrow(ch))) {
  occ <- which(ch[r, ] == 1L)
  rel <- occ[occ < k]                                # captures at occasions 1..k-1
  Rrel[rel] <- Rrel[rel] + 1L
  if (length(occ) > 1)
    for (a in seq_len(length(occ) - 1)) m[occ[a], occ[a + 1]] <- m[occ[a], occ[a + 1]] + 1L
}
ma <- cbind(released = Rrel[1:(k - 1)], m[1:(k - 1), 2:k])
ma <- cbind(ma, never = Rrel[1:(k - 1)] - rowSums(m[1:(k - 1), 2:k]))
colnames(ma) <- c("released", paste0("recap_", 2:k), "never")
rownames(ma) <- paste0("release_", 1:(k - 1))
ma

## ---- fig-marray ----
msub <- m[1:(k - 1), 2:k]
dimnames(msub) <- list(release = paste0("occ ", 1:(k - 1)),
                       recapture = paste0("occ ", 2:k))
mm <- as.data.frame(as.table(msub))
names(mm) <- c("release", "recapture", "count")
mm$release   <- factor(mm$release,   levels = rev(paste0("occ ", 1:(k - 1))))
mm$recapture <- factor(mm$recapture, levels = paste0("occ ", 2:k))

ggplot(mm, aes(recapture, release, fill = count)) +
  geom_tile(colour = te_paper, linewidth = 1) +
  geom_text(aes(label = count), colour = te_ink, size = 3.1) +
  scale_fill_gradient(low = te_paper, high = te_forest) +
  labs(title = "The m-array of first recaptures",
       subtitle = "Counts concentrate just after release and fade with the gap",
       x = "Occasion of next recapture", y = "Release occasion") +
  theme_te() +
  theme(panel.grid.major = element_blank())

## ---- likelihood ----
negll <- function(par) {
  phi <- plogis(par[1]); p <- plogis(par[2]); ll <- 0
  for (i in 1:(k - 1)) {
    Ri <- Rrel[i]; if (Ri == 0) next
    seen <- 0
    for (j in (i + 1):k) {
      q <- phi^(j - i) * (1 - p)^(j - i - 1) * p       # first seen again at j
      if (m[i, j] > 0) ll <- ll + m[i, j] * log(q)
      seen <- seen + q
    }
    never <- Ri - sum(m[i, (i + 1):k])
    if (never > 0) ll <- ll + never * log(1 - seen)    # never seen again
  }
  -ll
}

fit <- optim(c(0, 0), negll, method = "BFGS", hessian = TRUE)
stopifnot(fit$convergence == 0)   # check before inverting the Hessian
est <- plogis(fit$par)
se_logit <- sqrt(diag(solve(fit$hessian)))
se_prob <- se_logit * est * (1 - est)                  # delta method to the probability scale
ci <- t(sapply(1:2, function(i) plogis(fit$par[i] + c(-1.96, 1.96) * se_logit[i])))

data.frame(parameter = c("apparent survival (phi)", "recapture (p)"),
           estimate = round(est, 4), se = round(se_prob, 4),
           lower = round(ci[, 1], 3), upper = round(ci[, 2], 3),
           truth = c(phi_true, p_true))

## ---- fig-estimates ----
ed <- data.frame(parameter = c("Apparent survival", "Recapture probability"),
                 est = est, lo = ci[, 1], hi = ci[, 2], truth = c(phi_true, p_true))

ggplot(ed, aes(est, parameter)) +
  geom_errorbar(aes(xmin = lo, xmax = hi),
                orientation = "y", width = 0.15, colour = te_forest, linewidth = 0.8) +
  geom_point(colour = te_forest, size = 2.6) +
  geom_point(aes(x = truth), colour = te_brick, shape = 4, size = 3, stroke = 1.1) +
  annotate("text", x = ed$truth[1], y = 1.12, label = "true", colour = te_brick, size = 3.0) +
  coord_cartesian(xlim = c(0.3, 0.8)) +
  labs(title = "Estimates recover the generating values",
       subtitle = "Points and intervals are the fitted estimates; crosses mark the truth",
       x = "Probability", y = NULL) +
  theme_te()
