Skip to content
Data-WisePublic

About

Infrastructure for Mediation Analysis in R

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

medfit

CRAN status Lifecycle: stable R-CMD-check Codecov test coverage pkgdown

r-universe

medfit fits mediation models and gives you effects with intervals. med() runs the analysis, quick() prints it, and nie(), nde(), te(), pm() and tidy() read it, for every model from one mediator to a latent variable.

Where to start

Install

install.packages("medfit",
  repos = c("https://data-wise.r-universe.dev", "https://cloud.r-project.org"))

Or from GitHub: pak::pak("data-wise/medfit"). Releases up to 0.3.2 are too old for the examples on this site (CRAN now carries a later one) (what changed).

Start here in 60 seconds

mediation_demo ships with the package, so this runs as written:

library(medfit)

result <- med(
  data = mediation_demo,
  treatment = "treatment", mediator = "mediator1", outcome = "outcome",
  covariates = c("covariate1", "covariate2")
)
quick(result)
#> NIE = 0.333  | NDE = 0.206 | PM = 61.8 %

You should see one line. NIE is the indirect effect through the mediator, NDE the direct effect, PM the share mediated. If you get an error instead, see When something fails.

Pick your model

More cases: the Models table. Which engine? The engine guide.

After the first run: pieces, table, interval
nie(result)   # indirect effect (a * b)
nde(result)   # direct effect (c')
te(result)    # total effect
pm(result)    # proportion mediated
paths(result) # a, b, c'

library(generics)
tidy(result)   # every effect with its standard error
#> # A tibble: 6 × 3
#>   term    estimate std_error
#>   <chr>      <dbl>     <dbl>
#> 1 a          0.583    0.0989
#> 2 b          0.571    0.0535
#> 3 c_prime    0.206    0.110 
#> 4 nie        0.333    0.0645
#> 5 nde        0.206    0.110 
#> 6 te         0.539    0.119 

glance(result)  # one-row model summary
#> # A tibble: 1 × 6
#>     nie   nde    te    pm  nobs converged
#>   <dbl> <dbl> <dbl> <dbl> <int> <lgl>    
#> 1 0.333 0.206 0.539 0.618   400 TRUE     

# With a bootstrap interval
result_boot <- med(
  data = mediation_demo,
  treatment = "treatment", mediator = "mediator1", outcome = "outcome",
  covariates = c("covariate1", "covariate2"),
  boot = TRUE, n_boot = 1000, seed = 42
)
quick(result_boot)
#> NIE = 0.333  [0.208, 0.458] | NDE = 0.206 | PM = 61.8 %

When something fails

Go further

Get Started | Cookbook (twenty-five recipes) | Reference Card | Methods and Formulas | Which interval should I use?

More, when you need it

