# Covariates in dynamic occupancy models
# https://tidyecology.com/posts/dynamic-occupancy-covariates/
#
# 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-11-14

## ---- sim ----
set.seed(140)
R <- 400; Tn <- 6; K <- 4
size <- as.numeric(scale(runif(R, 0, 1)))
psi1 <- 0.50
g0 <- qlogis(0.18); g1 <-  0.9      # colonisation rises with size
e0 <- qlogis(0.35); e1 <- -0.9      # extinction falls with size
p0 <- qlogis(0.30); p1 <-  0.7      # detection rises with size
gam <- plogis(g0 + g1 * size); ext <- plogis(e0 + e1 * size); pv <- plogis(p0 + p1 * size)

z <- matrix(0L, R, Tn); z[, 1] <- rbinom(R, 1, psi1)
for (t in 2:Tn) z[, t] <- rbinom(R, 1, ifelse(z[, t - 1] == 1, 1 - ext, gam))
d <- matrix(0L, R, Tn); for (t in 1:Tn) d[, t] <- rbinom(R, K, z[, t] * pv)
round(c(mean_gamma = mean(gam), mean_eps = mean(ext), mean_p = mean(pv)), 3)

## ---- likelihood ----
emit <- function(dt, p, K) c(as.numeric(dt == 0), p^dt * (1 - p)^(K - dt))
site_ll <- function(dv, init, Phi, p, K) {
  a <- init * emit(dv[1], p, K); s <- sum(a); ll <- log(s); a <- a / s
  for (t in 2:length(dv)) {
    a <- as.vector(a %*% Phi) * emit(dv[t], p, K)
    s <- sum(a); ll <- ll + log(s); a <- a / s
  }
  ll
}
mk_nll <- function(p_constant) function(par) {
  ps <- plogis(par[1])
  ga <- plogis(par[2] + par[3] * size); ep <- plogis(par[4] + par[5] * size)
  pvv <- if (p_constant) rep(plogis(par[6]), R) else plogis(par[6] + par[7] * size)
  tot <- 0
  for (i in 1:R) {
    Phi <- matrix(c(1 - ga[i], ga[i], ep[i], 1 - ep[i]), 2, 2, byrow = TRUE)
    tot <- tot + site_ll(d[i, ], c(1 - ps, ps), Phi, pvv[i], K)
  }
  -tot
}
start <- c(0, g0, 0.3, e0, -0.3, p0, 0.3)
fit_full <- optim(start,      mk_nll(FALSE), method = "BFGS", hessian = TRUE)
stopifnot(fit_full$convergence == 0)   # check before inverting the Hessian
se_full  <- sqrt(diag(solve(fit_full$hessian)))
pf <- fit_full$par
round(c(gamma_b1 = pf[3], eps_b1 = pf[5], p_b1 = pf[7],
        se_gamma_b1 = se_full[3], se_eps_b1 = se_full[5], se_p_b1 = se_full[7]), 3)

## ---- fig-rates ----
suppressMessages({library(ggplot2); library(dplyr)})
theme_te <- function(bs = 12) theme_minimal(base_size = bs) + theme(
  plot.background = element_rect(fill = "#f5f4ee", colour = NA),
  panel.background = element_rect(fill = "#f5f4ee", colour = NA),
  panel.grid.major = element_line(colour = "#dad9ca", linewidth = 0.3),
  panel.grid.minor = element_blank(),
  axis.text = element_text(colour = "#2c3a31"), axis.title = element_text(colour = "#16241d"),
  plot.title = element_text(colour = "#16241d", face = "bold"),
  plot.subtitle = element_text(colour = "#46604a"),
  legend.text = element_text(colour = "#2c3a31"), legend.position = "top",
  legend.key = element_rect(fill = "#f5f4ee", colour = NA))

sg <- seq(min(size), max(size), length = 80)
rate_df <- bind_rows(
  data.frame(size = sg, val = plogis(pf[2] + pf[3] * sg), rate = "Colonisation", kind = "Fitted"),
  data.frame(size = sg, val = plogis(g0 + g1 * sg),        rate = "Colonisation", kind = "Truth"),
  data.frame(size = sg, val = plogis(pf[4] + pf[5] * sg), rate = "Extinction",   kind = "Fitted"),
  data.frame(size = sg, val = plogis(e0 + e1 * sg),        rate = "Extinction",   kind = "Truth"))
ggplot(rate_df, aes(size, val, colour = rate, linetype = kind)) +
  geom_line(linewidth = 0.9) +
  scale_colour_manual(values = c("Colonisation" = "#2f8f63", "Extinction" = "#b5534e")) +
  scale_linetype_manual(values = c("Fitted" = "solid", "Truth" = "22")) +
  coord_cartesian(ylim = c(0, 0.75)) +
  labs(x = "Patch size (standardised)", y = "Per-season rate", colour = NULL, linetype = NULL) +
  theme_te()

## ---- naive-fit ----
fit_naive <- optim(start[1:6], mk_nll(TRUE), method = "BFGS", hessian = TRUE)
pn <- fit_naive$par
dAIC <- (2 * fit_naive$value + 12) - (2 * fit_full$value + 14)
chi2 <- 2 * (fit_naive$value - fit_full$value)
round(c(naive_gamma_b1 = pn[3], naive_eps_b1 = pn[5],
        full_eps_b1 = pf[5], slope_ratio = pn[5] / pf[5],
        dAIC = dAIC, chi2 = chi2), 3)

## ---- fig-confound ----
ext_df <- bind_rows(
  data.frame(size = sg, val = plogis(e0 + e1 * sg),        model = "Truth"),
  data.frame(size = sg, val = plogis(pf[4] + pf[5] * sg), model = "Full (detection ~ size)"),
  data.frame(size = sg, val = plogis(pn[4] + pn[5] * sg), model = "Naive (constant detection)"))
ext_df$model <- factor(ext_df$model,
  levels = c("Truth", "Full (detection ~ size)", "Naive (constant detection)"))
ggplot(ext_df, aes(size, val, colour = model)) +
  geom_line(linewidth = 1) +
  scale_colour_manual(values = c("Truth" = "#b5534e",
    "Full (detection ~ size)" = "#275139", "Naive (constant detection)" = "#cda23f")) +
  coord_cartesian(ylim = c(0, 0.95)) +
  labs(x = "Patch size (standardised)", y = "Per-season extinction", colour = NULL) +
  theme_te() + guides(colour = guide_legend(nrow = 2))
