# Checking a structural equation model
# https://tidyecology.com/posts/checking-a-structural-equation-model/
#
# 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-01

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

claim_p <- function(resp, pred, cond, data) {
  f <- as.formula(paste(resp, "~", paste(c(pred, cond), collapse = " + ")))
  summary(lm(f, data = data))$coefficients[pred, "Pr(>|t|)"]
}
fishersC <- function(ps) {
  C <- -2 * sum(log(ps)); df <- 2 * length(ps)
  c(C = C, df = df, p = pchisq(C, df, lower.tail = FALSE))
}

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

## ---- data1 ----
set.seed(158)
n <- 40
elevation <- rnorm(n)
temp <- -0.70 * elevation + sqrt(1 - 0.7^2) * rnorm(n)
veg  <-  0.60 * temp      + sqrt(1 - 0.6^2) * rnorm(n)
insects <- 0.55 * veg + 0.45 * temp + rnorm(n)
d0 <- data.frame(elevation, temp, veg, insects)

## correct graph: elev -> temp -> veg -> insects, temp -> insects; basis set has 2 claims
Cc <- fishersC(c(claim_p("veg", "elevation", "temp", d0),
                 claim_p("insects", "elevation", c("veg","temp"), d0)))
## misspecified graph: drop temp -> insects; basis set grows to 3 claims
Cm <- fishersC(c(claim_p("veg", "elevation", "temp", d0),
                 claim_p("insects", "elevation", "veg", d0),
                 claim_p("insects", "temp", c("veg","elevation"), d0)))

## ---- bootstrap1 ----
f_temp <- lm(temp ~ elevation, d0); f_veg <- lm(veg ~ temp, d0)
f_ins  <- lm(insects ~ veg + temp, d0)
s_temp <- summary(f_temp)$sigma; s_veg <- summary(f_veg)$sigma; s_ins <- summary(f_ins)$sigma

simC <- function() {
  el <- rnorm(n, 0, sd(elevation))
  tp <- coef(f_temp)[1] + coef(f_temp)[2]*el + rnorm(n, 0, s_temp)
  vg <- coef(f_veg)[1]  + coef(f_veg)[2]*tp  + rnorm(n, 0, s_veg)
  is <- coef(f_ins)[1]  + coef(f_ins)[2]*vg  + coef(f_ins)[3]*tp + rnorm(n, 0, s_ins)
  dd <- data.frame(elevation = el, temp = tp, veg = vg, insects = is)
  unname(fishersC(c(claim_p("veg","elevation","temp", dd),
                    claim_p("insects","elevation",c("veg","temp"), dd)))["C"])
}
set.seed(9158)
B <- 3000
boot_C <- replicate(B, simC())
p_boot_correct <- mean(boot_C >= Cc["C"])
q95_boot <- unname(quantile(boot_C, 0.95)); q95_chi <- qchisq(0.95, Cc["df"])

## ---- fig-boot ----
xs  <- seq(0, max(quantile(boot_C, 0.999), Cc["C"]) + 1, length.out = 400)
chi <- data.frame(x = xs, dens = dchisq(xs, Cc["df"]))
ggplot() +
  geom_histogram(data = data.frame(boot_C), aes(boot_C, after_stat(density)),
                 bins = 45, fill = te$sage, colour = NA, alpha = 0.85) +
  geom_line(data = chi, aes(x, dens), colour = te$muted, linewidth = 0.9, linetype = "22") +
  geom_vline(xintercept = Cc["C"], colour = te$forest, linewidth = 1.0) +
  annotate("text", x = Cc["C"], y = max(chi$dens) * 0.92,
           label = sprintf("correct model\nC = %.2f\nbootstrap p = %.2f", Cc["C"], p_boot_correct),
           colour = te$forest, size = 3.0, hjust = -0.08, lineheight = 0.9) +
  labs(title = "Checking the yardstick: bootstrap the null of Fisher's C",
       subtitle = sprintf("The bootstrap null (pale green) tracks chi-squared(%d) (dashed): 95th percentile %.2f vs %.2f",
                          Cc["df"], q95_boot, q95_chi),
       x = "Fisher's C under the fitted correct model", y = "density") +
  theme_te(12)

