# Piecewise SEM and the test of d-separation
# https://tidyecology.com/posts/piecewise-sem-d-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-11-04

## ---- setup ----
library(ggplot2)

te <- list(paper="#f5f4ee", card="#ffffff", ink="#16241d", body="#2c3a31",
           forest="#275139", label="#46604a", sage="#93a87f", line="#dad9ca",
           muted="#5d6b61", warm="#b5534e", gold="#cda23f")

theme_te <- function(base_size = 12) {
  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$muted, size = base_size * 0.85),
          axis.title = element_text(colour = te$body),
          plot.title = element_text(colour = te$ink, face = "bold", size = base_size * 1.1),
          plot.subtitle = element_text(colour = te$muted, size = base_size * 0.9),
          strip.text = element_text(colour = te$ink, face = "bold"),
          legend.text = element_text(colour = te$body))
}

knitr::opts_chunk$set(fig.align = "center", dev = "png", dpi = 150)

## ---- 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)), "}$")
}

## ---- data ----
set.seed(156)
n <- 200
elevation <- rnorm(n)
temp <- -0.70 * elevation + sqrt(1 - 0.7^2) * rnorm(n)   # colder at height
veg  <-  0.60 * temp      + sqrt(1 - 0.6^2) * rnorm(n)   # warmer, more cover
eta  <- 0.30 + 0.55 * veg + 0.45 * temp                 # insects depend on both
insects <- rpois(n, exp(eta))
dat <- data.frame(elevation, temp, veg, insects)

## ---- paths ----
m_temp <- lm(temp ~ elevation)
m_veg  <- lm(veg ~ temp)
m_ins  <- glm(insects ~ veg + temp, family = poisson)

b_et <- unname(coef(m_temp)["elevation"])
b_tv <- unname(coef(m_veg)["temp"])
b_vi <- unname(coef(m_ins)["veg"])
b_ti <- unname(coef(m_ins)["temp"])
disp <- sum(residuals(m_ins, "pearson")^2) / m_ins$df.residual

## ---- helpers ----
claim_p <- function(resp, pred, cond, family = c("gaussian","poisson")) {
  family <- match.arg(family)
  f <- as.formula(paste(resp, "~", paste(c(pred, cond), collapse = " + ")))
  m <- if (family == "gaussian") lm(f, data = dat) else glm(f, family = poisson, data = dat)
  co <- summary(m)$coefficients
  co[pred, ncol(co)]           # last column: Pr(>|t|) or Pr(>|z|)
}
fishersC <- function(ps) {
  C <- -2 * sum(log(ps)); df <- 2 * length(ps)
  c(C = C, df = df, p = pchisq(C, df, lower.tail = FALSE))
}

## ---- testA ----
pA1 <- claim_p("veg", "elevation", "temp", "gaussian")               # elev _||_ veg | temp
pA2 <- claim_p("insects", "elevation", c("veg","temp"), "poisson")   # elev _||_ ins | veg, temp
CA  <- fishersC(c(pA1, pA2))

## ---- fig-dags ----
np <- data.frame(x = c(0,1,2,3), y = c(1,1,1.75,1),
                 lab = c("elevation","temperature","vegetation\ncover","insect\nabundance"))
mkedge <- function(x1,y1,x2,y2,present) data.frame(x1,y1,x2,y2,present)
base_edges <- function(cand) {
  e <- rbind(mkedge(0.30,1,0.70,1,TRUE),
             mkedge(1.18,1.12,1.82,1.63,TRUE),
             mkedge(2.18,1.63,2.82,1.12,TRUE),
             mkedge(1.20,0.92,2.80,0.92, cand == "A"))
  e$candidate <- if (cand == "A") "Candidate A: true DAG (C n.s., passes)" else
                                  "Candidate B: omits temp to insects (rejected)"
  e
}
edges <- rbind(base_edges("A"), base_edges("B"))
nodes <- rbind(cbind(np, candidate = "Candidate A: true DAG (C n.s., passes)"),
               cbind(np, candidate = "Candidate B: omits temp to insects (rejected)"))

