# Checking a compositional analysis
# https://tidyecology.com/posts/checking-a-compositional-analysis/
#
# 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-11-15

## ---- setup ----
library(ggplot2)
clr <- function(x) { lx <- log(x); lx - mean(lx) }
ilr_basis <- function(D) {
  V <- matrix(0, D, D - 1)
  for (i in seq_len(D - 1)) { a <- sqrt(1 / (i * (i + 1))); b <- sqrt(i / (i + 1))
    V[1:i, i] <- a; V[i + 1, i] <- -b }
  V
}
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)), legend.position = "top")
theme_set(theme_te())

## ---- sci-tex-helper ----
sci_tex <- function(x, digits = 2) {
  s <- formatC(x, format = "e", digits = digits)
  paste0("$", sub("e.*", "", s), " \\times 10^{", as.integer(sub(".*e", "", s)), "}$")
}

## ---- simulate ----
set.seed(7715)
D <- 6L; ng <- 70L; sdl <- rep(0.4, D)
mu <- c(3.2, 2.6, 2.4, 2.2, 2.0, 2.3)
sim <- function(m) sapply(1:D, function(j) rlnorm(ng, m[j], sdl[j]))
Xc <- sim(mu); mt <- mu; mt[1] <- mt[1] + log(3); Xt <- sim(mt)
Pc <- Xc / rowSums(Xc); Pt <- Xt / rowSums(Xt)

## ---- coherence ----
p2_full <- t.test(Pc[,2], Pt[,2])$p.value
subc <- Xc[,2:6] / rowSums(Xc[,2:6]); subt <- Xt[,2:6] / rowSums(Xt[,2:6])   # drop taxon 1
p2_sub <- t.test(subc[,1], subt[,1])$p.value                                  # taxon 2 is column 1 here
lr_full <- t.test(log(Xc[,2]/Xc[,3]), log(Xt[,2]/Xt[,3]))$p.value
lr_sub  <- t.test(log(subc[,1]/subc[,2]), log(subt[,1]/subt[,2]))$p.value
c(naive_full = p2_full, naive_drop_bloomer = p2_sub, coda_full = lr_full, coda_drop = lr_sub)

## ---- fig-coherence ----
pv <- c(p2_full, p2_sub, lr_full, lr_sub)
dco <- data.frame(method = rep(c("naive proportion", "CoDA log-ratio"), each = 2),
                  comp = factor(rep(c("full 6-part", "drop the bloomer"), 2),
                                levels = c("full 6-part", "drop the bloomer")),
                  nl = -log10(pv))
p_coherence <- ggplot(dco, aes(comp, nl, fill = method)) +
  geom_col(position = position_dodge(0.7), width = 0.6) +
  geom_hline(yintercept = -log10(0.05), linetype = "dashed", colour = te_faint) +
  scale_fill_manual(values = c("naive proportion" = te_red, "CoDA log-ratio" = te_forest), name = NULL) +
  labs(title = "Does the verdict depend on which parts are in the table?",
       subtitle = "The focal taxon is truly unchanged; the naive verdict flips, the log-ratio verdict does not",
       x = NULL, y = "test for focal taxon  (-log10 p)")
p_coherence

## ---- thumb ----
ggsave("thumbnail.png",
       p_coherence +
         labs(title = paste(strwrap("Does the verdict survive dropping a part?", 46),
                            collapse = "\n"),
              subtitle = "The focal taxon is truly unchanged") +
         theme(plot.title = element_text(colour = te_ink, face = "bold", size = 13),
               plot.subtitle = element_text(colour = te_faint, size = 9.5),
               plot.margin = margin(8, 14, 8, 8)),
       width = 7.5, height = 5.4, dpi = 100, bg = te_paper)

## ---- residuals ----
V <- ilr_basis(D)
Y <- rbind(Xc, Xt); ILR <- t(apply(Y, 1, clr)) %*% V
g <- factor(rep(c("c", "t"), each = ng))
res <- residuals(lm(ILR ~ g))
sh <- apply(res, 2, function(z) shapiro.test(z)$p.value)
c(min_shapiro_p = min(sh), coords = D - 1, all_pass = all(sh > 0.05))

## ---- refframe ----
logFC_rel <- mean(log(Pt[,2])) - mean(log(Pc[,2]))       # observed relative change, < 0
r_star    <- exp(-logFC_rel)                              # total change at which absolute change is zero
true_total <- exp(mean(log(rowSums(Xt))) - mean(log(rowSums(Xc))))   # geometric-mean ratio, the scale of r_star; known here, unobservable from composition
c(relative_logFC = logFC_rel, tipping_total = r_star, true_total = true_total)

## ---- fig-refframe ----
rs <- seq(1, 3.5, by = 0.05); dfr <- data.frame(r = rs, abs = logFC_rel + log(rs))
ggplot(dfr, aes(r, abs)) +
  annotate("rect", xmin = 1, xmax = r_star, ymin = -Inf, ymax = Inf, fill = te_red, alpha = 0.10) +
  annotate("rect", xmin = r_star, xmax = 3.5, ymin = -Inf, ymax = Inf, fill = te_forest, alpha = 0.10) +
  geom_hline(yintercept = 0, colour = te_faint) +
  geom_line(colour = te_ink, linewidth = 0.9) +
  geom_vline(xintercept = r_star, linetype = "dashed", colour = te_red) +
  geom_vline(xintercept = true_total, linetype = "dotted", colour = te_forest) +
  annotate("text", x = 1.05, y = max(dfr$abs) * 0.8, label = "taxon 2 absolutely down",
           hjust = 0, colour = te_red, size = 3.2) +
  annotate("text", x = 3.45, y = min(dfr$abs) * 0.8, label = "taxon 2 absolutely up",
           hjust = 1, colour = te_forest, size = 3.2) +
  labs(title = "The honest limit: the total is the missing information",
       subtitle = sprintf("Absolute change flips sign at r* = %.2f; the true hidden total change is %.2f", r_star, true_total),
       x = "assumed total-abundance fold-change (treatment / control)",
       y = "implied absolute log fold-change of taxon 2")
