# Validating a step selection model
# https://tidyecology.com/posts/validating-step-selection/
#
# 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-05-18

## ---- setup ----
library(ggplot2)
te_canvas <- theme(plot.background  = element_rect(fill = "#f5f4ee", colour = NA),
                   panel.background = element_rect(fill = "#f5f4ee", colour = NA))
library(grid)

res_raw <- function(x, y)
  1.4 * exp(-((x - 38)^2 + (y - 62)^2) / (2 * 24^2)) +
  1.1 * exp(-((x - 68)^2 + (y - 40)^2) / (2 * 26^2)) -
  0.00030 * ((x - 50)^2 + (y - 50)^2)
cov_raw <- function(x, y) sin(x / 15) * cos(y / 13) + 0.6 * sin((x + y) / 19)
gx <- seq(0, 100, length.out = 130); gd <- expand.grid(x = gx, y = gx)
mr <- mean(res_raw(gd$x, gd$y)); sr <- sd(res_raw(gd$x, gd$y))
mc <- mean(cov_raw(gd$x, gd$y)); sc <- sd(cov_raw(gd$x, gd$y))
resource <- function(x, y) (res_raw(x, y) - mr) / sr
cover    <- function(x, y) (cov_raw(x, y) - mc) / sc

rvm <- function(n, mu, kappa) {
  out <- numeric(n); a <- 1 + sqrt(1 + 4 * kappa^2)
  b <- (a - sqrt(2 * a)) / (2 * kappa); r <- (1 + b^2) / (2 * b)
  for (i in seq_len(n)) {
    repeat {
      u1 <- runif(1); u2 <- runif(1); u3 <- runif(1)
      z <- cos(pi * u1); f <- (1 + r * z) / (r + z); cc <- kappa * (r - f)
      if (cc * (2 - cc) - u2 > 0 || log(cc / u2) + 1 - cc >= 0) break
    }
    out[i] <- sign(u3 - 0.5) * acos(f) + mu
  }
  ((out + pi) %% (2 * pi)) - pi
}

## ---- simulate ----
b_res <- 0.8; b_cov <- 0.0; b_cov2 <- -0.7
k_move <- 2.0; th_move <- 5.5; kap_move <- 0.9
n <- 1200; M <- 300
set.seed(2718)
pos <- matrix(NA_real_, n + 1, 2); pos[1, ] <- c(45, 50)
heading <- numeric(n + 1); heading[1] <- runif(1, -pi, pi)
for (i in 1:n) {
  L <- rgamma(M, shape = k_move, scale = th_move); a <- rvm(M, 0, kap_move)
  ang <- heading[i] + a
  ex <- pos[i, 1] + L * cos(ang); ey <- pos[i, 2] + L * sin(ang)
  cv <- cover(ex, ey)
  w <- exp(b_res * resource(ex, ey) + b_cov * cv + b_cov2 * cv^2)
  j <- sample.int(M, 1, prob = w)
  pos[i + 1, ] <- c(ex[j], ey[j]); heading[i + 1] <- ang[j]
}
dx <- diff(pos[, 1]); dy <- diff(pos[, 2])
obs_l <- sqrt(dx^2 + dy^2); bearing <- atan2(dy, dx)
obs_turn <- ((diff(bearing) + pi) %% (2 * pi)) - pi
m1 <- mean(obs_l); v1 <- var(obs_l); k0 <- m1^2 / v1; th0 <- v1 / m1
Cc <- mean(cos(obs_turn)); Ss <- mean(sin(obs_turn)); Rbar <- sqrt(Cc^2 + Ss^2); mu0 <- atan2(Ss, Cc)
kap0 <- if (Rbar < 0.85) -0.4 + 1.39 * Rbar + 0.43 / (1 - Rbar) else 1 / (Rbar^3 - 4 * Rbar^2 + 3 * Rbar)

## ---- strata ----
K <- 15; set.seed(11223)
use_idx <- 2:n; n_str <- length(use_idx); Sp <- K + 1
res_m <- cov_m <- matrix(NA_real_, Sp, n_str)     # row 1 = used, rows 2..Sp = available
for (s in seq_along(use_idx)) {
  i <- use_idx[s]
  res_m[1, s] <- resource(pos[i + 1, 1], pos[i + 1, 2])
  cov_m[1, s] <- cover(pos[i + 1, 1], pos[i + 1, 2])
  Ls <- rgamma(K, shape = k0, scale = th0); As <- rvm(K, mu0, kap0)
  ang <- heading[i] + As
  ax <- pos[i, 1] + Ls * cos(ang); ay <- pos[i, 2] + Ls * sin(ang)
  res_m[2:Sp, s] <- resource(ax, ay); cov_m[2:Sp, s] <- cover(ax, ay)
}

