# Evaluating species distribution models in R
# https://tidyecology.com/posts/evaluating-sdm-performance/
#
# 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(terra)
library(dplyr)
library(tidyr)
library(ggplot2)

## ---- train ----
pal_paper <- "#f5f4ee"; pal_ink <- "#16241d"; pal_body <- "#2c3a31"
pal_forest <- "#275139"; pal_label <- "#46604a"; pal_line <- "#dad9ca"; pal_faint <- "#5d6b61"
pal_gold <- "#c9b458"; pal_red <- "#b5534e"
theme_te <- function(base_size = 12) {
  theme_minimal(base_size = base_size) +
    theme(text = element_text(colour = pal_body),
          plot.title = element_text(colour = pal_ink, face = "bold", size = rel(1.05)),
          plot.subtitle = element_text(colour = pal_faint),
          axis.title = element_text(colour = pal_label), axis.text = element_text(colour = pal_body),
          panel.grid.minor = element_blank(),
          panel.grid.major = element_line(colour = pal_line, linewidth = 0.3),
          strip.text = element_text(colour = pal_ink, face = "bold"),
          legend.title = element_text(colour = pal_label),
          plot.background = element_rect(fill = pal_paper, colour = NA),
          panel.background = element_rect(fill = "#f5f4ee", colour = NA),
          legend.key = element_rect(fill = pal_paper, colour = NA), legend.position = "top")
}
set.seed(42)
r_template <- rast(nrows = 80, ncols = 80, xmin = 0, xmax = 80, ymin = 0, ymax = 80)
xy <- xyFromCell(r_template, 1:ncell(r_template))
temp_trend <- 22 - 0.18 * xy[, "y"]
temp_noise <- rast(r_template); values(temp_noise) <- rnorm(ncell(r_template), 0, 2)
temp_noise <- focal(temp_noise, w = 9, fun = mean, na.rm = TRUE)
temp <- rast(r_template); values(temp) <- temp_trend; temp <- temp + temp_noise; names(temp) <- "temp"
prec_trend <- 500 + 5 * xy[, "x"]
prec_noise <- rast(r_template); values(prec_noise) <- rnorm(ncell(r_template), 0, 60)
prec_noise <- focal(prec_noise, w = 9, fun = mean, na.rm = TRUE)
prec <- rast(r_template); values(prec) <- prec_trend; prec <- prec + prec_noise; names(prec) <- "prec"
temp_v <- values(temp)[, 1]; prec_v <- values(prec)[, 1]
suit_true <- exp(-((temp_v - 15)^2) / (2 * 2.2^2)) * exp(-((prec_v - 800)^2) / (2 * 110^2))
set.seed(101); pres_cells <- sample(ncell(r_template), 300, prob = suit_true, replace = FALSE)
set.seed(202); bg_cells <- sample(ncell(r_template), 1000)
train <- rbind(data.frame(pa = 1, temp = temp_v[pres_cells], prec = prec_v[pres_cells]),
               data.frame(pa = 0, temp = temp_v[bg_cells],  prec = prec_v[bg_cells]))
train <- train[complete.cases(train), ]
m <- glm(pa ~ poly(temp, 2) + poly(prec, 2), data = train, family = binomial)

## ---- test-set ----
set.seed(777)
test_cells <- sample(ncell(r_template), 800)
p_occupy <- 0.75 * (suit_true[test_cells] / max(suit_true))
y_test <- rbinom(length(test_cells), 1, p_occupy)
score <- predict(m, data.frame(temp = temp_v[test_cells], prec = prec_v[test_cells]), type = "response")
ok <- complete.cases(score, y_test); y_test <- y_test[ok]; score <- score[ok]
c(sites = length(y_test), presences = sum(y_test), prevalence = round(mean(y_test), 3))

## ---- auc ----
n1 <- sum(y_test == 1); n0 <- sum(y_test == 0)
# rank-based AUC (the Mann-Whitney form)
auc_rank <- (sum(rank(score)[y_test == 1]) - n1 * (n1 + 1) / 2) / (n1 * n0)

