# Spatial cross-validation for SDMs in R
# https://tidyecology.com/posts/spatial-cross-validation-sdm/
#
# 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-14

## ---- setup ----
library(terra)
library(mgcv)
library(dplyr)
library(tidyr)
library(ggplot2)

## ---- data ----
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"
fold_cols <- c("1"="#275139","2"="#c9b458","3"="#b5534e","4"="#5d6b61","5"="#7ca2b0")
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"]; tn <- rast(r_template); values(tn)<-rnorm(ncell(r_template),0,2); tn<-focal(tn,w=9,fun=mean,na.rm=TRUE)
temp <- rast(r_template); values(temp)<-temp_trend; temp<-temp+tn
prec_trend <- 500+5*xy[,"x"]; pn <- rast(r_template); values(pn)<-rnorm(ncell(r_template),0,60); pn<-focal(pn,w=9,fun=mean,na.rm=TRUE)
prec <- rast(r_template); values(prec)<-prec_trend; prec<-prec+pn
tv <- values(temp)[,1]; pv <- values(prec)[,1]
tvz <- (tv-mean(tv))/sd(tv); pvz <- (pv-mean(pv))/sd(pv)
# hidden spatially-autocorrelated driver, never measured by the analyst
set.seed(55); zr <- rast(r_template); values(zr)<-rnorm(ncell(r_template),0,1)
zr <- focal(zr, w=7, fun=mean, na.rm=TRUE); zv <- values(zr)[,1]; zv <- (zv-mean(zv,na.rm=TRUE))/sd(zv,na.rm=TRUE)
eta <- -0.5 + 0.9*(-(tvz^2)) + 0.9*(-(pvz^2)) + 4.0*zv
p_occ <- plogis(eta)
set.seed(9); site_cells <- sample(ncell(r_template), 1500)
keep <- !is.na(zv[site_cells]) & !is.na(tv[site_cells]) & !is.na(pv[site_cells]); site_cells <- site_cells[keep]
set.seed(9); y <- rbinom(length(site_cells), 1, p_occ[site_cells])
dat <- data.frame(pa = y, temp = tv[site_cells], prec = pv[site_cells], x = xy[site_cells,1], y = xy[site_cells,2])

## ---- data-summary ----
c(sites = nrow(dat), presences = sum(dat$pa), prevalence = round(mean(dat$pa), 3))

## ---- folds ----
k <- 5
set.seed(1); fold_rand <- sample(rep(1:k, length.out = nrow(dat)))

# spatial blocks: a grid of 20-unit cells, whole blocks assigned to folds
bs <- 20
block_id <- as.integer(factor(paste(floor(dat$x / bs), floor(dat$y / bs))))
set.seed(7); block_fold <- sample(rep(1:k, length.out = max(block_id)))
fold_sp <- block_fold[block_id]
c(blocks = max(block_id), presences_per_fold = paste(tapply(dat$pa, fold_sp, sum), collapse = ","))

## ---- fig-folds ----
lay <- rbind(data.frame(x=dat$x, y=dat$y, fold=factor(fold_rand), scheme="Random folds"),
             data.frame(x=dat$x, y=dat$y, fold=factor(fold_sp),   scheme="Spatial block folds"))
ggplot(lay, aes(x, y, colour = fold)) +
  geom_point(size = 0.9, alpha = 0.85) +
  facet_wrap(~scheme) +
  scale_colour_manual(values = fold_cols, name = "Fold") +
  guides(colour = guide_legend(override.aes = list(size = 2.5))) +
  coord_equal(expand = FALSE) +
  labs(title = "Two ways to split the same points into five folds",
       subtitle = "Random scatters folds everywhere; spatial blocking keeps each fold together",
       x = "Easting", y = "Northing") +
  theme_te(12)

## ---- cv ----
auc_fn <- function(sc, yy) {
  n1 <- sum(yy == 1); n0 <- sum(yy == 0)
  (sum(rank(sc)[yy == 1]) - n1 * (n1 + 1) / 2) / (n1 * n0)
}
cv_auc <- function(fold, spatial_term) {
  pr <- numeric(nrow(dat))
  form <- if (spatial_term) pa ~ s(temp) + s(prec) + s(x, y, k = 120) else pa ~ s(temp) + s(prec)
  for (f in 1:k) {
    tr <- fold != f; te <- fold == f
    m <- gam(form, data = dat[tr, ], family = binomial, method = "REML")
    pr[te] <- predict(m, dat[te, ], type = "response")
  }
  auc_fn(pr, dat$pa)
}
results <- data.frame(
  model  = rep(c("Environment only", "Environment + spatial smooth"), each = 2),
  scheme = rep(c("Random CV", "Spatial block CV"), 2),
  auc = round(c(cv_auc(fold_rand, FALSE), cv_auc(fold_sp, FALSE),
                cv_auc(fold_rand, TRUE),  cv_auc(fold_sp, TRUE)), 3))
results

## ---- resub ----
m_full <- gam(pa ~ s(temp) + s(prec) + s(x, y, k = 120), data = dat, family = binomial, method = "REML")
round(auc_fn(predict(m_full, dat, type = "response"), dat$pa), 3)

## ---- fig-auc ----
results$model <- factor(results$model, levels = c("Environment only", "Environment + spatial smooth"))
ggplot(results, aes(scheme, auc, fill = scheme)) +
  geom_col(width = 0.62) +
  geom_text(aes(label = sprintf("%.3f", auc)), vjust = -0.4, colour = pal_ink, size = 3.6) +
  facet_wrap(~model) +
  scale_fill_manual(values = c("Random CV" = pal_gold, "Spatial block CV" = pal_forest), guide = "none") +
  scale_y_continuous(limits = c(0, 1), expand = expansion(mult = c(0, 0.08))) +
  labs(title = "Random cross-validation flatters both models",
       subtitle = "Spatial blocking is harder for both; the flexible spatial model falls furthest",
       x = NULL, y = "Pooled cross-validated AUC") +
  theme_te(12)