What's new in 0.22.2
  • 0.22.2 fixes a test that failed on CRAN's macOS builders; no function changes.

  • 0.22.1 gives model links more touch space on phones and keeps the status badges from shifting as they load.

  • 0.22.0 expands the worked examples for model extraction and treatment-by-mediator interactions. Native two-level fits retain the existing variance rules.

  • 0.21.3 (2026-11-27, GitHub; accepted by CRAN and published 2026-11-28): no change to what any function returns; cuts the tests that run on CRAN by about half after CRAN's pretest measured the Windows check at 11 minutes against a 10-minute limit.

  • 0.21.2 (2026-11-26, GitHub only; CRAN's pretest failed it on Windows check time, so 0.21.3 replaced it): no change to what any function returns; fixes the notes CRAN's incoming checks raised (Rd usage, check time) and the open S7 issue on 0.3.2, and moves the slowest tests and gates to the local full run.

  • 0.21.1 (2026-11-26, GitHub only): no change to the package code since 0.21.0 (released 2026-11-25); the longest test files are skipped on CRAN, the test warnings are gone, and the cookbook has recipe 25 (MLR). The highlights of 0.21.0 follow.

  • 0.21.0 (2026-11-25, GitHub only): adds the MLR estimator and classed product refusals below since 0.20.0 (released 2026-11-23).

  • MLR estimator (0.21.0): fit_sem(model, data, estimator = "MLR") keeps the ML estimates, uses sandwich standard errors and adds the Yuan-Bentler scaled chi-square (chisq_scaled, scaling_factor, p_value_scaled in glance()), as lavaan's MLR. It is defined for single-level complete-data fits with linear equality constraints; anything else stops with a classed error. See the NEWS.

  • Classed product refusals (0.21.0): every refusal of the : product term has a class (medfit_error_product_*); messages and numbers are unchanged.

  • 0.20.0 (2026-11-23, GitHub only): adds the native two-level route below since 0.19.0 (released 2026-11-17). The two-level API is experimental, so it may change without a deprecation period.

  • Native two-level fit (0.20.0, experimental): fit_sem(model, data, cluster = ) fits a two-level (1-1-1) model written in level: blocks with the sem engine, and extract_mediation() on the fit returns TwoLevelMediationData; fit_mediation(engine = "sem", model = , cluster = ) returns the same object. se_type = "sandwich" gives cluster-robust standard errors. See the model page.

  • Tests on native two-level fits (0.20.0, experimental): mbco_sem() tests the indirect effect at one level, and twolevel_equal_indirect() tests that the within and between indirect effects are equal; a pair-sign twin certificate stops a worse local optimum from standing in the equality refit.

  • Behavior change (0.20.0, experimental): the within-only rule for a two-level fit now reads the unrestricted (h1) ICC on both routes, so within-only results are rarer. See the NEWS.

  • Two-level parallel mediation (0.19.0, experimental): extract_mediation() on a two-level lavaan fit with two or more mediators returns TwoLevelParallelMediationData, with per-mediator and total effects at each level, delta-method SEs and twolevel_statistic() for the bootstrap. One-mediator results are unchanged. See the model page.

  • The sem engine (0.18.0): fit_mediation(engine = "sem") names medfit's own SEM fitter; engine = "native" still works, with identical results. sem_control() collects its optimizer settings. See Which engine should I use?.

  • Renamed pages (0.18.0): the sem engine articles moved (native-guide is now sem-guide, and so on); old links redirect.

  • Bounded FIML expansion (0.17.0): one or two covariates, two parallel and two serial mediators (numerically verified, not statistically admitted). See the model page.

  • Two-level mediation (0.17.0, experimental): extract_mediation() on a two-level lavaan fit returns TwoLevelMediationData; lavaan floor 0.7-3. See the model page.

  • Before that: 0.16.0 (both sign branches of a nonzero-product equality, corrected constrained standard errors) and 0.15.0 (native defaults flip: fixed_x and meanstructure on when unset).

Every release is listed in NEWS; upgrading from CRAN 0.3.2? See What changed since CRAN 0.3.2.

Bounded FIML: settings and limits

medfit 0.16.0 added explicit fit_sem(missing = "fiml", fixed_x = FALSE) for three-variable Gaussian random-X mediation, preserving listwise defaults. medfit 0.17.0 also fits one or two covariates, two parallel mediators (with zero, one or two covariates) and two-mediator serial models (the saturated graph or the pure chain, with up to two covariates; implemented and verified against lavaan, not calibrated for coverage). It is unavailable in 0.15.0 and earlier. Observed/model SEs and unstandardized effects and normal intervals are supported. The model requires continuous X, M and Y, a free or exactly zero direct path, independent Gaussian disturbances, free intercepts and ignorable missingness under that joint model. Native fit_mediation() uses engine_args for the same opt-in. Extracted FIML objects refuse quick(), pm() and glance(); fit tests, profiles and MBCO remain unavailable. Source-fit retained-row and Gaussian parameter-draw intervals (bootstrap_mediation(source_fit = ...)) are implemented but withheld: the call is refused until statistical admission is decided. glance() on the source SEMFit retains likelihood/IC fields. See the native guide for settings and limits.

Already have fitted models?

extract_mediation() turns fitted models into the same objects (see Model Extraction):

fit_m <- lm(mediator1 ~ treatment + covariate1 + covariate2, data = mediation_demo)
fit_y <- lm(outcome ~ treatment + mediator1 + covariate1 + covariate2, data = mediation_demo)
med_data <- extract_mediation(fit_m, model_y = fit_y, treatment = "treatment", mediator = "mediator1")
nie(med_data)
Cluster-randomized trial

When the treatment is assigned to whole clusters (such as schools), use the lmer engine (needs the suggested lme4; add se_type = "kr" with pbkrtest for few clusters):

# 40 schools of 10 students, treatment assigned by school
set.seed(123)
J <- 40
n <- 10
school <- rep(seq_len(J), each = n)
cdat <- data.frame(school = factor(school), X = sample(rep(0:1, J / 2))[school])
cdat$M <- 0.5 * cdat$X + rnorm(J, sd = 0.5)[school] + rnorm(J * n)
cdat$Y <- 0.2 * cdat$X + 0.4 * cdat$M + rnorm(J, sd = 0.5)[school] + rnorm(J * n)

fit <- fit_mediation(
  Y ~ X + M, M ~ X, data = cdat,
  treatment = "X", mediator = "M",
  engine = "lmer", cluster = "school"
)

nie(fit)        # a * b_between
decompose(fit)  # own-mediator and spillover parts (large-cluster approximation)
#> Natural Indirect Effect (NIE): 0.328
#>        own  spillover        nie
#> 0.25763321 0.07036432 0.32799753

See Methods and Formulas for the estimand and the assumptions each effect needs.

Latent mediator (sem engine)

When the mediator is a construct measured by several indicators, write the model as syntax. The sem engine needs no other package:

# a latent mediator `eta` with three indicators, simulated
set.seed(11)
n <- 400
X <- rnorm(n)
eta <- 0.5 * X + rnorm(n, sd = 0.9)
ldat <- data.frame(
  X = X,
  m1 = eta + rnorm(n, sd = 0.7),
  m2 = 0.8 * eta + rnorm(n, sd = 0.7),
  m3 = 0.7 * eta + rnorm(n, sd = 0.8)
)
ldat$Y <- 0.4 * eta + 0.2 * X + rnorm(n, sd = 0.8)

syntax <- "
  eta =~ m1 + m2 + m3
  eta ~ a * X
  Y ~ b * eta + cp * X
"
fit <- fit_mediation(model = syntax, data = ldat, treatment = "X",
                     mediator = "eta", engine = "sem")
nie(fit)

mbco_sem(fit_sem(syntax, ldat), "a", "b")  # minimum of two likelihood-ratio tests
#> Natural Indirect Effect (NIE): 0.1685
#>
#> 	Minimum-of-two likelihood-ratio tests (MBCO) for an indirect effect
#>
#> data:  a * b = 0
#> MBCO = 46.753, df = 1, p-value = 8.054e-12
Classes and functions
Class Models
MediationData Simple mediation: X to M to Y (paths a, b, c')
InteractionMediationData Treatment-by-mediator interaction, four-way decomposition (CDE, INTref, INTmed, PIE)
SerialMediationData A chain X to M1 to M2 to Y; nie() is the chain, te()/pm() count every path
ParallelMediationData Mediators side by side; indirect effect is the sum of a_j * b_j
JointMediationData Joint natural effects of several mediators with products
ClusterMediationData Treatment assigned to clusters (2-1-1): own-mediator and spillover parts
TwoLevelMediationData A two-level lavaan fit with X, M and Y on level-1 units (1-1-1): the indirect effect at the within and the between level (0.17.0, experimental)
TwoLevelParallelMediationData The same two-level fit with two or more parallel mediators: per-mediator and total effects at each level (0.19.0, experimental)
FIMLMediationData, FIMLParallelMediationData, FIMLSerialMediationData Bounded FIML fits (fit_sem(missing = "fiml")): X to M to Y, two parallel mediators, two serial mediators (see above)
SEMFit A sem engine fit; extract_mediation() turns it into the classes above
BootstrapResult Point estimates, intervals and the bootstrap distribution
You want to Call
Run a mediation in one step med(), quick()
Read effects and paths nie(), nde(), te(), pm(), paths(), decompose()
Get a table tidy(), glance(), confint(), coef(), vcov(), nobs()
Fit with control fit_mediation() (engines glm, regmedint, lmer, sem)
Use fitted models extract_mediation() (lm, glm, lmer, lavaan, SEMFit)
Write a model as syntax fit_sem(), mbco_sem()
Bootstrap bootstrap_mediation() (parametric, nonparametric, plugin)

Every function is on the Reference Card. For the classes, see the Introduction; for resampling, Bootstrap Inference.

Mediationverse and related packages

medfit is the foundation for the mediationverse ecosystem:

Package Purpose Role
medfit (this) Model fitting, extraction, bootstrap Foundation
RMediation Confidence intervals (DOP, MBCO) Application
probmed Probabilistic effect size (P_med) Application
medrobust Sensitivity analysis Application (independent of medfit)
medsim Simulation infrastructure Support
mediationverse Meta-package Ecosystem

See Ecosystem Coordination for version compatibility and development guidelines.

Also: Package documentation, Development Guide, NEWS.

About

Infrastructure for Mediation Analysis in R

Resources

Contributing

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages