# Hierarchical distance sampling
# https://tidyecology.com/posts/hierarchical-distance-sampling/
#
# 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)
te_canvas <- theme(plot.background  = element_rect(fill = "#f5f4ee", colour = NA),
                   panel.background = element_rect(fill = "#f5f4ee", colour = NA))

g_hn  <- function(x, s) exp(-x^2/(2*s^2))
mu_hn <- function(s, w) sqrt(2*pi)*s*(pnorm(w, 0, s) - 0.5)

w   <- 0.1                             # truncation, km
R   <- 200                             # transect segments (sites)
ell <- 0.5                             # segment length, km
A   <- 2*w*ell                         # covered area per site, km2
cut <- seq(0, w, length.out = 6); B <- 5   # five 20 m distance bins

## cell probability: the chance a site's animal is detected AND lands in bin b
pi_b <- function(s) sqrt(2*pi)*s*(pnorm(cut[-1], 0, s) - pnorm(cut[-length(cut)], 0, s))/w
sprintf("sigma 0.05: bin probabilities %s | missed %.4f | detected %.4f (= mu/w = %.4f)",
        paste(sprintf("%.4f", pi_b(0.05)), collapse = " "),
        1 - sum(pi_b(0.05)), sum(pi_b(0.05)), mu_hn(0.05, w)/w)

## ---- identity ----
lam <- 4.7; s <- 0.043; y <- c(3, 2, 1, 1, 0)
n <- sum(y); P <- pi_b(s); p0 <- 1 - sum(P); Nmax <- n + 400

closed <- sum(dpois(y, lam*P, log = TRUE))
summed <- log(sum(sapply(n:Nmax, function(N)
  dpois(N, lam) *
  exp(lfactorial(N) - sum(lfactorial(y)) - lfactorial(N - n)) *  # multinomial coefficient
  prod(P^y) * p0^(N - n))))
sprintf("latent-N sum to %d: %.12f | independent Poissons: %.12f | difference %.1e",
        Nmax, summed, closed, summed - closed)

## ---- counter-world ----
th <- 1.5
summed_nb <- log(sum(sapply(n:Nmax, function(N)
  dnbinom(N, mu = lam, size = th) *
  exp(lfactorial(N) - sum(lfactorial(y)) - lfactorial(N - n)) *
  prod(P^y) * p0^(N - n))))
sprintf("N ~ NB(mu = %g, size = %g): true log-likelihood %.6f vs Poisson shortcut %.6f (out by %.4f)",
        lam, th, summed_nb, closed, summed_nb - closed)

## ---- world ----
b0 <- log(4); b1 <- 0.6; a0 <- log(0.05); a1 <- -0.35
sim_hds <- function(seed, alpha1 = a1) {
  set.seed(seed)
  z <- rnorm(R); lam <- exp(b0 + b1*z); sg <- exp(a0 + alpha1*z)
  N <- rpois(R, lam)
  y <- t(sapply(seq_len(R), function(i) {
    if (N[i] == 0) return(rep(0, B))
    x <- runif(N[i], 0, w); d <- x[runif(N[i]) < g_hn(x, sg[i])]
    tabulate(findInterval(d, cut, rightmost.closed = TRUE), B)
  }))
  list(y = y, z = z, N = N)
}
nll_hds <- function(p, y, z, sig_cov = TRUE) {
  lam <- exp(p[1] + p[2]*z)
  sg  <- if (sig_cov) exp(p[3] + p[4]*z) else rep(exp(p[3]), length(z))
  -sum(dpois(y, lam * t(sapply(sg, pi_b)), log = TRUE))   # y is R x B, so t() the R x B back
}
fit <- function(par, ...) {
  o <- optim(par, nll_hds, ..., method = "Nelder-Mead")
  o <- optim(o$par, nll_hds, ..., method = "Nelder-Mead", hessian = TRUE)
  list(par = o$par, nll = o$value,
       se = tryCatch(sqrt(diag(solve(o$hessian))), error = function(e) rep(NA_real_, length(par))))
}
d <- sim_hds(4282)
sprintf("seed 4282: %d animals present, %d detected (%.1f%%), %d of %d sites detected nothing",
        sum(d$N), sum(d$y), 100*sum(d$y)/sum(d$N), sum(rowSums(d$y) == 0), R)

