# Pseudo-absence and background points for SDMs
# https://tidyecology.com/posts/pseudo-absence-selection-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-08

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

## ---- landscape ----
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"
env <- c(temp, 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)
pres_df <- data.frame(pa = 1, temp = temp_v[pres_cells], prec = prec_v[pres_cells])

## ---- how-many ----
opt_from_fit <- function(m, dat) {
  ts <- seq(min(dat$temp), max(dat$temp), length.out = 10000)
  ps <- seq(min(dat$prec), max(dat$prec), length.out = 10000)
  c(temp_opt = ts[which.max(predict(m, data.frame(temp = ts, prec = median(dat$prec)), type = "response"))],
    prec_opt = ps[which.max(predict(m, data.frame(temp = median(dat$temp), prec = ps), type = "response"))])
}
n_grid <- c(50, 100, 250, 500, 1000, 2500, 5000)
set.seed(303)
res1 <- do.call(rbind, lapply(n_grid, function(nb) {
  t(sapply(1:40, function(i) {
    bgc <- sample(ncell(r_template), nb)
    d <- rbind(pres_df, data.frame(pa = 0, temp = temp_v[bgc], prec = prec_v[bgc]))
    d <- d[complete.cases(d), ]
    m <- glm(pa ~ poly(temp, 2) + poly(prec, 2), data = d, family = binomial)
    c(n_bg = nb, opt_from_fit(m, d))
  }))
}))
res1 <- as.data.frame(res1)
res1 %>% group_by(n_bg) %>%
  summarise(temp_sd = round(sd(temp_opt), 3), temp_mean = round(mean(temp_opt), 2),
            prec_sd = round(sd(prec_opt), 1), .groups = "drop")

## ---- fig-how-many ----
res1_f <- res1 %>% mutate(n_bg = factor(n_bg, levels = n_grid))
ggplot(res1_f, aes(n_bg, temp_opt)) +
  geom_hline(yintercept = 15, colour = pal_red, linetype = "dashed") +
  geom_jitter(width = 0.12, height = 0, colour = pal_forest, alpha = 0.4, size = 1) +
  stat_summary(fun = mean, geom = "point", colour = pal_ink, size = 2.4) +
  annotate("text", x = 6.7, y = 15, label = "true optimum", colour = pal_red, hjust = 1, vjust = -0.4, size = 3.3) +
  labs(title = "Estimated temperature optimum by number of background points",
       subtitle = "40 background draws per level; black points are the mean of each",
       x = "Number of background points", y = "Estimated optimum (C)") +
  theme_te(12) + theme(legend.position = "none")

## ---- prevalence ----
fit_glm <- function(nb, seed, weighted = FALSE) {
  set.seed(seed); bgc <- sample(ncell(r_template), nb)
  d <- rbind(pres_df, data.frame(pa = 0, temp = temp_v[bgc], prec = prec_v[bgc]))
  d <- d[complete.cases(d), ]
  w <- if (weighted) ifelse(d$pa == 1, 1, sum(d$pa == 1) / sum(d$pa == 0)) else rep(1, nrow(d))
  glm(pa ~ poly(temp, 2) + poly(prec, 2), data = d, family = binomial, weights = w)
}
m_500 <- fit_glm(500, 11); m_5000 <- fit_glm(5000, 11)
axis_t <- seq(min(temp_v), max(temp_v), length.out = 200)
curve_t <- function(m) predict(m, data.frame(temp = axis_t, prec = median(pres_df$prec)), type = "response")
c(intercept_500 = round(coef(m_500)[[1]], 2), intercept_5000 = round(coef(m_5000)[[1]], 2),
  peak_500 = round(max(curve_t(m_500)), 2), peak_5000 = round(max(curve_t(m_5000)), 2))

## ---- rank-check ----
v500  <- values(terra::predict(env, m_500,  type = "response", na.rm = TRUE))[, 1]
v5000 <- values(terra::predict(env, m_5000, type = "response", na.rm = TRUE))[, 1]
round(cor(v500, v5000, method = "spearman", use = "complete.obs"), 3)

## ---- weighted ----
mw_500 <- fit_glm(500, 11, weighted = TRUE); mw_5000 <- fit_glm(5000, 11, weighted = TRUE)
c(weighted_peak_500 = round(max(curve_t(mw_500)), 2),
  weighted_peak_5000 = round(max(curve_t(mw_5000)), 2))