ggplot() +
  geom_segment(data = subset(edges, present), aes(x1,y1,xend=x2,yend=y2),
               colour = te$forest, linewidth = 0.9,
               arrow = arrow(length = unit(0.15,"cm"), type = "closed")) +
  geom_segment(data = subset(edges, !present), aes(x1,y1,xend=x2,yend=y2),
               colour = te$warm, linewidth = 0.7, linetype = "22",
               arrow = arrow(length = unit(0.13,"cm"), type = "closed")) +
  geom_label(data = nodes, aes(x,y,label=lab), fill = te$card, colour = te$ink,
             linewidth = 0.35, label.r = unit(0.1,"cm"), size = 3.0,
             lineheight = 0.85, fontface = "bold") +
  facet_wrap(~candidate, ncol = 1) +
  coord_cartesian(xlim = c(-0.3,3.3), ylim = c(0.55,2.05), clip = "off") +
  labs(title = "Two candidate causal structures for the same data",
       subtitle = "Green arrows are fitted paths; the red dashed arrow in B marks the omitted true effect") +
  theme_te(12) +
  theme(axis.text = element_blank(), axis.title = element_blank(), panel.grid = element_blank())

## ---- testB ----
pB1 <- claim_p("veg", "elevation", "temp", "gaussian")                 # holds
pB2 <- claim_p("insects", "elevation", "veg", "poisson")               # violated
pB3 <- claim_p("insects", "temp", c("veg","elevation"), "poisson")     # violated
CB  <- fishersC(c(pB1, pB2, pB3))

## ---- fig-claims ----
sci_pm <- function(x) {   # plotmath text for a x 10^b, one significant digit
  s <- formatC(x, format = "e", digits = 0)
  sprintf("%s %%*%% 10^%d", sub("e.*", "", s), as.integer(sub(".*e", "", s)))
}
cl <- rbind(
  data.frame(candidate = sprintf('"Candidate A (C = %.2f, df 4, p = %.2f)"', CA["C"], CA["p"]),
             claim = c("elev _||_ veg | temp","elev _||_ ins | veg,temp"), p = c(pA1,pA2)),
  data.frame(candidate = sprintf('bold(paste("Candidate B (C = %.1f, df 6, p = ", %s, ")"))',
                                 CB["C"], sci_pm(CB["p"])),
             claim = c("elev _||_ veg | temp","elev _||_ ins | veg","temp _||_ ins | veg,elev"),
             p = c(pB1,pB2,pB3)))
cl$nlp <- -log10(cl$p); cl$violated <- cl$p < 0.05

ggplot(cl, aes(reorder(claim, nlp), nlp, fill = violated)) +
  geom_hline(yintercept = -log10(0.05), colour = te$warm, linetype = "22", linewidth = 0.5) +
  geom_col(width = 0.6) +
  geom_text(aes(label = ifelse(p < 0.001, sprintf('paste("p=", %s)', sci_pm(p)),
                        ifelse(p < 0.01, sprintf('"p=%.3f"',p), sprintf('"p=%.2f"',p)))),
            parse = TRUE, hjust = -0.08, size = 2.9, colour = te$body) +
  coord_flip(clip = "off") +
  facet_wrap(~candidate, ncol = 1, scales = "free_y", labeller = label_parsed) +
  scale_fill_manual(values = c(`FALSE` = te$sage, `TRUE` = te$warm),
                    labels = c("holds","violated (p < 0.05)"), name = NULL) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.28))) +
  labs(title = "Every claim in the basis set, tested",
       subtitle = "Dashed line at p = 0.05; both violated claims in B involve the omitted edge",
       x = NULL, y = "-log10(p) of the tested absent path") +
  theme_te(12) + theme(legend.position = "top")

## ---- aic ----
KA <- 4; KB <- 3            # B estimates one fewer path
AICA <- unname(CA["C"] + 2 * KA)
AICB <- unname(CB["C"] + 2 * KB)
dAIC <- AICB - AICA