## ---- fits ----
f4 <- fit(c(1, 0.5, log(0.05), -0.3), y = d$y, z = d$z)
f3 <- fit(c(1, 0.5, log(0.05)), y = d$y, z = d$z, sig_cov = FALSE)
rbind(FULL  = c(b0 = f4$par[1], b1 = f4$par[2], a0 = f4$par[3], a1 = f4$par[4]),
      truth = c(b0, b1, a0, a1))
sprintf("FULL : b1 %.4f (SE %.4f) | a1 %.4f (SE %.4f) | N total %.0f (true %d, raw count %d)",
        f4$par[2], f4$se[2], f4$par[4], f4$se[4],
        sum(exp(f4$par[1] + f4$par[2]*d$z)), sum(d$N), sum(d$y))
sprintf("NAIVE: b1 %.4f (SE %.4f, %+.1f%%) | 95%% CI [%.3f, %.3f] contains 0.6? %s | dAIC %.1f",
        f3$par[2], f3$se[2], 100*(f3$par[2]/b1 - 1),
        f3$par[2] - 1.96*f3$se[2], f3$par[2] + 1.96*f3$se[2],
        f3$par[2] - 1.96*f3$se[2] < b1 && b1 < f3$par[2] + 1.96*f3$se[2],
        (2*f3$nll + 6) - (2*f4$nll + 8))

## ---- pseudo-true ----
zg <- seq(-5, 5, length.out = 601); wz <- dnorm(zg); wz <- wz/sum(wz)
Ey <- t(sapply(seq_along(zg), function(i) exp(b0 + b1*zg[i])*pi_b(exp(a0 + a1*zg[i]))))
negQ <- function(p, sig_cov = FALSE) {
  lm_ <- exp(p[1] + p[2]*zg)
  sg  <- if (sig_cov) exp(p[3] + p[4]*zg) else rep(exp(p[3]), length(zg))
  M <- t(sapply(seq_along(zg), function(i) lm_[i]*pi_b(sg[i])))
  Q <- Ey*log(M); Q[Ey == 0] <- 0        # 0 log 0 = 0, else the truth scores as undefined
  -sum(wz * rowSums(Q - M))
}
tight <- function(par, ...) {
  o <- optim(par, negQ, ..., method = "Nelder-Mead",
             control = list(reltol = 1e-14, maxit = 5000))
  for (i in 1:2) o <- optim(o$par, negQ, ..., method = "Nelder-Mead",
                            control = list(reltol = 1e-14, maxit = 5000))
  o$par
}
o4 <- tight(c(1, 0.5, log(0.05), -0.3), sig_cov = TRUE)
o3 <- tight(c(1, 0.5, log(0.05)))
sprintf("FULL model recovers the truth: largest error %.1e", max(abs(o4 - c(b0, b1, a0, a1))))
sprintf("NAIVE converges to: b1 %.4f (truth %.2f, %+.1f%%) | sigma %.5f (true range %.4f to %.4f)",
        o3[2], b1, 100*(o3[2]/b1 - 1), exp(o3[3]), exp(a0 + a1*2), exp(a0 - a1*2))
sprintf("implied mean abundance per site: truth %.4f | naive %.4f (%+.1f%%)",
        sum(wz*exp(b0 + b1*zg)), sum(wz*exp(o3[1] + o3[2]*zg)),
        100*(sum(wz*exp(o3[1] + o3[2]*zg))/sum(wz*exp(b0 + b1*zg)) - 1))