## ---- fit ----
fit_clogit <- function(mats) {
  p <- length(mats)
  nll <- function(b) {
    eta <- matrix(0, Sp, n_str)
    for (j in 1:p) eta <- eta + b[j] * mats[[j]]
    -sum(eta[1, ] - log(colSums(exp(eta))))
  }
  o <- optim(rep(0, p), nll, method = "BFGS", hessian = TRUE, control = list(reltol = 1e-10))
  list(b = o$par, V = solve(o$hessian))
}
fit_full <- fit_clogit(list(res_m, cov_m, cov_m^2))
fit_lin  <- fit_clogit(list(res_m, cov_m))
se_lin <- sqrt(diag(fit_lin$V))

## ---- uhc-fn ----
uhc_env <- function(fit, mats, focal, grid, B = 300) {
  p <- length(mats); Lc <- chol(fit$V)
  dens <- matrix(NA_real_, length(grid), B)
  for (bb in 1:B) {
    beta <- as.vector(fit$b + t(Lc) %*% rnorm(p))     # draw from the sampling distribution
    eta <- matrix(0, Sp, n_str)
    for (j in 1:p) eta <- eta + beta[j] * mats[[j]]
    P <- exp(eta); P <- sweep(P, 2, colSums(P), "/")
    cumP <- apply(P, 2, cumsum)
    u <- runif(n_str)
    idx <- colSums(sweep(cumP, 2, u, "<")) + 1        # one sampled step per stratum
    picked <- focal[cbind(idx, seq_len(n_str))]
    dens[, bb] <- density(picked, from = min(grid), to = max(grid),
                          n = length(grid), bw = 0.25)$y
  }
  data.frame(x = grid, lo = apply(dens, 1, quantile, 0.025),
             hi = apply(dens, 1, quantile, 0.975))
}

grid <- seq(-2.6, 2.6, length.out = 200)
obs_dens <- density(cov_m[1, ], from = min(grid), to = max(grid), n = length(grid), bw = 0.25)
env_full <- uhc_env(fit_full, list(res_m, cov_m, cov_m^2), cov_m, grid)
env_lin  <- uhc_env(fit_lin,  list(res_m, cov_m),          cov_m, grid)
frac_out <- function(env) 100 * mean(obs_dens$y < env$lo - 1e-8 | obs_dens$y > env$hi + 1e-8)

## ---- fig-uhc ----
mk <- function(env, ttl) {
  ggplot() +
    geom_ribbon(data = env, aes(x, ymin = lo, ymax = hi), fill = "#93a87f", alpha = 0.45) +
    geom_line(data = data.frame(x = obs_dens$x, y = obs_dens$y), aes(x, y),
              colour = "#b5534e", linewidth = 0.8) +
    labs(x = "cover at used step", y = "density", title = ttl) +
    theme_minimal(base_size = 11) + te_canvas +
    theme(panel.grid.minor = element_blank(), plot.title = element_text(size = 10, colour = "#46604a"))
}
grid.newpage(); pushViewport(viewport(layout = grid.layout(1, 2)))
print(mk(env_full, "Full model (resource + cover + cover^2)"), vp = viewport(layout.pos.col = 1))
print(mk(env_lin, "Linear model (resource + cover)"), vp = viewport(layout.pos.col = 2))

## ---- fig-response ----
cc <- seq(-2.4, 2.4, length.out = 200)
rss_df <- rbind(
  data.frame(model = "true", cover = cc, y = b_cov * cc + b_cov2 * cc^2),
  data.frame(model = "full fit", cover = cc, y = fit_full$b[2] * cc + fit_full$b[3] * cc^2),
  data.frame(model = "linear fit", cover = cc, y = fit_lin$b[2] * cc))
rss_df$model <- factor(rss_df$model, levels = c("true", "full fit", "linear fit"))
ggplot(rss_df, aes(cover, y, colour = model, linetype = model)) +
  geom_hline(yintercept = 0, colour = "#dad9ca") +
  geom_line(linewidth = 0.8) +
  scale_colour_manual(values = c("true" = "#16241d", "full fit" = "#275139", "linear fit" = "#b5534e")) +
  scale_linetype_manual(values = c("true" = "22", "full fit" = "solid", "linear fit" = "solid")) +
  labs(x = "cover (standardised)", y = "log relative selection", colour = NULL, linetype = NULL,
       title = "A straight line cannot capture a peak") +
  theme_minimal(base_size = 11) + te_canvas +
  theme(panel.grid.minor = element_blank(), legend.position = "inside", legend.position.inside = c(0.5, 0.2),
        legend.direction = "horizontal",
        legend.background = element_rect(fill = "#ffffff", colour = NA),
        plot.title = element_text(size = 10, colour = "#46604a"))
