| Title: | Statistical Mediation Analysis for SEMs |
| Version: | 1.0.0 |
| Description: | Conducts mediation analysis for structural equation models (SEM) estimated with 'lavaan', 'blavaan', 'cSEM', or 'modsem'. Implements the Baron and Kenny (1986) <doi:10.1037/0022-3514.51.6.1173> and Zhao, Lynch & Chen (2010) <doi:10.1086/651257> approaches to determine the presence and type of mediation. Supports covariance-based SEM, partial least squares SEM, Bayesian SEM, and moderated mediation models. Reports indirect effects with standard errors from Sobel, Delta, Monte-Carlo, and bootstrap methods, along with effect size measures (RIT, RID). |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.3.2 |
| Imports: | lavaan, mvtnorm, ggplot2, dplyr, purrr, stats |
| URL: | https://github.com/ihrke/rmedsem, https://ihrke.github.io/rmedsem/ |
| BugReports: | https://github.com/ihrke/rmedsem/issues |
| Depends: | R (≥ 4.1.0) |
| LazyData: | true |
| Suggests: | blavaan, boot, cSEM, HDInterval, modsem, semPlot, rmarkdown, testthat (≥ 3.0.0) |
| Config/testthat/edition: | 3 |
| NeedsCompilation: | no |
| Packaged: | 2026-03-10 21:13:16 UTC; mmi041 |
| Author: | Mehmet Mehmetoglu |
| Maintainer: | Matthias Mittner <matthias.mittner@uit.no> |
| Repository: | CRAN |
| Date/Publication: | 2026-03-16 16:50:02 UTC |
Ratio of Indirect to Direct Effect (RID)
Description
Ratio of Indirect to Direct Effect (RID)
Usage
RID(res, ...)
## S3 method for class 'rmedsem'
RID(res, ...)
Arguments
res |
fitted |
... |
additional arguments (currently unused) |
Value
A numeric scalar giving the ratio of the indirect effect to the direct effect (indirect / direct).
Examples
mod.txt <- "
read ~ math
science ~ read + math
"
mod <- lavaan::sem(mod.txt, data=rmedsem::hsbdemo)
out <- rmedsem(mod, indep="math", med="read", dep="science")
RID(out)
Ratio of Indirect to Total Effect (RIT)
Description
Ratio of Indirect to Total Effect (RIT)
Usage
RIT(res, ...)
## S3 method for class 'rmedsem'
RIT(res, ...)
Arguments
res |
fitted |
... |
additional arguments (currently unused) |
Value
A numeric scalar giving the ratio of the indirect effect to the total effect (indirect / total).
Examples
mod.txt <- "
read ~ math
science ~ read + math
"
mod <- lavaan::sem(mod.txt, data=rmedsem::hsbdemo)
out <- rmedsem(mod, indep="math", med="read", dep="science")
RIT(out)
Upsilon Effect Size
Description
Returns the Upsilon effect size (Lachowicz, Preacher & Kelley, 2018), an R-squared-type measure representing the variance in Y explained indirectly by X through M.
Usage
Upsilon(res, ...)
## S3 method for class 'rmedsem'
Upsilon(res, adjusted = TRUE, ...)
Arguments
res |
fitted |
... |
additional arguments (currently unused) |
adjusted |
logical; if |
Value
A numeric scalar giving the Upsilon effect size, an R-squared-type measure of the variance in the dependent variable explained indirectly through the mediator.
Examples
mod.txt <- "
read ~ math
science ~ read + math
"
mod <- lavaan::sem(mod.txt, data=rmedsem::hsbdemo)
out <- rmedsem(mod, indep="math", med="read", dep="science",
effect.size=c("RIT","RID","upsilon"))
Upsilon(out)
Upsilon(out, adjusted=FALSE)
Convert an rmedsem Object to a Data Frame
Description
Convert an rmedsem Object to a Data Frame
Usage
## S3 method for class 'rmedsem'
as.data.frame(x, ...)
Arguments
x |
the |
... |
additional arguments (currently unused) |
Value
a data.frame
Examples
mod.txt <- "
read ~ math
science ~ read + math
"
mod <- lavaan::sem(mod.txt, data=rmedsem::hsbdemo)
out <- rmedsem(mod, indep="math", med="read", dep="science")
as.data.frame(out)
High School and Beyond Demo Dataset
Description
High School and Beyond Demo Dataset
Usage
hsbdemo
Format
hsbdemo
A data frame with 20 rows and 13 columns:
- id
Student ID
- female
Mate-Choice Survey Data from Trondheim
Description
Data from a survey on mate-choice in Trondheim.
Usage
mchoice
Format
mchoice
A data frame with 1090 rows and 9 columns:
- smv_attr_face
How well does this describe you as a partner?, [1] very bad - [5] very well
- smv_attr_body
How well does this describe you as a partner?, [1] very bad - [5] very well
- smv_sexy
How well does this describe you as a partner?, [1] very bad - [5] very well
- ses_satis
On the whole, I am satisfied with myself, [1] totally disgree - [5] totally agree
- ses_qualities
I feel that I have a number of good qualities, [1] totally disgree - [5] totally agree
- ses_able_todo
I am able to do things as well as most other people, [1] totally disgree - [5] totally agree
- mwb_optimistic
I have been feeling optimistic about the future, [1] never - [5] always
- mwb_useful
I have been feeling useful, [1] never - [5] always
- mwb_energy
I have had energy to spare, [1] never - [5] always
- smv_kind
?, [1] ? - [5] ?
- smv_caring
?, [1] ? - [5] ?
- smv_understanding
?, [1] ? - [5] ?
- smv_make_laughh
?, [1] ? - [5] ?
- smv_funny
?, [1] ? - [5] ?
- smv_sociable
?, [1] ? - [5] ?
Plot an rmedsem Object
Description
Creates a visualization of the mediation analysis results. By default,
produces a coefficient plot. Use type = "effect" for an effect size
pie chart.
Usage
## S3 method for class 'rmedsem'
plot(x, type = c("coef", "effect"), ...)
Arguments
x |
the |
type |
character; either |
... |
additional arguments passed to |
Value
a ggplot object
Examples
mod.txt <- "
read ~ math
science ~ read + math
"
mod <- lavaan::sem(mod.txt, data=rmedsem::hsbdemo)
out <- rmedsem(mod, indep="math", med="read", dep="science")
plot(out)
plot(out, type="effect")
Plot Coefficients for an rmedsem Object
Description
Plot Coefficients for an rmedsem Object
Usage
plot_coef(res)
Arguments
res |
the |
Value
a ggplot object
Examples
mod.txt <- "
read ~ math
science ~ read + math
"
mod <- lavaan::sem(mod.txt, data=rmedsem::hsbdemo)
out <- rmedsem(mod, indep="math", med="read", dep="science")
plot_coef(out)
Plot Effect Sizes for an rmedsem Object
Description
Plot Effect Sizes for an rmedsem Object
Usage
plot_effect(res, description = TRUE)
Arguments
res |
the |
description |
logical, whether to add a description subtitle |
Value
a ggplot object
Examples
mod.txt <- "
read ~ math
science ~ read + math
"
mod <- lavaan::sem(mod.txt, data=rmedsem::hsbdemo)
out <- rmedsem(mod, indep="math", med="read", dep="science")
plot_effect(out)
Indent and merge strings
Description
Indent and merge strings
Usage
pre_indent_merge(s, indent)
Arguments
s |
a string |
indent |
an integer how much to indent s |
Value
indented string
Print an rmedsem Object
Description
Print an rmedsem Object
Usage
## S3 method for class 'rmedsem'
print(x, digits = 3, indent = 3, ...)
Arguments
x |
the |
digits |
an integer, number of digits to print in table |
indent |
an integer, number of spaces to indent |
... |
additional arguments (currently unused) |
Value
the rmedsem object x (invisibly)
Examples
mod.txt <- "
read ~ math
science ~ read + math
"
mod <- lavaan::sem(mod.txt, data=rmedsem::hsbdemo)
out <- rmedsem(mod, indep="math", med="read", dep="science")
print(out)
Print rmedsem Results for Blavaan Models
Description
Print rmedsem Results for Blavaan Models
Usage
## S3 method for class 'rmedsem.blavaan'
print(x, digits = 3, indent = 3, ...)
Arguments
x |
the |
digits |
an integer, number of digits to print in table |
indent |
an integer, number of spaces to indent |
... |
additional arguments (currently unused) |
Value
rmedsem object x (invisibly)
Print rmedsem Results for Lavaan, cSEM, and Modsem Models
Description
Print rmedsem Results for Lavaan, cSEM, and Modsem Models
Usage
## S3 method for class 'rmedsem.lavaan.csem.modsem'
print(x, digits = 3, indent = 3, ci_moderation = FALSE, ...)
Arguments
x |
the |
digits |
an integer, number of digits to print in table |
indent |
an integer, number of spaces to indent |
ci_moderation |
a logical, whether to print confidence intervals for direct, indirect and total moderation effects |
... |
additional arguments (currently unused) |
Value
rmedsem object x (invisibly)
Print Effect Sizes from Mediation Analysis
Description
Print Effect Sizes from Mediation Analysis
Usage
print_effectsize(res, digits = 3, indent = 3)
Arguments
res |
the |
digits |
an integer, number of digits to print in table |
indent |
an integer, number of spaces to indent |
Value
rmedsem object res (invisibly)
Mediation Analysis for Structural Equation Models
Description
Conducts mediation analysis on a fitted SEM model using the Baron and Kenny (1986) and/or Zhao, Lynch & Chen (2010) approaches.
Usage
rmedsem(
mod,
indep,
med,
dep,
approach = c("bk", "zlc"),
p.threshold = 0.05,
effect.size = c("RIT", "RID", "upsilon"),
...
)
Arguments
mod |
a fitted SEM model object (from lavaan, blavaan, cSEM, or modsem) |
indep |
a string indicating the name of the independent variable |
med |
a string indicating the name of the mediator variable |
dep |
a string indicating the name of the dependent variable |
approach |
either |
p.threshold |
a numeric giving the p-value threshold for significance |
effect.size |
character vector; one or more of |
... |
additional arguments passed to methods |
Value
an object of class rmedsem
Examples
mod.txt <- "
read ~ math
science ~ read + math
"
mod <- lavaan::sem(mod.txt, data=rmedsem::hsbdemo)
out <- rmedsem(mod, indep="math", med="read", dep="science")
out
Mediation Analysis for Blavaan Models
Description
Mediation Analysis for Blavaan Models
Usage
## S3 method for class 'blavaan'
rmedsem(
mod,
indep,
med,
dep,
approach = c("bk", "zlc"),
p.threshold = 0.05,
effect.size = c("RIT", "RID", "upsilon"),
...
)
Arguments
mod |
A fitted SEM model (blavaan). Note that the model has to be fit using
|
indep |
A string indicating the name of the independent variable in the model. |
med |
A string indicating the name of the mediator variable in the model. |
dep |
A string indicating the name of the dependent variable in the model. |
approach |
either 'bk' or 'zlc' or both c("bk", "zlc") (default) |
p.threshold |
A double giving the p-value for determining whether a path is significant or not |
effect.size |
calculate different effect-sizes; one or more of "RIT", "RID" |
... |
additional arguments (currently unused) |
Value
A rmedsem structure containing the results from the analysis
Examples
## Not run:
# Requires blavaan and a MCMC backend (Stan/JAGS)
model02 <- "
# measurement model
ind60 =~ x1 + x2 + x3
dem60 =~ y1 + y2 + y3 + y4
dem65 =~ y5 + y6 + y7 + y8
# regressions
dem60 ~ ind60
dem65 ~ ind60 + dem60
"
mod <- blavaan::bsem(model02, data=lavaan::PoliticalDemocracy, std.lv=TRUE,
meanstructure=TRUE, n.chains=1,
save.lvs=TRUE, burnin=500, sample=500)
out <- rmedsem(mod, indep="ind60", med="dem60", dep="dem65")
print(out)
## End(Not run)
Mediation Analysis for cSEM Models
Description
Mediation Analysis for cSEM Models
Usage
## S3 method for class 'cSEMResults'
rmedsem(
mod,
indep,
med,
dep,
approach = c("bk", "zlc"),
p.threshold = 0.05,
effect.size = c("RIT", "RID", "upsilon"),
nbootstrap = 1000,
ci.two.tailed = 0.95,
...
)
Arguments
mod |
A fitted SEM model (cSEM). |
indep |
A string indicating the name of the independent variable in the model. |
med |
A string indicating the name of the mediator variable in the model. |
dep |
A string indicating the name of the dependent variable in the model. |
approach |
either 'bk' or 'zlc' or both c("bk", "zlc") (default) |
p.threshold |
A double giving the p-value for determining whether a path is significant or not |
effect.size |
calculate different effect-sizes; one or more of "RIT", "RID" |
nbootstrap |
number of bootstrap samples, default=1000 |
ci.two.tailed |
A double giving the confidence level for two-tailed confidence intervals (default 0.95) |
... |
additional arguments (currently unused) |
Value
A rmedsem structure containing the results from the analysis
Mediation Analysis for Lavaan Models
Description
Mediation Analysis for Lavaan Models
Usage
## S3 method for class 'lavaan'
rmedsem(
mod,
indep,
med,
dep,
approach = c("bk", "zlc"),
p.threshold = 0.05,
effect.size = c("RIT", "RID", "upsilon"),
standardized = TRUE,
mcreps = NULL,
ci.two.tailed = 0.95,
...
)
Arguments
mod |
A fitted SEM model (lavaan). |
indep |
A string indicating the name of the independent variable in the model. |
med |
A string indicating the name of the mediator variable in the model. |
dep |
A string indicating the name of the dependent variable in the model. |
approach |
either 'bk' or 'zlc' or both c("bk", "zlc") (default) |
p.threshold |
A double giving the p-value for determining whether a path is significant or not |
effect.size |
calculate different effect-sizes; one or more of "RIT", "RID" |
standardized |
A boolean indicating whether the coefficients should be standardized. The default value is FALSE. |
mcreps |
An integer determining the number of monte-carlo samples. |
ci.two.tailed |
A double giving the confidence level for two-tailed confidence intervals (default 0.95) |
... |
additional arguments (currently unused) |
Value
A rmedsem structure containing the results from the analysis
Examples
mod.txt <- "
read ~ math
science ~ read + math
"
mod <- lavaan::sem(mod.txt, data=rmedsem::hsbdemo)
out <- rmedsem(mod, indep="math", med="read", dep="science",
standardized=TRUE, mcreps=5000,
approach = c("bk","zlc"))
print(out)
Mediation Analysis for Modsem Models
Description
Mediation Analysis for Modsem Models
Usage
## S3 method for class 'modsem'
rmedsem(
mod,
indep,
med,
dep,
approach = c("bk", "zlc"),
p.threshold = 0.05,
effect.size = c("RIT", "RID", "upsilon"),
moderator = NULL,
standardized = TRUE,
mcreps = NULL,
ci.two.tailed = 0.95,
...
)
Arguments
mod |
A fitted SEM model (modsem). |
indep |
A string indicating the name of the independent variable in the model. |
med |
A string indicating the name of the mediator variable in the model. |
dep |
A string indicating the name of the dependent variable in the model. |
approach |
either 'bk' or 'zlc' or both c("bk", "zlc") (default) |
p.threshold |
A double giving the p-value for determining whether a path is significant or not |
effect.size |
calculate different effect-sizes; one or more of "RIT", "RID" |
moderator |
A string indicating the name of the moderator variable in the model. |
standardized |
A boolean indicating whether the coefficients should be standardized. The default value is FALSE. |
mcreps |
An integer determining the number of monte-carlo samples. |
ci.two.tailed |
A double giving the confidence level for two-tailed confidence intervals (default 0.95) |
... |
additional arguments (currently unused) |
Value
A rmedsem structure containing the results from the analysis
Examples
if (requireNamespace("modsem", quietly = TRUE)) {
m <- "
OwnLook =~ smv_attr_face + smv_attr_body + smv_sexy
SelfEst =~ ses_satis + ses_qualities + ses_able_todo
MentWell =~ mwb_optimistic + mwb_useful + mwb_energy
smv =~ smv_kind + smv_caring + smv_understanding +
smv_make_laughh + smv_funny + smv_sociable
SelfEst ~ OwnLook + smv + smv:OwnLook
MentWell ~ OwnLook + SelfEst + smv + smv:OwnLook
"
est <- modsem::modsem(m, data = mchoice, method="lms")
# mediated moderation
rmedsem(indep="smv:OwnLook", dep="MentWell", med="SelfEst", mod=est)
# moderated mediation
rmedsem(indep="OwnLook", dep="MentWell", med="SelfEst", mod=est, moderator="smv")
}
Summarize an rmedsem Object
Description
Prints the mediation analysis results to the console.
Usage
## S3 method for class 'rmedsem'
summary(object, ...)
Arguments
object |
the |
... |
additional arguments passed to |
Value
the rmedsem object (invisibly)
Examples
mod.txt <- "
read ~ math
science ~ read + math
"
mod <- lavaan::sem(mod.txt, data=rmedsem::hsbdemo)
out <- rmedsem(mod, indep="math", med="read", dep="science")
summary(out)
Validate common rmedsem arguments
Description
Validate common rmedsem arguments
Usage
validate_rmedsem_args(indep, med, dep, approach, p.threshold, effect.size)
Fitness Center Survey Data from Trondheim
Description
Data from a survey in a fitness center in Trondheim.
Usage
workout
Format
workout
A data frame with 246 rows and 12 columns:
- age
Age in years
- lweight
How important is following to workout- to loose weight
- calories
How important is following to workout- to burn calories
- cweight
How important is following to workout- to control my weight
- body
How important is following to workout- to have a good body
- appear
How important is following to workout- to improve my appearance
- attract
How important is following to workout- to look more attractive
- muscle
How important is following to workout- to develop my muscles
- strength
How important is following to workout- to get stronger
- endur
How important is following to workout- to increase my endurance
- face
How well does the following describe you as a person - attractive face
- sexy
How well does the following describe you as a person - sexy