# Path analysis: direct, indirect, and total effects
# https://tidyecology.com/posts/path-analysis-direct-indirect-effects/
#
# 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"))
}

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

## ---- data ----
set.seed(155)
n <- 150
nitrogen <- rnorm(n)
a_true <- 0.60
biomass <- a_true * nitrogen + sqrt(1 - a_true^2) * rnorm(n)
cprime_true <- 0.15
b_true <- 0.55
herbivory <- cprime_true * nitrogen + b_true * biomass + rnorm(n)

## standardise so that lm coefficients are standardised path coefficients
z <- function(x) (x - mean(x)) / sd(x)
nitrogen  <- z(nitrogen)
biomass   <- z(biomass)
herbivory <- z(herbivory)

## ---- fit ----
m_bio <- lm(biomass ~ nitrogen)                # path a
m_her <- lm(herbivory ~ nitrogen + biomass)    # paths c' and b

a      <- unname(coef(m_bio)["nitrogen"])
cprime <- unname(coef(m_her)["nitrogen"])
b      <- unname(coef(m_her)["biomass"])

se_a  <- summary(m_bio)$coef["nitrogen", "Std. Error"]
se_cp <- summary(m_her)$coef["nitrogen", "Std. Error"]
se_b  <- summary(m_her)$coef["biomass",  "Std. Error"]
p_cp  <- summary(m_her)$coef["nitrogen", "Pr(>|t|)"]

## ---- decomp ----
indirect <- a * b
total    <- cprime + indirect
prop_med <- indirect / total

## the exact identity: total equals the simple regression slope
m_naive   <- lm(herbivory ~ nitrogen)
total_reg <- unname(coef(m_naive)["nitrogen"])
r_xy      <- cor(nitrogen, herbivory)
gap       <- abs(total_reg - total)

## how firm is the share? resample plots and refit both regressions
pm_boot <- replicate(2000, {
  i  <- sample(n, n, replace = TRUE)
  ai <- coef(lm(biomass[i] ~ nitrogen[i]))[[2]]
  ci <- coef(lm(herbivory[i] ~ nitrogen[i] + biomass[i]))
  ai * ci[[3]] / (ai * ci[[3]] + ci[[2]])
})
pm_ci <- quantile(pm_boot, c(0.025, 0.975))

## ---- fig-path ----
nodes <- data.frame(x = c(0,1,2), y = c(0,1,0),
                    lab = c("Soil\nnitrogen","Plant\nbiomass","Herbivory"))
edges <- data.frame(
  x1 = c(0.22, 1.20, 0.24), y1 = c(0.16, 0.84, 0),
  x2 = c(0.80, 1.78, 1.76), y2 = c(0.84, 0.16, 0),
  ltype = c("solid","solid","22"),
  col   = c(te$forest, te$forest, te$muted))
labs <- data.frame(
  x = c(0.44, 1.55, 1.0), y = c(0.62, 0.62, -0.16),
  lab = c(sprintf("a = %.2f", a), sprintf("b = %.2f", b),
          sprintf("c' = %.2f%s", cprime,
                  if (p_cp >= 0.05) " (ns)" else "")),
  col = c(te$forest, te$forest, te$muted))

ggplot() +
  geom_segment(data = edges, aes(x = x1, y = y1, xend = x2, yend = y2),
               linetype = edges$ltype, colour = edges$col, linewidth = 0.9,
               arrow = arrow(length = unit(0.16, "cm"), type = "closed")) +
  geom_label(data = nodes, aes(x, y, label = lab), fill = te$card, colour = te$ink,
             linewidth = 0.4, label.r = unit(0.12, "cm"), size = 3.6,
             lineheight = 0.9, fontface = "bold") +
  geom_text(data = labs, aes(x, y, label = lab), colour = labs$col, size = 3.2) +
  annotate("text", x = 1, y = 1.32,
           label = sprintf("naive bivariate r(nitrogen, herbivory) = %.2f", r_xy),
           colour = te$warm, size = 3.3) +
  coord_cartesian(xlim = c(-0.35, 2.35), ylim = c(-0.45, 1.5), clip = "off") +
  labs(title = "Two structural equations, three fitted paths",
       subtitle = "Standardised coefficients from lm(biomass ~ nitrogen) and lm(herbivory ~ nitrogen + biomass)") +
  theme_te(12) +
  theme(axis.text = element_blank(), axis.title = element_blank(),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank())

## ---- supp ----
set.seed(2155)
n2 <- 150
nit2 <- rnorm(n2)
a2t <- 0.70; cp2t <- -0.20; b2t <- 0.70
bio2 <- a2t * nit2 + sqrt(1 - a2t^2) * rnorm(n2)
her2 <- cp2t * nit2 + b2t * bio2 + rnorm(n2)
nit2 <- z(nit2); bio2 <- z(bio2); her2 <- z(her2)

a2  <- unname(coef(lm(bio2 ~ nit2))["nit2"])
mh2 <- lm(her2 ~ nit2 + bio2)
cp2 <- unname(coef(mh2)["nit2"]); b2 <- unname(coef(mh2)["bio2"])
ind2 <- a2 * b2; tot2 <- cp2 + ind2
r_xy2 <- cor(nit2, her2)

## ---- fig-decomp ----
mk <- function(direct, indirect, total, scn)
  data.frame(component = factor(c("direct (c')","indirect (a*b)","total"),
                                levels = c("direct (c')","indirect (a*b)","total")),
             value = c(direct, indirect, total), scenario = scn)
dd <- rbind(mk(cprime, indirect, total, "Main model: effect is mostly indirect"),
            mk(cp2,    ind2,     tot2,  "Suppression: direct effect flips sign"))
dd$fill <- ifelse(dd$component == "total", "total",
                  ifelse(dd$value < 0, "neg", "pos"))

ggplot(dd, aes(component, value, fill = fill)) +
  geom_hline(yintercept = 0, colour = te$muted, linewidth = 0.4) +
  geom_col(width = 0.62) +
  geom_text(aes(label = sprintf("%.2f", value),
                vjust = ifelse(value >= 0, -0.4, 1.3)),
            size = 3.1, colour = te$body) +
  facet_wrap(~scenario) +
  scale_fill_manual(values = c(pos = te$forest, neg = te$warm, total = te$sage),
                    guide = "none") +
  coord_cartesian(ylim = c(-0.32, 0.55)) +
  labs(title = "Total effect = direct + indirect",
       subtitle = "An exact partition of the OLS slope; it can hide a strong or oppositely signed direct path",
       x = NULL, y = "standardised effect of nitrogen on herbivory") +
  theme_te(12)
