# Removal and depletion sampling in R
# https://tidyecology.com/posts/removal-sampling-depletion/
#
# 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-01

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

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(17)
Ntrue <- 220; p_true <- 0.40; k <- 5

catch <- integer(k)
remaining <- Ntrue
for (j in 1:k) {
  cj <- rbinom(1, remaining, p_true)   # each remaining animal caught with prob p
  catch[j] <- cj
  remaining <- remaining - cj          # removed, not returned
}
catch
Tc <- sum(catch)                        # total caught
cum_before <- c(0, cumsum(catch)[-k])   # animals already removed before each pass
c(total_caught = Tc, true_N = Ntrue, never_caught = Ntrue - Tc)

## ---- fit-removal ----
nll <- function(par) {
  N <- Tc + exp(par[1])          # N is at least the total already caught
  p <- plogis(par[2])
  remain <- N - cum_before
  if (any(remain < catch)) return(1e6)
  -sum(lchoose(remain, catch) + catch * log(p) + (remain - catch) * log(1 - p))
}

o <- optim(c(log(20), qlogis(0.35)), nll, method = "BFGS", hessian = TRUE)
stopifnot(o$convergence == 0)   # check before inverting the Hessian
N_hat <- Tc + exp(o$par[1])
p_hat <- plogis(o$par[2])

V <- solve(o$hessian)
se_N <- sqrt(V[1, 1]) * (N_hat - Tc)          # delta method for N = Tc + exp(.)
se_p <- sqrt(V[2, 2]) * p_hat * (1 - p_hat)   # delta method for p
round(c(N_hat = N_hat, se_N = se_N, p_hat = p_hat, se_p = se_p), 3)

## ---- fig-catch-decline ----
exp_catch <- p_hat * (N_hat - cum_before)
d1 <- data.frame(pass = factor(1:k), catch = catch, fit = exp_catch)

ggplot(d1, aes(pass, catch)) +
  geom_col(width = 0.6, fill = forest) +
  geom_line(aes(x = as.numeric(pass), y = fit), colour = red, linewidth = 0.8) +
  geom_point(aes(x = as.numeric(pass), y = fit), colour = red, size = 2.4) +
  labs(title = "Catches decline as animals are removed",
       subtitle = "Observed catch per pass (bars) and the fitted removal model (line)",
       x = "Removal pass", y = "Individuals caught") +
  theme_te()

## ---- fig-leslie ----
les <- lm(catch ~ cum_before)
sl <- coef(les)[2]; ic <- coef(les)[1]
N_les <- -ic / sl                 # cumulative catch at which the line hits zero
p_les <- -sl                      # slope is minus the catch probability
c(N_leslie = N_les, p_leslie = p_les, r2 = summary(les)$r.squared)

ggplot(d1, aes(cum_before, catch)) +
  geom_abline(slope = sl, intercept = ic, colour = forest, linewidth = 0.7) +
  geom_point(colour = ink, size = 2.6) +
  geom_hline(yintercept = 0, colour = faint, linewidth = 0.3) +
  annotate("point", x = N_les, y = 0, colour = red, size = 3) +
  annotate("text", x = N_les, y = 14, label = sprintf("N = %.0f", N_les),
           colour = red, hjust = 1.05, size = 3.6) +
  labs(title = "Depletion regression estimates population size",
       subtitle = "Catch per pass falls linearly with the number already removed",
       x = "Cumulative catch before the pass", y = "Catch on the pass (CPUE)") +
  theme_te()

## ---- fig-profile ----
Ngrid <- Tc:(Tc + 250)
profll <- sapply(Ngrid, function(Nn) {
  remain <- Nn - cum_before
  ph <- Tc / sum(remain)          # closed-form best p for this N
  if (ph <= 0 || ph >= 1) return(-Inf)
  sum(dbinom(catch, remain, ph, log = TRUE))
})
Nhat_int <- Ngrid[which.max(profll)]
cut <- max(profll) - qchisq(0.95, 1) / 2
Nci_lr <- range(Ngrid[profll >= cut])
c(N_hat = Nhat_int, lo = Nci_lr[1], hi = Nci_lr[2])

d3 <- data.frame(N = Ngrid, rll = profll - max(profll))
d3 <- d3[d3$rll > -6, ]
ggplot(d3, aes(N, rll)) +
  annotate("rect", xmin = Nci_lr[1], xmax = Nci_lr[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 = Nhat_int, colour = red, linewidth = 0.5) +
  labs(title = "Profile likelihood gives the interval for N",
       subtitle = "Relative log-likelihood; dashed line is the 95% cut, shaded band the interval",
       x = "Population size N", y = "Relative log-likelihood") +
  theme_te()

## ---- two-pass ----
N2 <- catch[1]^2 / (catch[1] - catch[2])
p2 <- (catch[1] - catch[2]) / catch[1]
c(N_two_pass = N2, p_two_pass = p2)
