Skip to content

About

Cross-fitted causal mediation for Julia: interventional, natural, organic, controlled direct, and recanting-twin effects with moc and MTP EIF

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

CausalMediation.jl

DOI

This package is part of the CDCS Julia package set. An early draft of Causal Dynamics of Complex Systems uses the packages as a pedagogical implementation.

CausalMediation implements selected methods from the causal mediation literature directly in Julia, using the published definitions and estimating equations. See Related literature and software for the original contributions and the methods manual for the scope of the implementations.

Cross-fitted mediation for the CDCS Julia stack: interventional (RI), natural, organic, and controlled direct effects, with intermediate confounding (moc) and continuous MTP one-step / TMLE estimators. RecantingTwinMediation remains exported as a reserved compatibility type; estimation rejects it because this package does not currently implement a path-specific estimator. For cluster-randomised trials with post-treatment confounding, 0.3.0 also adds experimental ClusterMediationSpec routes: run_cluster_mediation (Theorem 2 Gaussian EIF reference) and the parametric hierarchical Bayesian route (fit_bayesian_cluster_mediation / bayesian_mediation_effects).

For eligible static-arm one-step fits, the default capped calculation preserves point estimates but reports unavailable inference with :truncated_weights. Request weight_truncation=false explicitly for the untruncated static-arm EIF, subject to its overlap, nuisance-model and moment assumptions. Small-sample nominal coverage remains unresolved; see Methods.

Documentation: simonab.github.io/CausalMediation.jl (after the Documentation workflow has run on main).

The registered 0.3.0 release (Pkg.add("CausalMediation")) requires Julia 1.12+, CausalDynamics 0.5+, and CausalTargeted 0.5+. Patch release 0.3.1 is prepared on main (pending CausalTargeted 0.5.3 on General). Registry tracking: REGISTRATION.md.

Install

using Pkg
Pkg.add("CausalMediation")

Source release 0.3.0 is registered in General. Its API changes are listed in CHANGELOG.md; install compatible CDCS package versions when following the current examples.

Development tip of main:

Pkg.add([
    PackageSpec(url="https://github.com/SimonAB/CausalDynamics.jl.git"),
    PackageSpec(url="https://github.com/SimonAB/CausalTargeted.jl.git"),
    PackageSpec(url="https://github.com/SimonAB/CausalMediation.jl.git"),
])

Quick start

using CausalMediation, CausalTargeted, StableRNGs

df, truth = simulate_continuous_mtp_mediation(200; rng = StableRNG(1))
spec = MediationSpec(:A, :Y; mediators = [:M], covariates = [:W])
res = run_mediation(spec, df; deltas = [1.0], folds = 2, n_mc = 16, parallel = false)
decompose(res)

Factor A (recode MTP, continuous M):

df, truth = simulate_categorical_a_mediation(280)
spec = MediationSpec(
    :A, :Y;
    mediators = [:M],
    covariates = [:W],
    policy_d0 = discrete_recode_policy(Dict{String, String}()),
    policy_d1 = discrete_recode_policy(truth.recode),
)

With intermediate confounders:

spec = MediationSpec(:A, :Y; mediators = [:M], covariates = [:W], moc = [:L])

Testing and validation

CI develops tip CausalDynamics and CausalTargeted so RepresentationSpec and nuisance APIs match the stack; Pkg.test() on Julia 1.13 is the merge gate. Julia 1.12 remains within the declared compatibility range. Full-stack stress with real cohorts lives in CausalTargeted (see links below).

Guardrail What we exercise Where
Unit / API Effect gates (interventional, natural, organic, CDE, reserved recanting-twin rejection), MediationSpec / run_mediation*, identify natural vs interventional, schema guards, categorical-$A$ policies test/runtests.jl
Synthetic recovery Binary and continuous MTP mediation; intermediate confounding (moc); TE / NDE / NIE vs simulation oracles test/runtests.jl (simulate_* DGPs)
Missing data Shared MAR $Y$ helper; scalar and bootstrap :drop/:ipcw checks; retired TMLE3 NDE helper rejects both options before fitting test/runtests.jl, test/test_tmle3_contract.jl
Representation bridge High-dim spectrum → codes → mediation grid on encoded panel test/test_representation_bridge.jl
Integration CausalDynamics identification certificates; CausalTargeted Super Learner / schema utilities test/runtests.jl
Stack stress (pre-ship) Mediation curves, missing-$M$ / missing-$Y$, freeze comparisons on conservation and CI benchmarks CausalTargeted stress_validation.qmd
Deep SCM hand-off Estimation on Lux / representation codes deep_scm_estimation_stress.qmd