# the ROC curve, then AUC by trapezoidal integration
thr <- sort(unique(c(0, score, 1)))
roc <- as.data.frame(t(sapply(thr, function(t) {
  pos <- score >= t
  c(threshold = t, tpr = sum(pos & y_test == 1) / n1,
    fpr = sum(pos & y_test == 0) / n0, spec = 1 - sum(pos & y_test == 0) / n0)
})))
roc <- roc[order(roc$fpr, roc$tpr), ]
auc_trap <- sum(diff(roc$fpr) * (head(roc$tpr, -1) + tail(roc$tpr, -1)) / 2)
c(auc_rank = round(auc_rank, 3), auc_trapezoid = round(auc_trap, 3))

## ---- fig-roc ----
roc$tss <- roc$tpr + roc$spec - 1
best <- roc[which.max(roc$tss), ]
ggplot(roc, aes(fpr, tpr)) +
  geom_abline(slope = 1, intercept = 0, colour = pal_faint, linetype = "dashed") +
  geom_path(colour = pal_forest, linewidth = 1) +
  geom_point(data = best, aes(fpr, tpr), colour = pal_red, size = 3) +
  annotate("text", x = 0.55, y = 0.30, hjust = 0,
           label = paste0("AUC = ", sprintf("%.3f", auc_rank)), colour = pal_ink, size = 4.2) +
  annotate("text", x = best$fpr + 0.03, y = best$tpr - 0.03, hjust = 0,
           label = "max-TSS operating point", colour = pal_red, size = 3.2) +
  coord_equal() +
  labs(title = "ROC curve for the held-out test set",
       x = "False positive rate (1 - specificity)", y = "True positive rate (sensitivity)") +
  theme_te(12) + theme(legend.position = "none")

## ---- fig-threshold ----
thr_star <- best$threshold
ss <- roc %>% select(threshold, tpr, spec) %>%
  pivot_longer(c(tpr, spec), names_to = "metric", values_to = "value") %>%
  mutate(metric = recode(metric, tpr = "Sensitivity", spec = "Specificity"))
ggplot(ss, aes(threshold, value, colour = metric)) +
  geom_line(linewidth = 1) +
  geom_vline(xintercept = thr_star, colour = pal_red, linetype = "dashed") +
  annotate("text", x = thr_star + 0.01, y = 0.15, hjust = 0, label = "max-TSS threshold",
           colour = pal_red, size = 3.2) +
  scale_colour_manual(values = c("Sensitivity" = pal_forest, "Specificity" = pal_gold), name = NULL) +
  labs(title = "The threshold trade-off",
       subtitle = "Sensitivity and specificity move in opposite directions as the cut-off rises",
       x = "Suitability threshold", y = "Rate") +
  theme_te(12)

## ---- confusion ----
pred_pa <- as.integer(score >= thr_star)
cm <- table(predicted = factor(pred_pa, c(1, 0)), observed = factor(y_test, c(1, 0)))
cm

## ---- metrics ----
TP <- cm["1","1"]; FN <- cm["0","1"]; FP <- cm["1","0"]; TN <- cm["0","0"]
sens <- TP / (TP + FN); spec <- TN / (TN + FP)
n <- sum(cm); po <- (TP + TN) / n
pe <- ((TP + FP) * (TP + FN) + (FN + TN) * (FP + TN)) / n^2
kappa <- (po - pe) / (1 - pe)
c(threshold = round(thr_star, 3), sensitivity = round(sens, 3), specificity = round(spec, 3),
  TSS = round(sens + spec - 1, 3), kappa = round(kappa, 3))

## ---- naive-half ----
pred_half <- as.integer(score >= 0.5)
sens_h <- sum(pred_half == 1 & y_test == 1) / n1
spec_h <- sum(pred_half == 0 & y_test == 0) / n0
c(sensitivity = round(sens_h, 3), specificity = round(spec_h, 3),
  TSS = round(sens_h + spec_h - 1, 3))
