med: Mediation

View source: R/med.h.R

medR Documentation

Mediation

Description

Simple mediation analysis. Estimates the indirect, direct and total effects of a model in which a predictor influences the dependent variable through a mediator. The model is fitted with the lavaan package (Rosseel, 2012); standard errors are computed with the delta method (equivalent to the Sobel test for the indirect effect) or by bootstrapping. Optionally provides the individual path estimates, an estimate plot, and an annotated path diagram of the model.

Usage

med(
  data,
  dep,
  med,
  pred,
  estMethod = "standard",
  bootstrap = 1000,
  test = TRUE,
  ci = FALSE,
  ciWidth = 95,
  pm = FALSE,
  paths = FALSE,
  label = FALSE,
  estPlot = FALSE,
  pathDiagram = FALSE,
  pathDiagramLabel = TRUE,
  pathDiagramEst = TRUE,
  pathDiagramSig = TRUE
)

Arguments

data

the data as a data frame

dep

a string naming the dependent variable

med

a string naming the mediator variable

pred

a string naming the predictor variable

estMethod

'standard' (default), or 'bootstrap', the estimation method to use

bootstrap

a number between 1 and 100000 (default: 1000) specifying the number of samples that need to been drawn in the bootstrap method

test

TRUE (default) or FALSE, provide 'Z' and 'p' values for the mediation estimates

ci

TRUE or FALSE (default), provide a confidence interval for the mediation estimates

ciWidth

a number between 50 and 99.9 (default: 95) specifying the confidence interval width that is used as 'ci'

pm

TRUE or FALSE (default), provide the percent mediation effect size for the mediation estimates

paths

TRUE or FALSE (default), provide the individual estimates of the paths in the mediation model

label

TRUE (default) or FALSE, provide insightful labels for all estimates

estPlot

TRUE or FALSE (default), provide an estimate plot where for each estimator the estimated coefficient and confidence intervals are plotted.

pathDiagram

TRUE or FALSE (default), provide a path diagram of the mediation model.

pathDiagramLabel

TRUE (default) or FALSE, annotate the path diagram arrows with the path labels.

pathDiagramEst

TRUE (default) or FALSE, annotate the path diagram arrows with the estimated coefficients.

pathDiagramSig

TRUE (default) or FALSE, annotate the path diagram arrows with significance stars.

Value

A results object containing:

results$med a table containing mediation estimates
results$paths a table containing the individual path estimates
results$pathDiagram an image
results$estPlot an image
results$modelSyntax the lavaan syntax used to fit the mediation model

Tables can be converted to data frames with asDF or as.data.frame. For example:

results$med$asDF

as.data.frame(results$med)

Examples

set.seed(1234)
X <- rnorm(100)
M <- 0.5*X + rnorm(100)
Y <- 0.7*M + rnorm(100)
dat <- data.frame(X=X, M=M, Y=Y)

med(dat, dep = "Y", pred = "X", med = "M")

#
#  Mediation Estimates
#  -----------------------------------------------------
#    Effect      Estimate    SE        Z        p
#  -----------------------------------------------------
#    Indirect      0.3736    0.0920    4.059    < .001
#    Direct        0.0364    0.1044    0.348     0.728
#    Total         0.4100    0.1247    3.287     0.001
#  -----------------------------------------------------
#
#


medmod documentation built on July 16, 2026, 9:07 a.m.