# Bayesian logistic regression under separation
# https://tidyecology.com/posts/bayesian-logistic-regression-separation/
#
# 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-15

## ---- setup ----
knitr::opts_chunk$set(message = FALSE, warning = FALSE,
                      fig.width = 7.5, fig.height = 5.4, dpi = 150)
library(ggplot2)
te <- list(ink="#16241d", body="#2c3a31", forest="#275139", label="#46604a",
           sage="#93a87f", paper="#f5f4ee", line="#dad9ca", faint="#5d6b61",
           gold="#cda23f", mown="#2f8f63", terra="#b5534e")
theme_te <- function(base_size = 13) {
  theme_minimal(base_size = base_size) +
    theme(plot.background = element_rect(fill = te$paper, colour = NA),
          panel.background = element_rect(fill = te$paper, colour = NA),
          panel.grid.major = element_line(colour = te$line, linewidth = 0.3),
          panel.grid.minor = element_blank(),
          axis.text = element_text(colour = te$faint),
          axis.title = element_text(colour = te$body),
          plot.title = element_text(colour = te$ink, face = "bold"),
          plot.subtitle = element_text(colour = te$label),
          legend.text = element_text(colour = te$body),
          plot.caption = element_text(colour = te$faint, size = base_size * 0.75))
}

## ---- data ----
set.seed(4)
n <- 40
x <- sort(runif(n, -2, 2))          # a standardised gradient, e.g. temperature
x_split <- mean(x[15:16])
y <- as.integer(x > x_split)        # presence above the threshold, absence below
c(presences = sum(y), absences = sum(1 - y))
paste(y, collapse = "")

## ---- glm ----
g  <- suppressWarnings(glm(y ~ x, family = binomial))
cf <- summary(g)$coefficients
round(cf, 2)

## ---- bayes ----
xs <- (x - mean(x)) / (2 * sd(x))                 # scale to SD 0.5 (Gelman 2008)
logpost <- function(b) {
  eta <- b[1] + b[2] * xs
  sum(dbinom(y, 1, plogis(eta), log = TRUE)) +
    dcauchy(b[1], 0, 10, log = TRUE) + dcauchy(b[2], 0, 2.5, log = TRUE)
}
metropolis <- function(iter = 200000, warm = 20000, prop = diag(c(12, 25)), seed = 1) {
  set.seed(seed)
  b <- c(0, 0); lp <- logpost(b); out <- matrix(NA_real_, iter, 2); acc <- 0
  for (t in seq_len(iter)) {
    cand <- b + as.numeric(prop %*% rnorm(2))
    lpp  <- logpost(cand)
    if (log(runif(1)) < lpp - lp) { b <- cand; lp <- lpp; acc <- acc + 1 }
    out[t, ] <- b
  }
  list(draws = out[(warm + 1):iter, ], acc = acc / iter)
}
## under separation the intercept and slope are correlated at 0.993, so a pilot run
## learns the shape of the posterior and the real run proposes along it
pilot  <- metropolis(40000, 10000, diag(c(12, 25)), seed = 7)
prop_R <- t(chol(cov(pilot$draws))) * 2.38 / sqrt(2)
fit    <- metropolis(200000, 20000, prop_R, seed = 21)
draws  <- fit$draws
b1_org <- draws[, 2] / (2 * sd(x))                # slope back on the x scale

## ---- fig-curves ----
xg  <- seq(min(x), max(x), length.out = 120)
xgs <- (xg - mean(x)) / (2 * sd(x))
pmat <- plogis(outer(draws[, 1], rep(1, length(xg))) + outer(draws[, 2], xgs))
bfit <- data.frame(x = xg, med = apply(pmat, 2, median),
                   lo = apply(pmat, 2, quantile, .025), hi = apply(pmat, 2, quantile, .975))
gfit <- data.frame(x = xg, p = predict(g, data.frame(x = xg), type = "response"))
ggplot() +
  geom_ribbon(data = bfit, aes(x, ymin = lo, ymax = hi), fill = te$forest, alpha = 0.18) +
  geom_line(data = gfit, aes(x, p), colour = te$terra, linewidth = 1, linetype = "22") +
  geom_line(data = bfit, aes(x, med), colour = te$forest, linewidth = 1.1) +
  geom_point(data = data.frame(x = x, y = y), aes(x, y), colour = te$ink, alpha = 0.6, size = 1.8) +
  annotate("text", x = min(x), y = 0.9, hjust = 0, size = 3.5, colour = te$terra,
           label = "MLE: an overconfident step") +
  annotate("text", x = max(x), y = 0.12, hjust = 1, size = 3.5, colour = te$forest,
           label = "Bayesian fit with 95% band") +
  labs(x = "environmental gradient x", y = "probability of presence",
       title = "Under separation the MLE is a cliff; the prior gives a curve",
       subtitle = "presence/absence perfectly split by the gradient in a small sample") +
  theme_te()

## ---- gap ----
xq  <- c(x[15], x_split, x[16])
qs  <- (xq - mean(x)) / (2 * sd(x))
pm  <- plogis(outer(draws[, 1], rep(1, 3)) + outer(draws[, 2], qs))
gap <- round(apply(pm, 2, quantile, c(.025, .5, .975)), 2)
colnames(gap) <- c("last absence", "gap centre", "first presence"); gap

## ---- profile ----
loglik <- function(b) sum(dbinom(y, 1, plogis(b[1] + b[2] * xs), log = TRUE))
b1grid <- seq(0, 40, length.out = 160)
prof <- t(sapply(b1grid, function(b1) c(
  ll = -optimize(function(b0) -loglik(c(b0, b1)),  c(-30, 30))$objective,
  lp = -optimize(function(b0) -logpost(c(b0, b1)), c(-30, 30))$objective)))
ll_rising   <- which.max(prof[, "ll"]) == length(b1grid)
b1_hat_post <- b1grid[which.max(prof[, "lp"])]
c(likelihood_still_rising_at_edge = ll_rising, posterior_peak = round(b1_hat_post, 1))

## ---- fig-profile ----
dfp <- rbind(
  data.frame(b1 = b1grid, val = prof[, "ll"] - max(prof[, "ll"]), curve = "log-likelihood"),
  data.frame(b1 = b1grid, val = prof[, "lp"] - max(prof[, "lp"]), curve = "log-posterior"))
ggplot(dfp, aes(b1, val, colour = curve)) +
  geom_line(linewidth = 1.1) +
  geom_vline(xintercept = b1_hat_post, linetype = "dashed", colour = te$forest) +
  scale_colour_manual(values = c("log-likelihood" = te$terra, "log-posterior" = te$forest), name = NULL) +
  coord_cartesian(ylim = c(-8, 0.5)) +
  annotate("text", x = 39, y = -2.6, hjust = 1, size = 3.5, colour = te$terra,
           label = "keeps rising: no finite maximum") +
  annotate("text", x = b1_hat_post + 1, y = -6, hjust = 0, size = 3.5, colour = te$forest,
           label = "finite peak") +
  labs(x = "slope coefficient (standardised predictor)", y = "relative log density",
       title = "Why the prior fixes separation",
       subtitle = "the likelihood has no maximum; adding the Cauchy prior creates one") +
  theme_te() + theme(legend.position = "top")