If you have a mediation scenario that should be harder to pass (tighter effect bounds, path-specific or natural-ID edge cases, messier MAR), please open an issue — we welcome stress cases that expose gaps before users do.

Documentation (local)

julia --project=docs -e 'using Pkg; Pkg.develop(path="."); Pkg.instantiate()'
julia --project=docs docs/make.jl

Pages: home, choosing an approach, getting started, methods & estimators, clustered mediation, API reference, naming & legacy migrations, ecosystem comparison, references.

Stress validation (Quarto notebook + catalogued datasets): CausalTargeted STRESS.md · stress_validation.qmd · deep_scm_estimation_stress.qmd (codes → mediation/LMTP) · Documenter · harness in causal-dynamics-book/scripts/stress_harness.

Design notes in-repo: DESIGN.md · BOUNDARIES.md · NAMING.md · REGISTRATION.md · ecosystem principles · ECOSYSTEM_COMPARISON.md.

Acknowledgements

Developed in the CDCS Julia package set. Maintainer: Simon A. Babayan.

Mediation estimands and one-step / TMLE estimators follow Díaz, Hejazi, Rudolph, Williams, Liu, Vansteelandt, Daniel, and collaborators, with targeted-learning foundations from van der Laan, Rose, and co-authors. Closest R companions are crumble, medoutcon, medRCT, and lmtp. The experimental clustered canonical reference implements the parametric form of Theorem 2 in Ham, Cheng & Li (2026), paired with an experimental parametric hierarchical Bayesian route (fit_bayesian_cluster_mediation / bayesian_mediation_effects); see cluster-mediation.md for scope and limitations.

Related literature and software

This package provides Julia-native implementations of the following papers (full list in docs/src/references.md):

  • Vansteelandt & Daniel (2017). Interventional effects for mediation analysis with multiple mediators. Epidemiology. doi:10.1097/EDE.0000000000000596 — RI TE / NDE / NIE
  • Díaz & Hejazi (2020). Causal mediation analysis for stochastic interventions. JRSS-B. doi:10.1111/rssb.12362
  • Hejazi, Rudolph, van der Laan & Díaz (2023). Stochastic interventional (in)direct effects. Biostatistics. doi:10.1093/biostatistics/kxac002 — intermediate confounding (moc)
  • Liu, Williams, Rudolph & Díaz (2024). General targeted machine learning for modern causal mediation analysis. arXiv:2408.14620 — run_mediation / grids
  • Pearl (2001); Robins & Greenland (1992); VanderWeele (2015). Natural / classical mediation — NaturalMediation when admissible
  • Ham, Cheng & Li (2026). Semiparametric causal mediation analysis in cluster-randomized trials. arXiv:2611.11903 — experimental clustered reference

Related software: R crumble, medoutcon, medRCT, lmtp; Python Ananke; Julia CausalDynamics.jl, CausalTargeted.jl, DAGMakie.jl.

License

MIT © Simon A. Babayan

Citation

For the experimental clustered stochastic-mediation method, cite the original methodological paper as well as the software:

Ham, D., Cheng, C., & Li, F. (2026). Semiparametric causal mediation analysis in cluster-randomized trials with post-treatment confounding. arXiv:2611.11903v1. doi:10.48550/arXiv.2611.11903. The clustered mediation manual maps the code to its equations, distinguishes our restricted Gaussian backend and synthetic validation from their study, and records the current inferential limitations.

Software citation:

@software{Babayan2026_CausalMediation,
  author  = {Babayan, Simon A.},
  title   = {CausalMediation.jl},
  year    = {2026},
  version = {v0.3.1},
  doi     = {10.5281/zenodo.21812342},
  url     = {https://github.com/SimonAB/CausalMediation.jl}
}

Concept DOI (all versions): 10.5281/zenodo.21812341.

About

Cross-fitted causal mediation for Julia: interventional, natural, organic, controlled direct, and recanting-twin effects with moc and MTP EIF

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages