Learn R Programming

RMediation

RMediation provides rigorous statistical methods for mediation analysis in observational and experimental designs. It addresses the known limitations of normal-theory confidence intervals (e.g., Sobel test) by implementing advanced methods that account for the non-normal distribution of the indirect effect.

Key Capabilities

1. Rigorous Confidence Intervals

Compute accurate Confidence Intervals (CIs) for indirect effects using methods that outperform the standard normal approximation:

  • Distribution of the Product: Exact method for the product of two normal random variables.
  • Monte Carlo Method: Robust simulation-based intervals.
  • Bootstrapping: Parametric and semi-parametric bootstrap implementations.

2. Advanced Hypothesis Testing

  • LRT-MBCO: Implements the Likelihood Ratio Test via Model-Based Constrained Optimization, a powerful frequentist method for testing indirect effects that controls Type I error rates better than standard approaches.
  • Sobel Test: Asymptotic normal test included for baseline comparison.

3. Seamless Integration

  • Works directly with summary statistics (coefficients/SEs).
  • Extracts parameters automatically from fitted lavaan or OpenMx model objects.

Installation

You can install the stable version from CRAN:

install.packages("RMediation")

Or the development version from GitHub:

R

# install.packages("remotes")
remotes::install_github("data-wise/RMediation")

Usage

Using Summary Statistics to Calculate CIs

If you already have estimates from a published paper or other software, you can calculate CIs using coefficients ($\hat{a}, \hat{b}$) and their standard errors.

library(RMediation)

# Example: Single mediator
# a = 0.5, b = 0.6, se.a = 0.08, se.b = 0.04, rho = 0 (independence)
medci(mu.x = 0.5, mu.y = 0.6, se.x = 0.08, se.y = 0.04, rho = 0, type = "prodclin")

Using ci to Calculate CIs for Indirect Effects of Path with Two Sequential Mediators

library(RMediation)

# Example: Two sequential mediators
ci(mu = c(b1 = 1, b2 = .7, b3 = .6, b4 = .45),
  Sigma = c(.05, 0, 0, 0, .05, 0, 0, .03, 0, .03),
  quant = ~ b1 * b2 * b3 * b4, type = "MC", plot = TRUE, plotCI = TRUE)

Contributing

Contributions are welcome! If you encounter issues or have feature requests:

Citation

If you use RMediation in your research, please cite the following:

Package Reference: > Tofighi, D., & MacKinnon, D. P. (2011). RMediation: An R package for mediation analysis confidence intervals. Behavior Research Methods, 43, 692–700. doi:10.3758/s13428-011-0076-x

MBCO Method: > Tofighi, D., & Kelley, K. (2020). Improved inference in mediation analysis: Introducing the model-based constrained optimization procedure. Psychological Methods, 25(4), 496-515. doi:10.1037/met0000259

Bootstrap MBCO: > Tofighi, D. (2020). Bootstrap Model-Based Constrained Optimization Tests of Indirect Effects. Frontiers in Psychology, 10, 2989. doi:10.3389/fpsyg.2019.02989

License

RMediation is licensed under the GPL-3.0.

Copy Link

Version

Install

install.packages('RMediation')

Monthly Downloads

1,050

Version

1.3.0

License

GPL (>= 3) | file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Davood Tofighi

Last Published

November 20th, 2025

Functions in RMediation (1.3.0)

reexports

Objects exported from other packages
qprodnormal

Quantile for the Distribution of Product of Two Normal Variables
pprodnormal

Percentile for the Distribution of Product of Two Normal Variables
tidy.logLik

Creates a data.frame for a log-likelihood object
mbco

Model-based Constrained Optimization (MBCO) Chi-squared Test
memory_exp

Memory Experiment Data Description from MacKinnon et al., 2018
medci

Confidence Interval for the Mediated Effect
RMediation-package

RMediation: Mediation Analysis Confidence Intervals
qMC

Quantile for the Monte Carlo Sampling Distribution of a nonlinear function of coefficients estimates
ci

CI for a nonlinear function of coefficients estimates
pMC

Probability (percentile) for the Monte Carlo Sampling Distribution of a nonlinear function of coefficients estimates