# Closure and spurious correlation
# https://tidyecology.com/posts/closure-and-spurious-correlation/
#
# 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 ----
library(ggplot2)
clr <- function(x) { lx <- log(x); lx - mean(lx) }   # centred log-ratio
C   <- function(x) x / sum(x)                          # closure to the simplex

te_ink <- "#16241d"; te_forest <- "#275139"; te_faint <- "#5d6b61"
te_paper <- "#f5f4ee"; te_line <- "#dad9ca"; te_red <- "#b5534e"
theme_te <- function(base = 12) theme_minimal(base_size = base) + 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 = "#2c3a31"), axis.title = element_text(colour = te_ink),
  plot.title = element_text(colour = te_ink, face = "bold"),
  plot.subtitle = element_text(colour = te_faint, size = rel(0.9)),
  strip.text = element_text(colour = te_ink, face = "bold"), legend.position = "top")
theme_set(theme_te())

## ---- simulate ----
set.seed(8241)
n <- 500L; D <- 5L
mu    <- c(3.0, 2.6, 2.2, 1.8, 2.4)
sdlog <- c(0.55, 0.60, 0.50, 0.70, 0.58)
X <- sapply(seq_len(D), function(j) rlnorm(n, mu[j], sdlog[j]))   # absolute abundances
colnames(X) <- paste0("t", 1:D)

Cabs <- cor(X)
off_abs <- Cabs[upper.tri(Cabs)]
c(mean_abs_cor = mean(abs(off_abs)), max_abs_cor = max(abs(off_abs)))

## ---- closure ----
P <- X / rowSums(X)          # proportions: each row sums to 1
Cclo <- cor(P)
off_clo <- Cclo[upper.tri(Cclo)]
c(all_negative = all(off_clo < 0), mean_closed_cor = mean(off_clo),
  cor_t1_t2 = Cclo[1, 2])

## ---- fig-closure ----
d_abs <- data.frame(x = scale(log(X[,1])), y = scale(log(X[,2])),
                    space = "Absolute abundance (independent)")
d_clo <- data.frame(x = P[,1], y = P[,2], space = "Closed to proportions")
fp <- rbind(d_abs, setNames(d_clo, names(d_abs)))
p_closure <- ggplot(fp, aes(x, y)) +
  geom_point(colour = te_forest, alpha = 0.35, size = 1.1) +
  geom_smooth(method = "lm", se = FALSE, colour = te_red, linewidth = 0.9, formula = y ~ x) +
  facet_wrap(~ space, scales = "free") +
  labs(title = "Closure manufactures correlation",
       subtitle = "Parts are independent in absolute terms; the row total forces a negative tilt",
       x = "taxon 1", y = "taxon 2")
p_closure

## ---- thumb ----
ggsave("thumbnail.png",
       p_closure + labs(title = NULL, subtitle = NULL) +
         theme(strip.text = element_text(size = 9),
               plot.margin = margin(8, 14, 8, 8)),
       width = 7.5, height = 5.4, dpi = 100, bg = te_paper)

## ---- identity ----
S <- cov(P)
max(abs(rowSums(S)))     # each row of the covariance matrix sums to zero

## ---- subcomposition ----
sub <- X[, 1:3] / rowSums(X[, 1:3])     # subcomposition of taxa 1, 2, 3
c(full = cor(P[,1], P[,2]), sub = cor(sub[,1], sub[,2]))

## ---- logratio-variance ----
c(absolute = var(log(X[,1] / X[,2])),
  full     = var(log(P[,1] / P[,2])),
  sub      = var(log(sub[,1] / sub[,2])))

## ---- fig-incoherence ----
incoh <- sapply(5:2, function(k) { s <- X[, 1:k] / rowSums(X[, 1:k]); cor(s[,1], s[,2]) })
di <- data.frame(parts = factor(paste0(5:2, "-part"), levels = paste0(5:2, "-part")),
                 cor = incoh)
ggplot(di, aes(parts, cor, group = 1)) +
  geom_line(colour = te_red, linewidth = 0.9) + geom_point(colour = te_red, size = 2.4) +
  labs(title = "Pearson correlation is subcompositionally incoherent",
       subtitle = "cor(t1, t2) drifts as parts are dropped; var(log t1/t2) stays fixed at 0.679",
       x = "subcomposition (parts retained)", y = "Pearson cor(t1, t2)")

## ---- monte-carlo ----
set.seed(101)
B <- 2000L
mc <- replicate(B, {
  Xb <- sapply(seq_len(D), function(j) rlnorm(n, mu[j], sdlog[j]))
  Pb <- Xb / rowSums(Xb)
  c(abs = mean(cor(Xb)[upper.tri(diag(D))]),
    clo = mean(cor(Pb)[upper.tri(diag(D))]))
})
round(c(absolute_mean = mean(mc["abs",]),
        closed_mean   = mean(mc["clo",]),
        closed_lo = quantile(mc["clo",], .025),
        closed_hi = quantile(mc["clo",], .975)), 3)