## ---- data2 ----
set.seed(2158)
n2 <- 120
X <- rnorm(n2)
M <- 0.60 * X + sqrt(1 - 0.6^2) * rnorm(n2)
Y <- 0.60 * M + sqrt(1 - 0.6^2) * rnorm(n2)
d2 <- data.frame(X, M, Y)

## ---- equiv ----
p_chain_fwd <- claim_p("Y", "X", "M", d2)     # X _||_ Y | M, framed forward
p_chain_bwd <- claim_p("X", "Y", "M", d2)     # X _||_ Y | M, framed backward
C_equiv <- fishersC(p_chain_fwd)

## the collider X -> M <- Y is a different class: it implies X _||_ Y marginally
p_marginal <- summary(lm(Y ~ X, d2))$coefficients["X", "Pr(>|t|)"]
C_collider <- fishersC(p_marginal)

## ---- fig-equiv ----
nx <- data.frame(node = c("X","M","Y"), x = c(0,1,2), y = c(0,1,0))
edef <- list(
  "X -> M -> Y  (chain)"          = list(c("X","M"), c("M","Y")),
  "Y -> M -> X  (chain reversed)" = list(c("Y","M"), c("M","X")),
  "X <- M -> Y  (common cause)"   = list(c("M","X"), c("M","Y")),
  "X -> M <- Y  (collider)"       = list(c("X","M"), c("Y","M")))
plab <- c("X -> M -> Y  (chain)"          = sprintf("C = %.2f, p = %.2f  (passes)", C_equiv["C"], C_equiv["p"]),
          "Y -> M -> X  (chain reversed)" = sprintf("C = %.2f, p = %.2f  (passes)", C_equiv["C"], C_equiv["p"]),
          "X <- M -> Y  (common cause)"   = sprintf("C = %.2f, p = %.2f  (passes)", C_equiv["C"], C_equiv["p"]),
          "X -> M <- Y  (collider)"       = sprintf("C = %.1f, p < 0.001  (rejected)", C_collider["C"]))
mk_edges <- function(pairs, panel) do.call(rbind, lapply(pairs, function(pr) {
  a <- nx[nx$node == pr[1], ]; b <- nx[nx$node == pr[2], ]
  dx <- b$x - a$x; dy <- b$y - a$y; L <- sqrt(dx^2 + dy^2)
  data.frame(x = a$x + 0.22*dx/L, y = a$y + 0.22*dy/L,
             xend = b$x - 0.22*dx/L, yend = b$y - 0.22*dy/L, panel = panel)
}))
edges <- do.call(rbind, Map(mk_edges, edef, names(edef)))
nodes <- do.call(rbind, lapply(names(edef), function(p) cbind(nx, panel = p)))
labs  <- data.frame(panel = names(plab), lab = unname(plab),
                    col = ifelse(grepl("rejected", plab), te$warm, te$forest))
for (df in c("edges","nodes","labs"))
  assign(df, within(get(df), panel <- factor(panel, levels = names(edef))))

ggplot() +
  geom_segment(data = edges, aes(x, y, xend = xend, yend = yend), colour = te$forest,
               linewidth = 0.9, arrow = arrow(length = unit(0.16, "cm"), type = "closed")) +
  geom_label(data = nodes, aes(x, y, label = node), fill = te$card, colour = te$ink,
             linewidth = 0.4, label.r = unit(0.14, "cm"), size = 4.2, fontface = "bold") +
  geom_text(data = labs, aes(x = 1, y = -0.55, label = lab), colour = labs$col, size = 3.1) +
  facet_wrap(~panel, ncol = 2) +
  coord_cartesian(xlim = c(-0.35, 2.35), ylim = c(-0.8, 1.35), clip = "off") +
  labs(title = "The test reads the equivalence class, not the arrows",
       subtitle = "Data from X to M to Y: the three equivalent graphs fit identically; only the collider is a different class") +
  theme_te(12) +
  theme(axis.text = element_blank(), axis.title = element_blank(), panel.grid = element_blank())
