# Detection covariates and checking in SCR
# https://tidyecology.com/posts/scr-detection-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-10-07

## ---- setup ----
library(ggplot2); library(tidyr)
paper <- "#f5f4ee"; ink <- "#16241d"; body <- "#2c3a31"; forest <- "#275139"
labelc <- "#46604a"; sage <- "#93a87f"; linec <- "#dad9ca"; faint <- "#5d6b61"
gold <- "#c9b458"; rust <- "#b5534e"
theme_te <- function(base_size = 12) {
  theme_minimal(base_size = base_size) +
    theme(
      plot.background = element_rect(fill = paper, colour = NA),
      panel.background = element_rect(fill = paper, colour = NA),
      panel.grid.minor = element_blank(),
      panel.grid.major = element_line(colour = linec, linewidth = 0.3),
      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 = labelc),
      legend.title = element_text(colour = body), legend.text = element_text(colour = body),
      strip.text = element_text(colour = ink, face = "bold")
    )
}

## ---- sci-tex-helper ----
sci_tex <- function(x, digits = 2) {
  s <- formatC(x, format = "e", digits = digits)
  paste0("$", sub("e.*", "", s), " \\times 10^{", as.integer(sub(".*e", "", s)), "}$")
}

## ---- simulate ----
set.seed(4130)
tr    <- seq(1, 8, by = 1)
traps <- as.matrix(expand.grid(x = tr, y = tr)); J <- nrow(traps)
K     <- 6
a0_true <- qlogis(0.18); a1_true <- 1.1; sig_true <- 0.75
buffer  <- 3
xl <- range(traps[, 1]) + c(-buffer, buffer); yl <- range(traps[, 2]) + c(-buffer, buffer)
A  <- diff(xl) * diff(yl); N_true <- 90; D_true <- N_true / A

effort <- rnorm(J)                                   # standardised effort per trap
g0_j   <- plogis(a0_true + a1_true * effort)

sx <- runif(N_true, xl[1], xl[2]); sy <- runif(N_true, yl[1], yl[2])
d2 <- outer(sx, traps[, 1], "-")^2 + outer(sy, traps[, 2], "-")^2
P  <- sweep(exp(-d2 / (2 * sig_true^2)), 2, g0_j, "*")
Yf <- matrix(rbinom(N_true * J, K, P), N_true, J)
cc <- rowSums(Yf) > 0; Y <- Yf[cc, , drop = FALSE]; n <- nrow(Y)
trap_tot <- colSums(Y)

## ---- fit ----
hx <- diff(xl) / 35; hy <- diff(yl) / 35            # 35 x 35 equal cells
gx <- seq(xl[1] + hx / 2, xl[2] - hx / 2, length.out = 35)   # cell centres
gy <- seq(yl[1] + hy / 2, yl[2] - hy / 2, length.out = 35)
G  <- as.matrix(expand.grid(x = gx, y = gy))
d2G <- outer(G[, 1], traps[, 1], "-")^2 + outer(G[, 2], traps[, 2], "-")^2
Cc  <- rowSums(lchoose(K, Y))

nll_null <- function(th) {                           # logD, qlogis g0, log sigma
  Dd <- exp(th[1]); g0 <- plogis(th[2]); sg <- exp(th[3])
  Pg <- g0 * exp(-d2G / (2 * sg^2))
  pb <- mean(1 - exp(rowSums(K * log1p(-Pg)))); if (pb <= 0) return(1e10)
  LM <- Y %*% t(log(Pg)) + (K - Y) %*% t(log1p(-Pg)) + Cc
  m  <- apply(LM, 1, max); lf <- m + log(rowMeans(exp(LM - m)))
  -(dpois(n, Dd * A * pb, log = TRUE) + sum(lf) - n * log(pb))
}
nll_full <- function(th) {                           # logD, a0, a1, log sigma
  Dd <- exp(th[1]); gj <- plogis(th[2] + th[3] * effort); sg <- exp(th[4])
  Pg <- sweep(exp(-d2G / (2 * sg^2)), 2, gj, "*")
  pb <- mean(1 - exp(rowSums(K * log1p(-Pg)))); if (pb <= 0) return(1e10)
  LM <- Y %*% t(log(Pg)) + (K - Y) %*% t(log1p(-Pg)) + Cc
  m  <- apply(LM, 1, max); lf <- m + log(rowMeans(exp(LM - m)))
  -(dpois(n, Dd * A * pb, log = TRUE) + sum(lf) - n * log(pb))
}
f0 <- optim(c(log(n/(A*0.4)), qlogis(0.18), log(0.8)), nll_null,
            method = "BFGS", hessian = TRUE, control = list(maxit = 500, reltol = 1e-10))
