Package {medmod}


Type: Package
Title: Simple Mediation and Moderation Analysis
Version: 1.2.0
Date: 2026-07-14
Description: This toolbox allows you to do simple mediation and moderation analysis. Models are estimated with the 'lavaan' package by Rosseel (2012) <doi:10.18637/jss.v048.i02>; standard errors for the mediation estimates are computed with the delta method following Sobel (1982) <doi:10.2307/270723> or by bootstrapping. It is also available as a module for 'jamovi' (see https://www.jamovi.org for more information). You can find an in depth tutorial on the 'lavaan' model syntax used for this package on https://lavaan.ugent.be/tutorial/index.html.
License: GPL-2 | GPL-3 [expanded from: GPL (≥ 2)]
Encoding: UTF-8
Depends: R (≥ 3.2)
Imports: jmvcore (≥ 1.0.0), R6, lavaan, ggplot2 (≥ 3.4.0), rlang
Suggests: testthat
URL: https://github.com/raviselker/medmod
BugReports: https://github.com/raviselker/medmod/issues
Config/roxygen2/version: 8.0.0
NeedsCompilation: no
Packaged: 2026-07-15 21:14:14 UTC; raviselker
Author: Ravi Selker [aut, cre]
Maintainer: Ravi Selker <selker.ravi@gmail.com>
Repository: CRAN
Date/Publication: 2026-07-16 04:10:02 UTC

medmod: Simple Mediation and Moderation Analysis

Description

This toolbox allows you to do simple mediation and moderation analysis. Models are estimated with the 'lavaan' package by Rosseel (2012) doi:10.18637/jss.v048.i02; standard errors for the mediation estimates are computed with the delta method following Sobel (1982) doi:10.2307/270723 or by bootstrapping. It is also available as a module for 'jamovi' (see https://www.jamovi.org for more information). You can find an in depth tutorial on the 'lavaan' model syntax used for this package on https://lavaan.ugent.be/tutorial/index.html.

Details

Simple mediation analysis med()
Simple moderation analysis mod()

Author(s)

Maintainer: Ravi Selker selker.ravi@gmail.com

Authors:

See Also

Useful links:


Center a vector

Description

center returns a numeric vector with centered values.

Usage

center(x)

Arguments

x

Numeric vector.


Draw a path diagram

Description

drawPathDiagram renders boxes and labelled arrows as a ggplot. Arrows run between box borders; an edge whose to is not a node name ends at its explicit toX/toY point (used to point at a path).

Usage

drawPathDiagram(nodes, edges, ggtheme, theme, sigCaption = TRUE)

Arguments

nodes

Data frame with name, label, x, y.

edges

Data frame with from, to, toX, toY, fromX, fromY, label, nudgeX, nudgeY (label offset from the arrow midpoint) and hjust (1 grows the label leftwards from its position, 0 rightwards, 0.5 centers it, so the gap to the arrow stays constant however long the label is). Arrows run between box borders by default; non-NA fromX/fromY and toX/toY override the anchor points.

ggtheme

The jamovi ggplot2 theme.

theme

The jamovi theme colors.

sigCaption

Whether to caption the significance star cutoffs.


Extract parameter rows from lavaan estimates

Description

lavaanRow returns the rows of a lavaan::parameterestimates() data frame whose columns equal the given values, e.g. lavaanRow(est, label = 'ab') or lavaanRow(est, lhs = dep, rhs = pred). It errors when the expected parameter is absent instead of silently returning zero rows.

Usage

lavaanRow(est, ...)

Arguments

est

Data frame of parameter estimates.

...

Named values; each name is a column of est that must equal the value.


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
#  -----------------------------------------------------
#
#


Moderation

Description

Simple moderation analysis. Estimates a model in which the effect of a predictor on the dependent variable depends on the value of a moderator, testing the main effects of predictor and moderator and their interaction. All variables are mean-centered before the model is fitted with the lavaan package (Rosseel, 2012); standard errors are computed with the delta method or by bootstrapping. Optionally provides a simple slope analysis with a corresponding plot, and an annotated path diagram of the model.

Usage

mod(
  data,
  dep,
  mod,
  pred,
  estMethod = "standard",
  bootstrap = 1000,
  label = FALSE,
  test = TRUE,
  ci = FALSE,
  ciWidth = 95,
  pathDiagram = FALSE,
  pathDiagramLabel = TRUE,
  pathDiagramEst = TRUE,
  pathDiagramSig = TRUE,
  pathDiagramMainEffect = FALSE,
  simpleSlopeEst = FALSE,
  simpleSlopePlot = FALSE
)

Arguments

data

the data as a data frame

dep

a string naming the dependent variable

mod

a string naming the moderator 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

label

TRUE or FALSE (default), provide the coefficient labels (b1, b2, b3) used in the model and the path diagram

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'

pathDiagram

TRUE or FALSE (default), provide a conceptual path diagram of the moderation 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.

pathDiagramMainEffect

TRUE or FALSE (default), draw the moderator's main effect on the dependent variable (b2) in the path diagram.

simpleSlopeEst

TRUE or FALSE (default), provide the estimates of the simple slopes.

simpleSlopePlot

TRUE or FALSE (default), provide a plot of the simple slopes.

Value

A results object containing:

results$mod a table containing moderation estimates
results$pathDiagram an image
results$simpleSlope$estimates a table containing the simple slope estimates
results$simpleSlope$plot an image
results$modelSyntax the lavaan syntax used to fit the moderation model

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

results$mod$asDF

as.data.frame(results$mod)

Examples

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

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

#
#  Moderation Estimates
#  --------------------------------------------------
#             Estimate    SE        Z        p
#  --------------------------------------------------
#    X           0.951    0.0965     9.86    < .001
#    M          -0.471    0.0923    -5.10    < .001
#    X:M         4.185    0.1009    41.50    < .001
#  --------------------------------------------------
#
#


Format a path coefficient annotation

Description

pathLabel builds the arrow annotation for a path diagram as a plotmath expression, e.g. 'b[1] == "0.42***"', so subscripted path names render as real subscripts on any graphics device.

Usage

pathLabel(name, est, p, showName = TRUE, showEst = TRUE, showSig = TRUE)

Arguments

name

Path name as plotmath (e.g. 'a' or 'b[1]').

est

Estimated coefficient.

p

The p value, converted to significance stars.

showName

Whether to include the path name.

showEst

Whether to include the estimate.

showSig

Whether to include the significance stars.


Shorten labels for display

Description

shortenLabel truncates long labels with an ellipsis so they fit inside path diagram boxes.

Usage

shortenLabel(x, n = 16)

Arguments

x

Character vector of labels.

n

Maximum number of characters.


Significance stars for p values

Description

sigStars converts p values to the conventional significance stars: * p < .05, ** p < .01, *** p < .001.

Usage

sigStars(p)

Arguments

p

Numeric vector of p values.