## ---- mc-coverage ----
M <- 200
r <- t(sapply(1:M, function(i) {
  dd <- sim_hds(52830 + i)
  a <- fit(c(1, 0.5, log(0.05), -0.3), y = dd$y, z = dd$z)
  b <- fit(c(1, 0.5, log(0.05)), y = dd$y, z = dd$z, sig_cov = FALSE)
  c(a$par[2], a$se[2], b$par[2], b$se[2],
    sum(exp(a$par[1] + a$par[2]*dd$z))/sum(dd$N),
    sum(exp(b$par[1] + b$par[2]*dd$z))/sum(dd$N))
}))
cvg <- function(e, se) mean(abs(e - b1) < 1.96*se)
sprintf("FULL : b1 %.4f (SD %.4f, mean SE %.4f) | CI coverage %.3f | N_hat/N %.4f",
        mean(r[, 1]), sd(r[, 1]), mean(r[, 2]), cvg(r[, 1], r[, 2]), mean(r[, 5]))
sprintf("NAIVE: b1 %.4f (SD %.4f, mean SE %.4f) | CI coverage %.3f | N_hat/N %.4f",
        mean(r[, 3]), sd(r[, 3]), mean(r[, 4]), cvg(r[, 3], r[, 4]), mean(r[, 6]))

## ---- fig-fit ----
ink <- "#16241d"; forest <- "#275139"; gold <- "#c9b458"; rust <- "#b5534e"
hi <- d$z > 0
obs <- rbind(data.frame(bin = 1:B, n = colSums(d$y[!hi, ]), k = "low cover", src = "observed"),
             data.frame(bin = 1:B, n = colSums(d$y[hi, ]),  k = "high cover", src = "observed"))
lam_h <- exp(f4$par[1] + f4$par[2]*d$z); sg_h <- exp(f4$par[3] + f4$par[4]*d$z)
Efit <- t(sapply(seq_len(R), function(i) lam_h[i]*pi_b(sg_h[i])))
fitd <- rbind(data.frame(bin = 1:B, n = colSums(Efit[!hi, ]), k = "low cover", src = "fitted"),
              data.frame(bin = 1:B, n = colSums(Efit[hi, ]),  k = "high cover", src = "fitted"))
p1 <- ggplot(obs, aes(bin, n, fill = k)) +
  geom_col(position = "dodge", alpha = 0.55) +
  geom_line(data = fitd, aes(bin, n, colour = k), linewidth = 0.9) +
  geom_point(data = fitd, aes(bin, n, colour = k), size = 1.6) +
  scale_fill_manual(values = c("low cover" = forest, "high cover" = rust)) +
  scale_colour_manual(values = c("low cover" = forest, "high cover" = rust), guide = "none") +
  scale_x_continuous(breaks = 1:B, labels = paste0(cut[-6]*1000, "-", cut[-1]*1000)) +
  labs(x = "distance bin (m)", y = "detections", fill = NULL,
       title = "Bars observed, lines fitted",
       subtitle = "one visit to each of 200 segments") +
  theme_minimal(base_size = 10) + te_canvas +
  theme(legend.position = "bottom", plot.title = element_text(colour = ink, face = "bold"),
        plot.subtitle = element_text(size = 8), axis.text.x = element_text(size = 6))
p2 <- ggplot(rbind(data.frame(v = r[, 1], k = "full model"),
                   data.frame(v = r[, 3], k = "detection held constant")),
             aes(v, fill = k)) +
  geom_histogram(bins = 32, position = "identity", alpha = 0.6) +
  geom_vline(xintercept = b1, colour = ink, linetype = 2, linewidth = 0.7) +
  scale_fill_manual(values = c("full model" = forest, "detection held constant" = gold)) +
  labs(x = "estimated habitat slope on abundance", y = "surveys", fill = NULL,
       title = "Where the slope lands", subtitle = "dashed: the truth (0.6)") +
  theme_minimal(base_size = 10) + te_canvas +
  theme(legend.position = "bottom", plot.title = element_text(colour = ink, face = "bold"),
        plot.subtitle = element_text(size = 8))
grid::grid.newpage()
grid::pushViewport(grid::viewport(layout = grid::grid.layout(1, 2)))
print(p1, vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 1))
print(p2, vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 2))