f1 <- optim(c(log(n/(A*0.4)), a0_true, 1.0, log(0.8)), nll_full,
            method = "BFGS", hessian = TRUE, control = list(maxit = 500, reltol = 1e-10))
stopifnot(f1$convergence == 0)   # check before inverting the Hessian
a1h <- f1$par[3]; se1 <- sqrt(diag(solve(f1$hessian)))
aic0 <- 2*f0$value + 2*length(f0$par); aic1 <- 2*f1$value + 2*length(f1$par)
lrt  <- 2*(f0$value - f1$value); lrt_p <- pchisq(lrt, 1, lower.tail = FALSE)
D0 <- exp(f0$par[1]); g0_0 <- plogis(f0$par[2]); sig0 <- exp(f0$par[3])
D1 <- exp(f1$par[1]); sig1 <- exp(f1$par[4])

## ---- gof ----
Tstat <- function(Ymat) cor(colSums(Ymat), effort)
T_obs <- Tstat(Y)

sim_data <- function(D, sg, gj) {                    # gj is the trap-level g0 vector
  Ns <- rpois(1, D * A)
  xs <- runif(Ns, xl[1], xl[2]); ys <- runif(Ns, yl[1], yl[2])
  d2s <- outer(xs, traps[, 1], "-")^2 + outer(ys, traps[, 2], "-")^2
  Yz  <- matrix(rbinom(Ns * J, K, sweep(exp(-d2s / (2 * sg^2)), 2, gj, "*")), Ns, J)
  Yz[rowSums(Yz) > 0, , drop = FALSE]
}
set.seed(77)
B  <- 500
T0 <- replicate(B, Tstat(sim_data(D0, sig0, rep(g0_0, J))))               # null model
T1 <- replicate(B, Tstat(sim_data(D1, sig1, plogis(f1$par[2] + a1h * effort))))  # full model
p0 <- mean(T0 >= T_obs); p1 <- mean(T1 >= T_obs)

## ---- fig-effort ----
td <- data.frame(effort = effort, total = trap_tot)
p1p <- ggplot(td, aes(effort, total)) +
  geom_hline(yintercept = mean(trap_tot), colour = rust, linetype = "dashed", linewidth = 0.7) +
  geom_smooth(method = "lm", formula = y ~ x, se = FALSE, colour = forest, linewidth = 0.9) +
  geom_point(colour = ink, size = 2, alpha = 0.8) +
  annotate("text", x = min(effort), y = mean(trap_tot), label = "constant-g0 prediction",
           vjust = -0.7, hjust = 0, colour = rust, size = 3.2) +
  labs(x = "trap effort (standardised)", y = "total detections at trap",
       title = "High-effort traps detect more",
       subtitle = "the constant-detection model has no way to represent this") +
  theme_te()
p1p

## ---- fig-gof ----
gof <- rbind(data.frame(stat = T0, model = "constant g0"),
             data.frame(stat = T1, model = "effort on g0"))
labs_df <- data.frame(model = c("constant g0", "effort on g0"),
                      lab = c(if (p0 == 0) paste0("p < ", sprintf("%.3f", 1 / B)) else paste0("p = ", round(p0, 3)),
                              paste0("p = ", round(p1, 3))))
p2p <- ggplot(gof, aes(stat)) +
  geom_histogram(bins = 34, fill = sage, colour = paper, linewidth = 0.2) +
  geom_vline(xintercept = T_obs, colour = ink, linewidth = 0.8) +
  geom_text(data = labs_df, aes(x = -Inf, y = Inf, label = lab),
            hjust = -0.2, vjust = 1.6, colour = ink, size = 3.4, inherit.aes = FALSE) +
  facet_wrap(~ model, scales = "free_x") +
  labs(x = "correlation of trap totals with effort", y = "simulated datasets",
       title = "Only the effort model reproduces the effort association",
       subtitle = "observed value (line) in the tail for constant g0, inside for effort") +
  theme_te()
p2p

## ---- thumb ----
ggsave("thumbnail.png", p2p + labs(title = NULL, subtitle = NULL),
       width = 7.5, height = 5.4, dpi = 100, bg = paper)
