← ClaudeAtlas

causal-mediationlisted

Causal mediation analysis in R, including direct and indirect effects, assumptions, and sensitivity analysis.
choxos/BiostatAgent · ★ 4 · AI & Automation · score 77
Install: claude install-skill choxos/BiostatAgent
# Causal Mediation Analysis in R ## Overview Causal mediation analysis methods for decomposing total effects into direct and indirect effects. Covers traditional approaches, natural effect models, sensitivity analysis for unmeasured confounding, mediation with survival outcomes, and comprehensive causal mediation frameworks. ## Traditional Mediation (Baron-Kenny) ### Basic Approach ```r # Baron-Kenny steps for mediation # Total effect: Y = c*X + e # Mediator: M = a*X + e # Outcome with mediator: Y = c'*X + b*M + e # Indirect effect: a*b # Direct effect: c' # Step 1: Total effect (X -> Y) fit_total <- lm(outcome ~ treatment + covariates, data = df) c_total <- coef(fit_total)["treatment"] # Step 2: Effect on mediator (X -> M) fit_med <- lm(mediator ~ treatment + covariates, data = df) a <- coef(fit_med)["treatment"] # Step 3: Direct effect (X -> Y | M) fit_direct <- lm(outcome ~ treatment + mediator + covariates, data = df) c_prime <- coef(fit_direct)["treatment"] b <- coef(fit_direct)["mediator"] # Effects indirect_effect <- a * b direct_effect <- c_prime total_effect <- c_total proportion_mediated <- indirect_effect / total_effect cat("Total effect:", round(total_effect, 4), "\n") cat("Direct effect:", round(direct_effect, 4), "\n") cat("Indirect effect:", round(indirect_effect, 4), "\n") cat("Proportion mediated:", round(proportion_mediated * 100, 1), "%\n") ``` ### Sobel Test (Not Recommended for Small Samples) ```r # Sobel test for indirect effect sobel_test <-