# Bayesian N-mixture abundance
# https://tidyecology.com/posts/bayesian-n-mixture-abundance/
#
# 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-09-30

## ---- sim ----
set.seed(5027)
R <- 150L                 # sites
K <- 4L                   # repeat visits
lambda.true <- 2.8
p.true <- 0.37

N.true <- rpois(R, lambda.true)               # latent site abundance
y <- matrix(0L, R, K)                         # observed counts
for (i in 1:R) y[i, ] <- rbinom(K, N.true[i], p.true)
ymax <- apply(y, 1, max)                       # naive per-site abundance

## ---- naive ----
naive.lambda <- mean(ymax)
naive.total <- sum(ymax)
true.total <- sum(N.true)

## ---- gibbs ----
n.iter <- 12000L; burn <- 2000L
lam.s <- numeric(n.iter); p.s <- numeric(n.iter); Ntot.s <- numeric(n.iter)
Nmax <- max(ymax) + 40L                        # truncation for the latent grid
lam <- max(naive.lambda, 1); p <- 0.5; N <- pmax(ymax, 1L)

for (it in 1:n.iter) {
  # draw latent N_i on the grid max(y_i)..Nmax
  for (i in 1:R) {
    grid <- ymax[i]:Nmax
    logw <- dpois(grid, lam, log = TRUE)
    for (j in 1:K) logw <- logw + dbinom(y[i, j], grid, p, log = TRUE)
    w <- exp(logw - max(logw))
    N[i] <- sample(grid, 1, prob = w)
  }
  # lambda | N : conjugate Gamma
  lam <- rgamma(1, 0.001 + sum(N), 0.001 + R)
  # p | N, y : conjugate Beta over detections and opportunities
  p <- rbeta(1, 1 + sum(y), 1 + sum(N) * K - sum(y))
  lam.s[it] <- lam; p.s[it] <- p; Ntot.s[it] <- sum(N)
}
keep <- (burn + 1):n.iter
lam.post <- lam.s[keep]; p.post <- p.s[keep]; Ntot.post <- Ntot.s[keep]

## ---- summary ----
lam.med <- median(lam.post); lam.ci <- quantile(lam.post, c(.025, .975))
p.med <- median(p.post); p.ci <- quantile(p.post, c(.025, .975))
Nt.med <- median(Ntot.post); Nt.ci <- quantile(Ntot.post, c(.025, .975))

## ---- fig-post ----
library(ggplot2); library(dplyr)
ink <- "#16241d"; forest <- "#275139"; rust <- "#b5534e"; gold <- "#cda23f"; line <- "#dad9ca"
theme_te <- theme_minimal(base_size = 12) +
  theme(
        plot.background  = element_rect(fill = "#f5f4ee", colour = NA),
        panel.background = element_rect(fill = "#f5f4ee", colour = NA),
        panel.grid.minor = element_blank(),
        panel.grid.major = element_line(colour = line, linewidth = 0.3),
        plot.title = element_text(colour = ink, face = "bold", size = 13),
        plot.subtitle = element_text(colour = "#5d6b61", size = 10),
        axis.title = element_text(colour = ink), axis.text = element_text(colour = "#46604a"),
        strip.text = element_text(colour = ink, face = "bold"), legend.position = "none")
d1 <- bind_rows(
  data.frame(param = "Abundance rate (lambda)", value = lam.post),
  data.frame(param = "Detection probability (p)", value = p.post))
truth <- data.frame(param = c("Abundance rate (lambda)", "Detection probability (p)"),
                    x = c(lambda.true, p.true))
naive.df <- data.frame(param = "Abundance rate (lambda)", x = naive.lambda)
ggplot(d1, aes(value)) +
  geom_density(fill = forest, colour = forest, alpha = 0.35, linewidth = 0.5) +
  geom_vline(data = truth, aes(xintercept = x), colour = rust, linewidth = 0.7) +
  geom_vline(data = naive.df, aes(xintercept = x), colour = gold, linewidth = 0.7, linetype = "dashed") +
  facet_wrap(~param, scales = "free") +
  labs(title = "Posterior abundance rate and detection, with the naive max count",
       subtitle = "Red: simulating truth. Gold dashed: mean per-site maximum count (abundance panel).",
       x = NULL, y = "Posterior density") + theme_te

## ---- fig-total ----
sage <- "#93a87f"; paper <- "#f5f4ee"
ggplot(data.frame(value = Ntot.post), aes(value)) +
  geom_histogram(aes(y = after_stat(density)), bins = 45, fill = sage, colour = paper, linewidth = 0.15) +
  geom_vline(xintercept = true.total, colour = rust, linewidth = 0.8) +
  geom_vline(xintercept = naive.total, colour = gold, linewidth = 0.8, linetype = "dashed") +
  labs(title = "Posterior total abundance across all sites",
       subtitle = "Red: true total. Gold dashed: naive total from summed maximum counts.",
       x = "Total individuals", y = "Posterior density") + theme_te