## ---- fig-relative ----
resp2 <- rbind(
  data.frame(temp = axis_t, y = curve_t(m_500),   n_bg = "500",  scheme = "Unweighted"),
  data.frame(temp = axis_t, y = curve_t(m_5000),  n_bg = "5000", scheme = "Unweighted"),
  data.frame(temp = axis_t, y = curve_t(mw_500),  n_bg = "500",  scheme = "Down-weighted background"),
  data.frame(temp = axis_t, y = curve_t(mw_5000), n_bg = "5000", scheme = "Down-weighted background"))
resp2$scheme <- factor(resp2$scheme, levels = c("Unweighted", "Down-weighted background"))
ggplot(resp2, aes(temp, y, colour = n_bg)) +
  geom_line(linewidth = 1) +
  facet_wrap(~scheme) +
  scale_colour_manual(values = c("500" = pal_gold, "5000" = pal_forest), name = "Background points") +
  labs(title = "Predicted values are relative, not probabilities",
       subtitle = "Same presences and same niche shape; only the vertical scale moves",
       x = "Temperature (C)", y = "Predicted value") +
  theme_te(12)

## ---- env-flat-host ----
temp_bins <- table(cut(temp_v, 8)); prec_bins <- table(cut(prec_v, 8))
fit_opt <- function(bg) {
  d <- rbind(pres_df, data.frame(pa = 0, bg))
  opt_from_fit(glm(pa ~ poly(temp, 2) + poly(prec, 2), data = d, family = binomial), d)
}
set.seed(2027)
env_host <- t(replicate(20, {
  geo <- sample(ncell(r_template), 5000)
  bg_box <- data.frame(temp = runif(5000, min(temp_v), max(temp_v)), prec = runif(5000, min(prec_v), max(prec_v)))
  c(fit_opt(data.frame(temp = temp_v[geo], prec = prec_v[geo])), fit_opt(bg_box))
}))
colnames(env_host) <- c("temp_geo", "prec_geo", "temp_flat", "prec_flat")
temp_bins; prec_bins
round(colMeans(env_host), 2)

## ---- env-flat-region ----
fit_bg <- function(pres, bg) glm(pa ~ poly(temp, 2) + poly(prec, 2), family = binomial,
                                 data = rbind(pres, data.frame(pa = 0, bg)))
auc_rank <- function(s1, s0) (sum(rank(c(s1, s0))[seq_along(s1)]) - length(s1) * (length(s1) + 1) / 2) /
  (length(s1) * length(s0))
region_run <- function(slope) {
  reg <- sample(ncell(r_template), 3000, prob = exp(slope * (temp_v - mean(temp_v))))
  cells <- function(cc) data.frame(temp = temp_v[cc], prec = prec_v[cc])
  pres <- data.frame(pa = 1, cells(sample(reg, 300, prob = suit_true[reg])))
  bg_geo <- cells(sample(reg, 3000, replace = TRUE))
  bg_flat <- data.frame(temp = runif(3000, min(temp_v[reg]), max(temp_v[reg])),
                        prec = runif(3000, min(prec_v[reg]), max(prec_v[reg])))
  m_geo <- fit_bg(pres, bg_geo); m_flat <- fit_bg(pres, bg_flat)
  grid_d <- rbind(pres, data.frame(pa = 0, bg_geo))
  test_p <- cells(sample(reg, 300, prob = suit_true[reg])); test_b <- cells(sample(reg, 3000, replace = TRUE))
  c(opt_geo = opt_from_fit(m_geo, grid_d)[[1]], opt_flat = opt_from_fit(m_flat, grid_d)[[1]],
    pres_mean = mean(pres$temp),
    auc_geo = auc_rank(predict(m_geo, test_p), predict(m_geo, test_b)),
    auc_flat = auc_rank(predict(m_flat, test_p), predict(m_flat, test_b)),
    rho_geo = cor(predict(m_geo, cells(reg)), suit_true[reg], method = "spearman"),
    rho_flat = cor(predict(m_flat, cells(reg)), suit_true[reg], method = "spearman"))
}
set.seed(2028)
env_reg <- lapply(c(scarce = -0.15, common = 0.3), function(s) t(replicate(100, region_run(s))))
env_mean <- sapply(env_reg, colMeans)
env_mcse <- sapply(env_reg, function(r) apply(r, 2, sd) / sqrt(nrow(r)))
stopifnot(abs(env_mean["opt_geo", ] - 15) < 0.2)
round(env_mean, 3)

## ---- env-flat-numbers ----
em <- function(row, col, fmt = "%.2f") sprintf(fmt, env_mean[row, col])
shift <- abs(env_mean["opt_flat", ] - env_mean["opt_geo", ])