## ---- head-to-head-setup ----
s0 <- 0.03; Tv <- 3
pbar <- mu_hn(s0, w)/w
Eg2  <- mu_hn(s0/sqrt(2), w)/w          # E[g^2] under x ~ U(0, w)
sprintf("per-visit detection probability p = mu/w = %.5f", pbar)
sprintf("but g(x) across individuals: mean %.4f, SD %.4f -> intra-class correlation %.4f",
        pbar, sqrt(Eg2 - pbar^2), (Eg2 - pbar^2)/(pbar*(1 - pbar)))

## ---- head-to-head ----
sim_world <- function(seed, territorial) {
  set.seed(seed)
  z <- rnorm(R); N <- rpois(R, exp(b0 + b1*z))
  y1 <- matrix(0, R, B); cnt <- matrix(0, R, Tv)
  for (i in seq_len(R)) {
    if (N[i] == 0) next
    x <- runif(N[i], 0, w)                    # where the animals are
    for (t in seq_len(Tv)) {
      xt  <- if (territorial) x else runif(N[i], 0, w)
      det <- runif(N[i]) < g_hn(xt, s0)
      cnt[i, t] <- sum(det)
      if (t == 1) y1[i, ] <- tabulate(findInterval(xt[det], cut, rightmost.closed = TRUE), B)
    }
  }
  list(y1 = y1, cnt = cnt, z = z, N = N)
}
## N-mixture likelihood: the binomial coefficients do not move during the fit, so build once
nmix_setup <- function(cnt, Kmax = 90) {
  Ng <- 0:Kmax
  list(logC = sapply(Ng, function(N) rowSums(lchoose(N, cnt))),
       n = rowSums(cnt), Tv = ncol(cnt),
       Nmat = matrix(Ng, nrow(cnt), Kmax + 1, byrow = TRUE))
}
nll_nmix <- function(p, S, z) {
  lam <- exp(p[1] + p[2]*z); pp <- plogis(p[3])
  LP <- dpois(S$Nmat, lam, log = TRUE) + S$logC +
        S$n*log(pp) + (S$Tv*S$Nmat - S$n)*log1p(-pp)
  -sum(log(rowSums(exp(LP))))
}
run <- function(seed, territorial) {
  dw <- sim_world(seed, territorial)
  fh <- fit(c(1, 0.5, log(0.04)), y = dw$y1, z = dw$z, sig_cov = FALSE)
  S  <- nmix_setup(dw$cnt)
  fn <- optim(c(1, 0.5, qlogis(0.4)), nll_nmix, S = S, z = dw$z, method = "Nelder-Mead")
  fn <- optim(fn$par, nll_nmix, S = S, z = dw$z, method = "Nelder-Mead")
  c(hds_b1 = fh$par[2], hds_N = sum(exp(fh$par[1] + fh$par[2]*dw$z))/sum(dw$N),
    nm_b1 = fn$par[2], nm_p = plogis(fn$par[3]),
    nm_N = sum(exp(fn$par[1] + fn$par[2]*dw$z))/sum(dw$N),
    n1 = sum(dw$y1), n3 = sum(dw$cnt),
    vv = mean(apply(dw$cnt, 1, var)))
}
for (terr in c(TRUE, FALSE)) {
  a <- run(4282, terr)
  cat(sprintf("\n--- %s world (seed 4282): %.0f detections in 1 visit, %.0f across 3 visits\n",
              if (terr) "TERRITORIAL: animals hold position" else "MOBILE: animals redraw position",
              a["n1"], a["n3"]))
  cat(sprintf("    distances, 1 visit : b1 %.4f | N_hat/N %.4f\n", a["hds_b1"], a["hds_N"]))
  cat(sprintf("    counts, 3 visits   : b1 %.4f | p_hat %.4f (true %.4f) | N_hat/N %.4f\n",
              a["nm_b1"], a["nm_p"], pbar, a["nm_N"]))
}

## ---- head-to-head-mc ----
M2 <- 100
mc <- lapply(c(TRUE, FALSE), function(terr)
  t(sapply(1:M2, function(i) run(52840 + i + 1000*terr, terr))))
