# Closed-population capture-recapture in R
# https://tidyecology.com/posts/closed-capture-recapture/
#
# 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)

ink <- "#16241d"; body <- "#2c3a31"; forest <- "#275139"; gold <- "#c9b458"
paper <- "#f5f4ee"; line_c <- "#dad9ca"; faint <- "#5d6b61"; sage <- "#93a87f"; red <- "#b5534e"

theme_te <- function(base = 12) {
  theme_minimal(base_size = base) +
    theme(panel.grid.minor = element_blank(),
          panel.grid.major = element_line(colour = line_c, linewidth = 0.3),
          plot.background = element_rect(fill = paper, colour = NA),
          panel.background = element_rect(fill = paper, colour = NA),
          axis.title = element_text(colour = body), axis.text = element_text(colour = faint),
          plot.title = element_text(colour = ink, face = "bold"),
          plot.subtitle = element_text(colour = faint),
          legend.title = element_text(colour = body), legend.text = element_text(colour = faint))
}

## ---- simulate ----
set.seed(18)
Ntrue <- 300; p_true <- 0.25; k <- 5

H <- matrix(rbinom(Ntrue * k, 1, p_true), Ntrue, k)  # capture histories, one row per animal
seen <- rowSums(H) > 0
Mt1 <- sum(seen)     # distinct animals ever caught
n <- sum(H)          # total number of captures
c(distinct_seen = Mt1, total_captures = n, never_seen = Ntrue - Mt1)

## ---- fig-accumulation ----
newly <- integer(k); cumd <- integer(k); everseen <- logical(Ntrue)
for (t in 1:k) {
  newthis <- H[, t] == 1 & !everseen
  newly[t] <- sum(newthis)
  everseen <- everseen | H[, t] == 1
  cumd[t] <- sum(everseen)
}
rbind(newly = newly, cumulative = cumd)

d1 <- data.frame(occ = 1:k, newly = newly, cum = cumd)
ggplot(d1, aes(occ)) +
  geom_col(aes(y = newly), width = 0.55, fill = sage) +
  geom_line(aes(y = cum), colour = forest, linewidth = 0.9) +
  geom_point(aes(y = cum), colour = forest, size = 2.4) +
  geom_hline(yintercept = Ntrue, linetype = "dashed", colour = red, linewidth = 0.5) +
  annotate("text", x = 1.1, y = Ntrue + 9, label = "true N = 300", colour = red, hjust = 0, size = 3.4) +
  labs(title = "New captures fall but never reach the true total",
       subtitle = "Newly marked animals per occasion (bars) and cumulative distinct caught (line)",
       x = "Capture occasion", y = "Individuals") +
  theme_te()

## ---- lincoln-petersen ----
n1 <- sum(H[, 1]); n2 <- sum(H[, 2]); m2 <- sum(H[, 1] == 1 & H[, 2] == 1)
Nlp <- n1 * n2 / m2
Nch <- (n1 + 1) * (n2 + 1) / (m2 + 1) - 1
var_ch <- (n1 + 1) * (n2 + 1) * (n1 - m2) * (n2 - m2) / ((m2 + 1)^2 * (m2 + 2))
se_ch <- sqrt(var_ch)
c(n1 = n1, n2 = n2, m2 = m2, Lincoln_Petersen = Nlp, Chapman = Nch,
  lo = Nch - 1.96 * se_ch, hi = Nch + 1.96 * se_ch)

## ---- schnabel ----
caught_before <- 0; num <- 0; den <- 0
for (t in 1:k) {
  Ct <- sum(H[, t])                  # caught on occasion t
  Mt <- caught_before                # marked animals already in the population
  Rt <- if (t == 1) 0 else sum(H[, t] == 1 & rowSums(H[, 1:(t-1), drop = FALSE]) > 0)
  num <- num + Ct * Mt
  den <- den + Rt
  caught_before <- sum(rowSums(H[, 1:t, drop = FALSE]) > 0)
}
Nsch <- num / den
se_inv <- sqrt(den) / num; inv <- den / num     # interval on 1/N, then inverted
sch_ci <- 1 / (inv + c(1.96, -1.96) * se_inv)
c(N_schnabel = Nsch, lo = sch_ci[1], hi = sch_ci[2])

## ---- fit-m0 ----
Ngrid <- Mt1:(Mt1 + 600)
ll <- sapply(Ngrid, function(N) {
  p <- n / (k * N)                    # closed-form best p for this N
  if (p <= 0 || p >= 1) return(-Inf)
  lgamma(N + 1) - lgamma(N - Mt1 + 1) + n * log(p) + (k * N - n) * log(1 - p)
})
NhatM0 <- Ngrid[which.max(ll)]
cut <- max(ll) - qchisq(0.95, 1) / 2
ciM0 <- range(Ngrid[ll >= cut])
c(N_M0 = NhatM0, lo = ciM0[1], hi = ciM0[2], p = n / (k * NhatM0))

## ---- fig-estimators ----
d2 <- data.frame(
  method = factor(c("Distinct seen\n(lower bound)", "Lincoln-Petersen\n(Chapman)", "Schnabel", "Model M0"),
                  levels = c("Distinct seen\n(lower bound)", "Lincoln-Petersen\n(Chapman)", "Schnabel", "Model M0")),
  est = c(Mt1, Nch, Nsch, NhatM0),
  lo = c(NA, Nch - 1.96 * se_ch, sch_ci[1], ciM0[1]),
  hi = c(NA, Nch + 1.96 * se_ch, sch_ci[2], ciM0[2]),
  grp = c("seen", "est", "est", "est"))

ggplot(d2, aes(method, est, colour = grp)) +
  geom_hline(yintercept = Ntrue, linetype = "dashed", colour = faint) +
  geom_errorbar(aes(ymin = lo, ymax = hi), width = 0.14, linewidth = 0.7, na.rm = TRUE) +
  geom_point(size = 3.2) +
  scale_colour_manual(values = c(seen = gold, est = forest), guide = "none") +
  annotate("text", x = 0.7, y = Ntrue + 7, label = "truth", colour = faint, hjust = 0, size = 3.3) +
  labs(title = "Capture-recapture recovers the animals never seen",
       subtitle = "Population estimates with 95% intervals against the simulation truth",
       x = NULL, y = "Estimated population size") +
  theme_te()

## ---- fig-profile ----
d3 <- data.frame(N = Ngrid, rll = ll - max(ll))
d3 <- d3[d3$rll > -6, ]
ggplot(d3, aes(N, rll)) +
  annotate("rect", xmin = ciM0[1], xmax = ciM0[2], ymin = -Inf, ymax = Inf, alpha = 0.08, fill = gold) +
  geom_line(colour = forest, linewidth = 0.7) +
  geom_hline(yintercept = -qchisq(0.95, 1) / 2, linetype = "dashed", colour = faint) +
  geom_vline(xintercept = NhatM0, colour = red, linewidth = 0.5) +
  labs(title = "Profile likelihood for population size under model M0",
       subtitle = "Relative log-likelihood; dashed line is the 95% cut, shaded band the interval",
       x = "Population size N", y = "Relative log-likelihood") +
  theme_te()
