mod: Moderation

View source: R/mod.h.R

modR Documentation

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


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