| Title: | Statistical Mediation Analysis for SEMs |
|---|---|
| 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). |
| Authors: | Mehmet Mehmetoglu [aut] (ORCID: <https://orcid.org/0000-0002-6092-8551>), Matthias Mittner [aut, cre] (ORCID: <https://orcid.org/0000-0003-0205-7353>), Kjell Slupphaug [aut] (ORCID: <https://orcid.org/0009-0005-8324-2834>) |
| Maintainer: | Matthias Mittner <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.0.9000 |
| Built: | 2026-05-17 05:20:56 UTC |
| Source: | https://github.com/ihrke/rmedsem |
Convert an rmedsem Object to a Data Frame
## S3 method for class 'rmedsem' as.data.frame(x, ...)## S3 method for class 'rmedsem' as.data.frame(x, ...)
x |
the |
... |
additional arguments (currently unused) |
a data.frame
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)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
hsbdemohsbdemo
hsbdemoA data frame with 20 rows and 13 columns:
Student ID
Data from a survey on mate-choice in Trondheim.
mchoicemchoice
mchoiceA data frame with 1090 rows and 9 columns:
How well does this describe you as a partner?, [1] very bad - [5] very well
How well does this describe you as a partner?, [1] very bad - [5] very well
How well does this describe you as a partner?, [1] very bad - [5] very well
On the whole, I am satisfied with myself, [1] totally disgree - [5] totally agree
I feel that I have a number of good qualities, [1] totally disgree - [5] totally agree
I am able to do things as well as most other people, [1] totally disgree - [5] totally agree
I have been feeling optimistic about the future, [1] never - [5] always
I have been feeling useful, [1] never - [5] always
I have had energy to spare, [1] never - [5] always
?, [1] ? - [5] ?
?, [1] ? - [5] ?
?, [1] ? - [5] ?
?, [1] ? - [5] ?
?, [1] ? - [5] ?
?, [1] ? - [5] ?
Plot Coefficients for an rmedsem Object
plot_coef(res)plot_coef(res)
res |
the |
a ggplot object
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)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
plot_effect(res, description = TRUE)plot_effect(res, description = TRUE)
res |
the |
description |
logical, whether to add a description subtitle |
a ggplot object
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)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)
Creates a visualization of the mediation analysis results. By default,
produces a coefficient plot. Use type = "effect" for an effect size
pie chart.
## S3 method for class 'rmedsem' plot(x, type = c("coef", "effect"), ...)## S3 method for class 'rmedsem' plot(x, type = c("coef", "effect"), ...)
x |
the |
type |
character; either |
... |
additional arguments passed to |
a ggplot object
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")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")
Print an rmedsem Object
## S3 method for class 'rmedsem' print(x, digits = 3, indent = 3, ...)## S3 method for class 'rmedsem' print(x, digits = 3, indent = 3, ...)
x |
the |
digits |
an integer, number of digits to print in table |
indent |
an integer, number of spaces to indent |
... |
additional arguments (currently unused) |
the rmedsem object x (invisibly)
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)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)
Ratio of Indirect to Direct Effect (RID)
RID(res, ...) ## S3 method for class 'rmedsem' RID(res, ...)RID(res, ...) ## S3 method for class 'rmedsem' RID(res, ...)
res |
fitted |
... |
additional arguments (currently unused) |
A numeric scalar giving the ratio of the indirect effect to the direct effect (indirect / direct).
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)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)
RIT(res, ...) ## S3 method for class 'rmedsem' RIT(res, ...)RIT(res, ...) ## S3 method for class 'rmedsem' RIT(res, ...)
res |
fitted |
... |
additional arguments (currently unused) |
A numeric scalar giving the ratio of the indirect effect to the total effect (indirect / total).
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)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)
Conducts mediation analysis on a fitted SEM model using the Baron and Kenny (1986) and/or Zhao, Lynch & Chen (2010) approaches.
rmedsem( mod, indep, med, dep, approach = c("bk", "zlc"), p.threshold = 0.05, effect.size = c("RIT", "RID", "upsilon"), ... )rmedsem( mod, indep, med, dep, approach = c("bk", "zlc"), p.threshold = 0.05, effect.size = c("RIT", "RID", "upsilon"), ... )
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 |
an object of class rmedsem
mod.txt <- " read ~ math science ~ read + math " mod <- lavaan::sem(mod.txt, data=rmedsem::hsbdemo) out <- rmedsem(mod, indep="math", med="read", dep="science") outmod.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
## S3 method for class 'blavaan' rmedsem( mod, indep, med, dep, approach = c("bk", "zlc"), p.threshold = 0.05, effect.size = c("RIT", "RID", "upsilon"), ... )## S3 method for class 'blavaan' rmedsem( mod, indep, med, dep, approach = c("bk", "zlc"), p.threshold = 0.05, effect.size = c("RIT", "RID", "upsilon"), ... )
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) |
A rmedsem structure containing the results from the analysis
## 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)## 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
## 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, ... )## 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, ... )
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) |
A rmedsem structure containing the results from the analysis
Mediation Analysis for Lavaan Models
## 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, ... )## 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, ... )
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) |
A rmedsem structure containing the results from the analysis
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)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
## 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, ... )## 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, ... )
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) |
A rmedsem structure containing the results from the analysis
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") }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") }
Prints the mediation analysis results to the console.
## S3 method for class 'rmedsem' summary(object, ...)## S3 method for class 'rmedsem' summary(object, ...)
object |
the |
... |
additional arguments passed to |
the rmedsem object (invisibly)
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)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)
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.
Upsilon(res, ...) ## S3 method for class 'rmedsem' Upsilon(res, adjusted = TRUE, ...)Upsilon(res, ...) ## S3 method for class 'rmedsem' Upsilon(res, adjusted = TRUE, ...)
res |
fitted |
... |
additional arguments (currently unused) |
adjusted |
logical; if |
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.
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)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)
Data from a survey in a fitness center in Trondheim.
workoutworkout
workoutA data frame with 246 rows and 12 columns:
Age in years
How important is following to workout- to loose weight
How important is following to workout- to burn calories
How important is following to workout- to control my weight
How important is following to workout- to have a good body
How important is following to workout- to improve my appearance
How important is following to workout- to look more attractive
How important is following to workout- to develop my muscles
How important is following to workout- to get stronger
How important is following to workout- to increase my endurance
How well does the following describe you as a person - attractive face
How well does the following describe you as a person - sexy