names(mc) <- c("territorial", "mobile")
for (k in names(mc)) {
  x <- mc[[k]]
  cat(sprintf("\n%s world, %d surveys: %.0f detections in 1 visit vs %.0f across 3\n",
              k, M2, mean(x[, "n1"]), mean(x[, "n3"])))
  cat(sprintf("  distances, 1 visit : b1 %.4f (SD %.4f) | N_hat/N %.4f (SD %.4f)\n",
              mean(x[, "hds_b1"]), sd(x[, "hds_b1"]), mean(x[, "hds_N"]), sd(x[, "hds_N"])))
  cat(sprintf("  counts, 3 visits   : b1 %.4f (SD %.4f) | p_hat %.4f | N_hat/N %.4f (SD %.4f)\n",
              mean(x[, "nm_b1"]), sd(x[, "nm_b1"]), mean(x[, "nm_p"]),
              mean(x[, "nm_N"]), sd(x[, "nm_N"])))
}
sprintf("mean within-site variance across the 3 visits: territorial %.4f | mobile %.4f",
        mean(mc$territorial[, "vv"]), mean(mc$mobile[, "vv"]))
sprintf("territorial N_hat/N should be pbar/p_hat = %.4f; simulated %.4f",
        pbar/mean(mc$territorial[, "nm_p"]), mean(mc$territorial[, "nm_N"]))

## ---- fig-h2h ----
dfN <- do.call(rbind, lapply(names(mc), function(k) rbind(
  data.frame(v = mc[[k]][, "hds_N"], world = k, m = "distances, 1 visit"),
  data.frame(v = mc[[k]][, "nm_N"],  world = k, m = "counts, 3 visits"))))
p1 <- ggplot(dfN, aes(world, v, fill = m)) +
  geom_hline(yintercept = 1, colour = ink, linetype = 2) +
  geom_boxplot(alpha = 0.6, outlier.size = 0.5, linewidth = 0.35) +
  scale_fill_manual(values = c("distances, 1 visit" = forest, "counts, 3 visits" = gold)) +
  labs(x = NULL, y = "estimated N / true N", fill = NULL,
       title = "Same animals, same detection function",
       subtitle = "dashed: no bias") +
  theme_minimal(base_size = 10) + te_canvas +
  theme(legend.position = "bottom", plot.title = element_text(colour = ink, face = "bold"),
        plot.subtitle = element_text(size = 8))
dfp <- do.call(rbind, lapply(names(mc), function(k)
  data.frame(v = mc[[k]][, "nm_p"], world = k)))
p2 <- ggplot(dfp, aes(v, fill = world)) +
  geom_histogram(bins = 30, position = "identity", alpha = 0.6) +
  geom_vline(xintercept = pbar, colour = ink, linetype = 2, linewidth = 0.7) +
  scale_fill_manual(values = c(territorial = rust, mobile = forest)) +
  labs(x = "fitted per-visit detection probability", y = "surveys", fill = NULL,
       title = "What the N-mixture thinks p is",
       subtitle = sprintf("dashed: the truth (%.3f)", pbar)) +
  theme_minimal(base_size = 10) + te_canvas +
  theme(legend.position = "bottom", plot.title = element_text(colour = ink, face = "bold"),
        plot.subtitle = element_text(size = 8))
grid::grid.newpage()
grid::pushViewport(grid::viewport(layout = grid::grid.layout(1, 2)))
print(p1, vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 1))
print(p2, vp = grid::viewport(layout.pos.row = 1, layout.pos.col = 2))

## ---- thumb ----
thumb_title <- paste(strwrap("Same animals, same detection function", 46), collapse = "\n")
ggsave("thumbnail.png",
       p1 + labs(title = thumb_title) +
         theme(plot.title = element_text(size = 13, face = "bold"),
               plot.subtitle = element_text(size = 9.5),
               plot.margin = margin(8, 14, 8, 8)),
       width = 7.5, height = 5.4, dpi = 100, bg = "#f5f4ee")
