Title: | Mediation, Moderation and Moderated-Mediation After Model Fitting |
---|---|
Description: | Computes indirect effects, conditional effects, and conditional indirect effects in a structural equation model or path model after model fitting, with no need to define any user parameters or label any paths in the model syntax, using the approach presented in Cheung and Cheung (2023) <doi:10.3758/s13428-023-02224-z>. Can also form bootstrap confidence intervals by doing bootstrapping only once and reusing the bootstrap estimates in all subsequent computations. Supports bootstrap confidence intervals for standardized (partially or completely) indirect effects, conditional effects, and conditional indirect effects as described in Cheung (2009) <doi:10.3758/BRM.41.2.425> and Cheung, Cheung, Lau, Hui, and Vong (2022) <doi:10.1037/hea0001188>. Model fitting can be done by structural equation modeling using lavaan() or regression using lm(). |
Authors: | Shu Fai Cheung [aut, cre] , Sing-Hang Cheung [aut] |
Maintainer: | Shu Fai Cheung <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.2.5 |
Built: | 2024-12-09 09:43:03 UTC |
Source: | CRAN |
Check all indirect paths in a model and
return them as a list of arguments of x
, y
,
and m
, to be used by indirect_effect()
.
all_indirect_paths( fit = NULL, exclude = NULL, x = NULL, y = NULL, group = NULL ) all_paths_to_df(all_paths)
all_indirect_paths( fit = NULL, exclude = NULL, x = NULL, y = NULL, group = NULL ) all_paths_to_df(all_paths)
fit |
A fit object. It can be the output of
|
exclude |
A character vector of variables to be excluded in the search, such as control variables. |
x |
A character vector of variables that will be
included as the |
y |
A character vector of variables that will be
included as the |
group |
Either the group number
as appeared in the |
all_paths |
An |
It makes use of igraph::all_simple_paths()
to identify paths in a model.
Since Version 0.1.14.2, support for
multigroup models has been added for models
fitted by lavaan
. If a model has more
than one group and group
is not
specified, than paths in all groups
will be returned. If group
is
specified, than only paths in the
selected group will be returned.
all_indirect_paths()
returns
a list of the class all_paths
. Each argument is a
list of three character vectors,
x
, the name of the predictor that starts a path, y
,
the name of the outcome that ends a path, and m
, a
character vector of one or more names of the mediators,
from x
to y
. This class has a print method.
all_paths_to_df()
returns a data frame with three
columns, x
, y
, and m
, which can be used by
functions such as indirect_effect()
.
all_indirect_paths()
: Enumerate all indirect paths.
all_paths_to_df()
: Convert the output of
all_indirect_paths()
to a data frame with
three columns: x
, y
, and m
.
Shu Fai Cheung https://orcid.org/0000-0002-9871-9448
indirect_effect()
, lm2list()
.
many_indirect_effects()
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths out1 <- all_indirect_paths(fit) out1 names(out1) # Exclude c1 and c2 in the search out2 <- all_indirect_paths(fit, exclude = c("c1", "c2")) out2 names(out2) # Exclude c1 and c2, and only consider paths start # from x and end at y out3 <- all_indirect_paths(fit, exclude = c("c1", "c2"), x = "x", y = "y") out3 names(out3) # Multigroup models data(data_med_complicated_mg) mod <- " m11 ~ x1 + x2 + c1 + c2 m12 ~ m11 + c1 + c2 m2 ~ x1 + x2 + c1 + c2 y1 ~ m11 + m12 + x1 + x2 + c1 + c2 y2 ~ m2 + x1 + x2 + c1 + c2 " fit <- sem(mod, data_med_complicated_mg, group = "group") summary(fit) all_indirect_paths(fit, x = "x1", y = "y1") all_indirect_paths(fit, x = "x1", y = "y1", group = 1) all_indirect_paths(fit, x = "x1", y = "y1", group = "Group B")
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths out1 <- all_indirect_paths(fit) out1 names(out1) # Exclude c1 and c2 in the search out2 <- all_indirect_paths(fit, exclude = c("c1", "c2")) out2 names(out2) # Exclude c1 and c2, and only consider paths start # from x and end at y out3 <- all_indirect_paths(fit, exclude = c("c1", "c2"), x = "x", y = "y") out3 names(out3) # Multigroup models data(data_med_complicated_mg) mod <- " m11 ~ x1 + x2 + c1 + c2 m12 ~ m11 + c1 + c2 m2 ~ x1 + x2 + c1 + c2 y1 ~ m11 + m12 + x1 + x2 + c1 + c2 y2 ~ m2 + x1 + x2 + c1 + c2 " fit <- sem(mod, data_med_complicated_mg, group = "group") summary(fit) all_indirect_paths(fit, x = "x1", y = "y1") all_indirect_paths(fit, x = "x1", y = "y1", group = 1) all_indirect_paths(fit, x = "x1", y = "y1", group = "Group B")
It checks whether a path, usually an indirect path, exists in a model.
check_path(x, y, m = NULL, fit = NULL, est = NULL)
check_path(x, y, m = NULL, fit = NULL, est = NULL)
x |
Character. The name of predictor at the start of the path. |
y |
Character. The name of the outcome variable at the end of the path. |
m |
A vector of the variable
names of the mediators. The path goes
from the first mediator successively
to the last mediator. If |
fit |
The fit object. Currently
only supports a
|
est |
The output of
|
It checks whether the path
defined by a predictor (x
), an
outcome (y
), and optionally a
sequence of mediators (m
), exists
in a model. It can check models in a
lavaan::lavaan-class
object or a
list of outputs of lm()
.
It also support
lavaan.mi
objects
returned by
semTools::runMI()
or
its wrapper, such as semTools::sem.mi()
.
For example, in the following model
in lavaan
syntax
m1 ~ x m2 ~ m1 m3 ~ x y ~ m2 + m3
This path is valid: x = "x", y = "y", m = c("m1", "m2")
This path is invalid: x = "x", y = "y", m = c("m2")
This path is also invalid: x = "x", y = "y", m = c("m1", "m2")
A logical vector of length
one. TRUE
if the path is valid,
FALSE
if the path is invalid.
library(lavaan) data(data_serial_parallel) dat <- data_serial_parallel mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE) # The following paths are valid check_path(x = "x", y = "y", m = c("m11", "m12"), fit = fit) check_path(x = "x", y = "y", m = "m2", fit = fit) # The following paths are invalid check_path(x = "x", y = "y", m = c("m11", "m2"), fit = fit) check_path(x = "x", y = "y", m = c("m12", "m11"), fit = fit)
library(lavaan) data(data_serial_parallel) dat <- data_serial_parallel mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE) # The following paths are valid check_path(x = "x", y = "y", m = c("m11", "m12"), fit = fit) check_path(x = "x", y = "y", m = "m2", fit = fit) # The following paths are invalid check_path(x = "x", y = "y", m = c("m11", "m2"), fit = fit) check_path(x = "x", y = "y", m = c("m12", "m11"), fit = fit)
Extract the change in conditional indirect effect.
## S3 method for class 'cond_indirect_diff' coef(object, ...)
## S3 method for class 'cond_indirect_diff' coef(object, ...)
object |
The output of
|
... |
Optional arguments. Ignored. |
The coef
method of the
cond_indirect_diff
-class object.
Scalar: The change of
conditional indirect effect in
object
.
Return the estimates of
the conditional indirect effects or
conditional effects for all levels in
the output of
cond_indirect_effects()
.
## S3 method for class 'cond_indirect_effects' coef(object, ...)
## S3 method for class 'cond_indirect_effects' coef(object, ...)
object |
The output of
|
... |
Optional arguments. Ignored by the function. |
It extracts and returns the
column ind
or std
in the output
of cond_indirect_effects()
.
A numeric vector: The estimates of the conditional effects or conditional indirect effects.
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ x + w1 + x:w1 m2 ~ m1 y ~ m2 + x + w4 + m2:w4 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # Conditional effects from x to m1 when w1 is equal to each of the levels out1 <- cond_indirect_effects(x = "x", y = "m1", wlevels = c("w1"), fit = fit) out1 coef(out1) # Conditional indirect effects from x1 through m1 and m2 to y, out2 <- cond_indirect_effects(x = "x", y = "y", m = c("m1", "m2"), wlevels = c("w1", "w4"), fit = fit) out2 coef(out2) # Standardized conditional indirect effects from x1 through m1 and m2 to y, out2std <- cond_indirect_effects(x = "x", y = "y", m = c("m1", "m2"), wlevels = c("w1", "w4"), fit = fit, standardized_x = TRUE, standardized_y = TRUE) out2std coef(out2std)
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ x + w1 + x:w1 m2 ~ m1 y ~ m2 + x + w4 + m2:w4 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # Conditional effects from x to m1 when w1 is equal to each of the levels out1 <- cond_indirect_effects(x = "x", y = "m1", wlevels = c("w1"), fit = fit) out1 coef(out1) # Conditional indirect effects from x1 through m1 and m2 to y, out2 <- cond_indirect_effects(x = "x", y = "y", m = c("m1", "m2"), wlevels = c("w1", "w4"), fit = fit) out2 coef(out2) # Standardized conditional indirect effects from x1 through m1 and m2 to y, out2std <- cond_indirect_effects(x = "x", y = "y", m = c("m1", "m2"), wlevels = c("w1", "w4"), fit = fit, standardized_x = TRUE, standardized_y = TRUE) out2std coef(out2std)
Return the estimate of Delta_Med in a 'delta_med'-class object.
## S3 method for class 'delta_med' coef(object, ...)
## S3 method for class 'delta_med' coef(object, ...)
object |
The output of
|
... |
Optional arguments. Ignored. |
It just extracts and
returns the element delta_med
in the output of delta_med()
,
the estimate of the Delta_Med
proposed by Liu, Yuan, and Li (2023),
an -like measure of indirect
effect.
A scalar: The estimate of Delta_Med.
Shu Fai Cheung https://orcid.org/0000-0002-9871-9448
Liu, H., Yuan, K.-H., & Li, H. (2023). A systematic framework for defining R-squared measures in mediation analysis. Psychological Methods. Advance online publication. https://doi.org/10.1037/met0000571
library(lavaan) dat <- data_med mod <- " m ~ x y ~ m + x " fit <- sem(mod, dat) dm <- delta_med(x = "x", y = "y", m = "m", fit = fit) dm print(dm, full = TRUE) coef(dm)
library(lavaan) dat <- data_med mod <- " m ~ x y ~ m + x " fit <- sem(mod, dat) dm <- delta_med(x = "x", y = "y", m = "m", fit = fit) dm print(dm, full = TRUE) coef(dm)
Return the estimate of
the indirect effect in the output of
indirect_effect()
or or the
conditional indirect in the output of
cond_indirect()
.
## S3 method for class 'indirect' coef(object, ...)
## S3 method for class 'indirect' coef(object, ...)
object |
The output of
|
... |
Optional arguments. Ignored by the function. |
It extracts and returns the
element indirect
. in an object.
If standardized effect is requested
when calling indirect_effect()
or
cond_indirect()
, the effect
returned is also standardized.
A scalar: The estimate of the indirect effect or conditional indirect effect.
indirect_effect()
and
cond_indirect()
.
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ x + w1 + x:w1 m2 ~ x y ~ m1 + m2 + x " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # Examples for indirect_effect(): # Inidrect effect from x through m2 to y out1 <- indirect_effect(x = "x", y = "y", m = "m2", fit = fit) out1 coef(out1) # Conditional Indirect effect from x1 through m1 to y, # when w1 is 1 SD above mean hi_w1 <- mean(dat$w1) + sd(dat$w1) out2 <- cond_indirect(x = "x", y = "y", m = "m1", wvalues = c(w1 = hi_w1), fit = fit) out2 coef(out2)
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ x + w1 + x:w1 m2 ~ x y ~ m1 + m2 + x " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # Examples for indirect_effect(): # Inidrect effect from x through m2 to y out1 <- indirect_effect(x = "x", y = "y", m = "m2", fit = fit) out1 coef(out1) # Conditional Indirect effect from x1 through m1 to y, # when w1 is 1 SD above mean hi_w1 <- mean(dat$w1) + sd(dat$w1) out2 <- cond_indirect(x = "x", y = "y", m = "m1", wvalues = c(w1 = hi_w1), fit = fit) out2 coef(out2)
Return the estimates of
the indirect effects in the output of
many_indirect_effects()
.
## S3 method for class 'indirect_list' coef(object, ...)
## S3 method for class 'indirect_list' coef(object, ...)
object |
The output of
|
... |
Optional arguments. Ignored by the function. |
It extracts the estimates in each 'indirect'-class object in the list.
If standardized effect is requested
when calling many_indirect_effects()
,
the effects
returned are also standardized.
A numeric vector of the indirect effects.
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths from x to y paths <- all_indirect_paths(fit, x = "x", y = "y") paths # Indirect effect estimates out <- many_indirect_effects(paths, fit = fit) out coef(out)
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths from x to y paths <- all_indirect_paths(fit, x = "x", y = "y") paths # Indirect effect estimates out <- many_indirect_effects(paths, fit = fit) out coef(out)
Return the proportion
of effect mediated in the output of
indirect_proportion()
.
## S3 method for class 'indirect_proportion' coef(object, ...)
## S3 method for class 'indirect_proportion' coef(object, ...)
object |
The output of
|
... |
Not used. |
It extracts and returns the
element proportion
in the input
object.
A scalar: The proportion of effect mediated.
library(lavaan) dat <- data_med head(dat) mod <- " m ~ x + c1 + c2 y ~ m + x + c1 + c2 " fit <- sem(mod, dat, fixed.x = FALSE) out <- indirect_proportion(x = "x", y = "y", m = "m", fit = fit) out coef(out)
library(lavaan) dat <- data_med head(dat) mod <- " m ~ x + c1 + c2 y ~ m + x + c1 + c2 " fit <- sem(mod, dat, fixed.x = FALSE) out <- indirect_proportion(x = "x", y = "y", m = "m", fit = fit) out coef(out)
Returns the path
coefficients of the terms in an
lm_from_lavaan
-class object.
## S3 method for class 'lm_from_lavaan' coef(object, ...)
## S3 method for class 'lm_from_lavaan' coef(object, ...)
object |
A 'lm_from_lavaan'-class object. |
... |
Additional arguments. Ignored. |
An lm_from_lavaan
-class
object converts a regression model
for a variable in a lavaan
-class
object to a formula
-class object.
This function simply extracts the
path coefficients estimates.
Intercept is always included, and set
to zero if mean structure is not in
the source lavaan
-class object.
This is an advanced helper used by
plot.cond_indirect_effects()
.
Exported for advanced users and
developers.
A numeric vector of the path coefficients.
library(lavaan) data(data_med) mod <- " m ~ a * x + c1 + c2 y ~ b * m + x + c1 + c2 " fit <- sem(mod, data_med, fixed.x = FALSE) fit_list <- lm_from_lavaan_list(fit) coef(fit_list$m) coef(fit_list$y)
library(lavaan) data(data_med) mod <- " m ~ a * x + c1 + c2 y ~ b * m + x + c1 + c2 " fit <- sem(mod, data_med, fixed.x = FALSE) fit_list <- lm_from_lavaan_list(fit) coef(fit_list$m) coef(fit_list$y)
Compute the conditional
effects, indirect effects, or
conditional indirect effects in a
structural model fitted by lm()
,
lavaan::sem()
, or semTools::sem.mi()
.
cond_indirect( x, y, m = NULL, fit = NULL, est = NULL, implied_stats = NULL, wvalues = NULL, standardized_x = FALSE, standardized_y = FALSE, boot_ci = FALSE, level = 0.95, boot_out = NULL, R = 100, seed = NULL, parallel = TRUE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE, save_boot_full = FALSE, prods = NULL, get_prods_only = FALSE, save_boot_out = TRUE, mc_ci = FALSE, mc_out = NULL, save_mc_full = FALSE, save_mc_out = TRUE, ci_out = NULL, save_ci_full = FALSE, save_ci_out = TRUE, ci_type = NULL, group = NULL, boot_type = c("perc", "bc") ) cond_indirect_effects( wlevels, x, y, m = NULL, fit = NULL, w_type = "auto", w_method = "sd", sd_from_mean = NULL, percentiles = NULL, est = NULL, implied_stats = NULL, boot_ci = FALSE, R = 100, seed = NULL, parallel = TRUE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE, boot_out = NULL, output_type = "data.frame", mod_levels_list_args = list(), mc_ci = FALSE, mc_out = NULL, ci_out = NULL, ci_type = NULL, boot_type = c("perc", "bc"), groups = NULL, ... ) indirect_effect( x, y, m = NULL, fit = NULL, est = NULL, implied_stats = NULL, standardized_x = FALSE, standardized_y = FALSE, boot_ci = FALSE, level = 0.95, boot_out = NULL, R = 100, seed = NULL, parallel = TRUE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE, save_boot_full = FALSE, save_boot_out = TRUE, mc_ci = FALSE, mc_out = NULL, save_mc_full = FALSE, save_mc_out = TRUE, ci_out = NULL, save_ci_full = FALSE, save_ci_out = TRUE, ci_type = NULL, boot_type = c("perc", "bc"), group = NULL ) cond_effects( wlevels, x, y, m = NULL, fit = NULL, w_type = "auto", w_method = "sd", sd_from_mean = NULL, percentiles = NULL, est = NULL, implied_stats = NULL, boot_ci = FALSE, R = 100, seed = NULL, parallel = TRUE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE, boot_out = NULL, output_type = "data.frame", mod_levels_list_args = list(), mc_ci = FALSE, mc_out = NULL, ci_out = NULL, ci_type = NULL, boot_type = c("perc", "bc"), groups = NULL, ... ) many_indirect_effects(paths, ...)
cond_indirect( x, y, m = NULL, fit = NULL, est = NULL, implied_stats = NULL, wvalues = NULL, standardized_x = FALSE, standardized_y = FALSE, boot_ci = FALSE, level = 0.95, boot_out = NULL, R = 100, seed = NULL, parallel = TRUE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE, save_boot_full = FALSE, prods = NULL, get_prods_only = FALSE, save_boot_out = TRUE, mc_ci = FALSE, mc_out = NULL, save_mc_full = FALSE, save_mc_out = TRUE, ci_out = NULL, save_ci_full = FALSE, save_ci_out = TRUE, ci_type = NULL, group = NULL, boot_type = c("perc", "bc") ) cond_indirect_effects( wlevels, x, y, m = NULL, fit = NULL, w_type = "auto", w_method = "sd", sd_from_mean = NULL, percentiles = NULL, est = NULL, implied_stats = NULL, boot_ci = FALSE, R = 100, seed = NULL, parallel = TRUE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE, boot_out = NULL, output_type = "data.frame", mod_levels_list_args = list(), mc_ci = FALSE, mc_out = NULL, ci_out = NULL, ci_type = NULL, boot_type = c("perc", "bc"), groups = NULL, ... ) indirect_effect( x, y, m = NULL, fit = NULL, est = NULL, implied_stats = NULL, standardized_x = FALSE, standardized_y = FALSE, boot_ci = FALSE, level = 0.95, boot_out = NULL, R = 100, seed = NULL, parallel = TRUE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE, save_boot_full = FALSE, save_boot_out = TRUE, mc_ci = FALSE, mc_out = NULL, save_mc_full = FALSE, save_mc_out = TRUE, ci_out = NULL, save_ci_full = FALSE, save_ci_out = TRUE, ci_type = NULL, boot_type = c("perc", "bc"), group = NULL ) cond_effects( wlevels, x, y, m = NULL, fit = NULL, w_type = "auto", w_method = "sd", sd_from_mean = NULL, percentiles = NULL, est = NULL, implied_stats = NULL, boot_ci = FALSE, R = 100, seed = NULL, parallel = TRUE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE, boot_out = NULL, output_type = "data.frame", mod_levels_list_args = list(), mc_ci = FALSE, mc_out = NULL, ci_out = NULL, ci_type = NULL, boot_type = c("perc", "bc"), groups = NULL, ... ) many_indirect_effects(paths, ...)
x |
Character. The name of the predictor at the start of the path. |
y |
Character. The name of the outcome variable at the end of the path. If the model has only one outcome variable (e.g., moderation only and no mediator), then this argument can be omitted. |
m |
A vector of the variable
names of the mediator(s). The path
goes from the first mediator
successively to the last mediator. If
|
fit |
The fit object. Can be a
lavaan::lavaan object or a
list of |
est |
The output of
|
implied_stats |
Implied means,
variances, and covariances of
observed variables, of the form of
the output of |
wvalues |
A numeric vector of
named elements. The names are the
variable names of the moderators, and
the values are the values to which
the moderators will be set to.
Default is |
standardized_x |
Logical.
Whether |
standardized_y |
Logical.
Whether |
boot_ci |
Logical. Whether
bootstrap confidence interval will be
formed. Default is |
level |
The level of confidence for the bootstrap confidence interval. Default is .95. |
boot_out |
If |
R |
Integer. If |
seed |
If bootstrapping
or Monte Carlo simulation is
conducted, this is the seed for the
bootstrapping or simulation.
Default is |
parallel |
Logical. If
bootstrapping is conducted, whether
parallel processing will be used.
Default is |
ncores |
Integer. The number of
CPU cores to use when |
make_cluster_args |
A named list
of additional arguments to be passed
to |
progress |
Logical. Display
bootstrapping progress or not.
Default is |
save_boot_full |
If |
prods |
The product terms found. For internal use. |
get_prods_only |
IF |
save_boot_out |
If |
mc_ci |
Logical. Whether
Monte Carlo confidence interval will be
formed. Default is |
mc_out |
If |
save_mc_full |
If |
save_mc_out |
If |
ci_out |
If |
save_ci_full |
If |
save_ci_out |
If either |
ci_type |
The type of
confidence intervals to be formed.
Can be either |
group |
Either the group number
as appeared in the |
boot_type |
If bootstrap
confidence interval is to be formed,
the type of bootstrap confidence
interval. The supported types
are |
wlevels |
The output of
|
w_type |
Character. Whether the
moderator is a |
w_method |
Character, either
|
sd_from_mean |
A numeric vector.
Specify the distance in standard
deviation from the mean for each
level. Default is |
percentiles |
A numeric vector.
Specify the percentile (in
proportion) for each level. Default
is |
output_type |
The type of output
of |
mod_levels_list_args |
Additional arguments to be passed to
|
groups |
Either a vector of
group numbers
as appeared in the |
... |
For |
paths |
The output of |
For a model with a mediation path
moderated by one or more moderators,
cond_indirect_effects()
can be used
to compute the conditional indirect
effect from one variable to another
variable, at one or more set of
selected value(s) of the
moderator(s).
If only the effect for one set of
value(s) of the moderator(s) is
needed, cond_indirect()
can be
used.
If only the mediator(s) is/are
specified (m
) and no values of
moderator(s) are specified, then the
indirect effect from one variable
(x
) to another variable (y
) is
computed. A convenient wrapper
indirect_effect()
can be used to
compute the indirect effect.
If only the value(s) of moderator(s)
is/are specified (wvalues
or
wlevels
) and no mediators (m
) are
specified when calling
cond_indirect_effects()
or
cond_indirect()
, then the
conditional direct effects from one
variable to another are computed.
All three functions support using
nonparametric bootstrapping (for
lavaan
or lm
outputs) or
Monte Carlo simulation (for
lavaan
outputs only) to form
confidence intervals.
Bootstrapping or Monte Carlo
simulation only needs to be done
once. These are the possible ways to
form bootstrapping:
Do bootstrapping or Monte Carlo
simulation in the first call
to one of these functions, by setting
boot_ci
or mc_ci
to TRUE
and
R
to the
number of bootstrap samples or
replications, level
to the level of confidence (default
.95 or 95%), and seed
to reproduce
the results (parallel
and ncores
are optional for bootstrapping).
This will take some
time to run for bootstrapping. The
output will have all
bootstrap or Monte Carlo estimates
stored. This
output, whether it is from
indirect_effect()
,
cond_indirect_effects()
, or
cond_indirect()
, can be reused by
any of these three functions by
setting boot_out
(for bootstrapping)
or mc_out
(for Monte Carlo
simulation) to this output.
They will form the confidence
intervals using the stored bootstrap
or Monte Carlo
estimates.
Do bootstrapping using
do_boot()
or Monte Carlo simulation
us8ing do_mc()
. The output can be used
in the boot_out
(for bootstrapping)
or mc_out
(for Monte Carlo simulation)
argument of
indirect_effect()
,
cond_indirect_effects()
and
cond_indirect()
.
For bootstrapping,
if lavaan::sem()
is used to fit
a model and se = "boot"
is used,
do_boot()
can extract them to
generate a boot_out
-class object
that again can be used in the
boot_out
argument.
If boot_out
or mc_out
is set, arguments such
as R
, seed
, and parallel
will
be ignored.
Since Version 0.1.14.2, support for
multigroup models has been added for models
fitted by lavaan
. Both bootstrapping
and Monte Carlo confidence intervals
are supported. When used on
a multigroup model:
For cond_indirect()
and
indirect_effect()
, users need to
specify the group
argument
(by number or label). When using
cond_indirect_effects()
, if
group
is not set, all groups wil
be used and the indirect effect
in each group will be computed,
kind of treating group as a moderator.
For many_indirect_effects()
,
the paths can be generated from a
multigroup models.
Currently, cond_indirect_effects()
does not support a multigroup model
with moderators on the path selected.
The function cond_indirect()
does
not have this limitation but users
need to manually specify the desired
value of the moderator(s).
many_indirect_effects()
If bootstrapping or Monte Carlo
confidence intervals are requested,
it is advised to use do_boot()
first to simulate the estimates.
Nevertheless, In Version 0.1.14.9
and later versions, if boot_ci
or mc_ci
is TRUE
when calling
many_indirect_effects()
but
boot_out
or mc_out
is not set,
bootstrapping or simulation will
be done only once, and then the
bootstrapping or simulated estimates
will be used for all paths. This
prevents accidentally repeating the
process once for each direct path.
indirect_effect()
and
cond_indirect()
return an
indirect
-class object.
cond_indirect_effects()
returns a
cond_indirect_effects
-class object.
These two classes of objects have
their own print methods for printing
the results (see print.indirect()
and print.cond_indirect_effects()
).
They also have a coef
method for
extracting the estimates
(coef.indirect()
and
coef.cond_indirect_effects()
) and a
confint
method for extracting the
confidence intervals
(confint.indirect()
and
confint.cond_indirect_effects()
).
Addition and subtraction can also be
conducted on indirect
-class object
to estimate and test a function of
effects (see math_indirect)
cond_indirect()
: Compute
conditional, indirect, or conditional
indirect effects for one set of
levels.
cond_indirect_effects()
: Compute the
conditional effects or conditional
indirect effects for several sets of
levels of the moderator(s).
indirect_effect()
: Compute the
indirect effect. A wrapper of
cond_indirect()
. Can be used when
there is no moderator.
cond_effects()
: Just
an alias to cond_indirect_effects()
,
a better name when a path has no
moderator.
many_indirect_effects()
: Compute the
indirect effects along more than one paths.
It call indirect_effect()
once for
each of the path.
mod_levels()
and
merge_mod_levels()
for generating
levels of moderators. do_boot for
doing bootstrapping before calling
these functions.
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ a1 * x + d1 * w1 + e1 * x:w1 m2 ~ a2 * x y ~ b1 * m1 + b2 * m2 + cp * x " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) hi_w1 <- mean(dat$w1) + sd(dat$w1) # Examples for cond_indirect(): # Conditional effect from x to m1 when w1 is 1 SD above mean cond_indirect(x = "x", y = "m1", wvalues = c(w1 = hi_w1), fit = fit) # Direct effect from x to y (direct because no 'm' variables) indirect_effect(x = "x", y = "y", fit = fit) # Conditional Indirect effect from x1 through m1 to y, when w1 is 1 SD above mean cond_indirect(x = "x", y = "y", m = "m1", wvalues = c(w1 = hi_w1), fit = fit) # Examples for cond_indirect_effects(): # Create levels of w1, the moderators w1levels <- mod_levels("w1", fit = fit) w1levels # Conditional effects from x to m1 when w1 is equal to each of the levels cond_indirect_effects(x = "x", y = "m1", wlevels = w1levels, fit = fit) # Conditional Indirect effect from x1 through m1 to y, # when w1 is equal to each of the levels cond_indirect_effects(x = "x", y = "y", m = "m1", wlevels = w1levels, fit = fit) # Multigroup models for cond_indirect_effects() dat <- data_med_mg mod <- " m ~ x + c1 + c2 y ~ m + x + c1 + c2 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE, group = "group") # If a model has more than one group, # it will be used as a 'moderator'. cond_indirect_effects(x = "x", y = "y", m = "m", fit = fit) # Multigroup model for indirect_effect() dat <- data_med_mg mod <- " m ~ x + c1 + c2 y ~ m + x + c1 + c2 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE, group = "group") # If a model has more than one group, # the argument 'group' must be set. ind1 <- indirect_effect(x = "x", y = "y", m = "m", fit = fit, group = "Group A") ind1 ind2 <- indirect_effect(x = "x", y = "y", m = "m", fit = fit, group = 2) ind2 # Examples for many_indirect_effects(): library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths from x to y paths <- all_indirect_paths(fit, x = "x", y = "y") paths # Indirect effect estimates out <- many_indirect_effects(paths, fit = fit) out # Multigroup models for many_indirect_effects() data(data_med_complicated_mg) mod <- " m11 ~ x1 + x2 + c1 + c2 m12 ~ m11 + c1 + c2 m2 ~ x1 + x2 + c1 + c2 y1 ~ m11 + m12 + x1 + x2 + c1 + c2 y2 ~ m2 + x1 + x2 + c1 + c2 " fit <- sem(mod, data_med_complicated_mg, group = "group") summary(fit) paths <- all_indirect_paths(fit, x = "x1", y = "y1") paths # Indirect effect estimates for all paths in all groups out <- many_indirect_effects(paths, fit = fit) out
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ a1 * x + d1 * w1 + e1 * x:w1 m2 ~ a2 * x y ~ b1 * m1 + b2 * m2 + cp * x " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) hi_w1 <- mean(dat$w1) + sd(dat$w1) # Examples for cond_indirect(): # Conditional effect from x to m1 when w1 is 1 SD above mean cond_indirect(x = "x", y = "m1", wvalues = c(w1 = hi_w1), fit = fit) # Direct effect from x to y (direct because no 'm' variables) indirect_effect(x = "x", y = "y", fit = fit) # Conditional Indirect effect from x1 through m1 to y, when w1 is 1 SD above mean cond_indirect(x = "x", y = "y", m = "m1", wvalues = c(w1 = hi_w1), fit = fit) # Examples for cond_indirect_effects(): # Create levels of w1, the moderators w1levels <- mod_levels("w1", fit = fit) w1levels # Conditional effects from x to m1 when w1 is equal to each of the levels cond_indirect_effects(x = "x", y = "m1", wlevels = w1levels, fit = fit) # Conditional Indirect effect from x1 through m1 to y, # when w1 is equal to each of the levels cond_indirect_effects(x = "x", y = "y", m = "m1", wlevels = w1levels, fit = fit) # Multigroup models for cond_indirect_effects() dat <- data_med_mg mod <- " m ~ x + c1 + c2 y ~ m + x + c1 + c2 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE, group = "group") # If a model has more than one group, # it will be used as a 'moderator'. cond_indirect_effects(x = "x", y = "y", m = "m", fit = fit) # Multigroup model for indirect_effect() dat <- data_med_mg mod <- " m ~ x + c1 + c2 y ~ m + x + c1 + c2 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE, group = "group") # If a model has more than one group, # the argument 'group' must be set. ind1 <- indirect_effect(x = "x", y = "y", m = "m", fit = fit, group = "Group A") ind1 ind2 <- indirect_effect(x = "x", y = "y", m = "m", fit = fit, group = 2) ind2 # Examples for many_indirect_effects(): library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths from x to y paths <- all_indirect_paths(fit, x = "x", y = "y") paths # Indirect effect estimates out <- many_indirect_effects(paths, fit = fit) out # Multigroup models for many_indirect_effects() data(data_med_complicated_mg) mod <- " m11 ~ x1 + x2 + c1 + c2 m12 ~ m11 + c1 + c2 m2 ~ x1 + x2 + c1 + c2 y1 ~ m11 + m12 + x1 + x2 + c1 + c2 y2 ~ m2 + x1 + x2 + c1 + c2 " fit <- sem(mod, data_med_complicated_mg, group = "group") summary(fit) paths <- all_indirect_paths(fit, x = "x1", y = "y1") paths # Indirect effect estimates for all paths in all groups out <- many_indirect_effects(paths, fit = fit) out
Compute the difference in conditional indirect effects between two sets of levels of the moderators.
cond_indirect_diff(output, from = NULL, to = NULL, level = 0.95)
cond_indirect_diff(output, from = NULL, to = NULL, level = 0.95)
output |
A
|
from |
A row number of |
to |
A row number of |
level |
The level of confidence for the confidence interval. Default is .95. |
Ths function takes the output of
cond_indirect_effects()
and
computes the difference in
conditional indirect effects between
any two rows, that is, between levels
of the moderator, or two sets of
levels of the moderators when the
path has more than one moderator.
The difference is meaningful when the difference between the two levels or sets of levels are meaningful. For example, if the two levels are the mean of the moderator and one standard deviation above mean of the moderator, then this difference is the change in indirect effect when the moderator increases by one standard deviation.
If the two levels are 0 and 1, then
this difference is the index of
moderated mediation as proposed by
Hayes (2015). (This index can also be
computed directly by
index_of_mome()
, designed
specifically for this purpose.)
The function can also compute the change in the standardized indirect effect between two levels of a moderator or two sets of levels of the moderators.
This function is intended to be a general purpose function that allows users to compute the difference between any two levels or sets of levels that are meaningful in a context.
This function itself does not set the
levels of comparison. The levels to
be compared need to be set when
calling cond_indirect_effects()
.
This function extracts required
information from the output of
cond_indirect_effects()
.
If bootstrap or Monte Carlo
estimates are available
in the input or bootstrap
or Monte Carlo confidence
intervals are requested in calling
cond_indirect_effects()
,
cond_indirect_diff()
will also form
the bootstrap confidence
interval for the difference in
conditional indirect effects
using the stored estimates.
If bootstrap confidence interval is to be formed and both effects used the same type of interval, then that type will be used. Otherwise, percentile confidence interval will be formed.
A cond_indirect_diff
-class
object. This class has a print
method
(print.cond_indirect_diff()
), a
coef
method
(coef.cond_indirect_diff()
), and a
confint
method
(confint.cond_indirect_diff()
).
cond_indirect_diff()
: Compute the difference in in
conditional indirect effect between
two rows in the output of
cond_indirect_effects()
.
Hayes, A. F. (2015). An index and test of linear moderated mediation. Multivariate Behavioral Research, 50(1), 1-22. doi:10.1080/00273171.2014.962683
index_of_mome()
for
computing the index of moderated
mediation, index_of_momome()
for
computing the index of moderated
moderated mediation,
cond_indirect_effects()
,
mod_levels()
, and
merge_mod_levels()
for preparing
the levels to be compared.
library(lavaan) dat <- modmed_x1m3w4y1 dat$xw1 <- dat$x * dat$w1 mod <- " m1 ~ a * x + f * w1 + d * xw1 y ~ b * m1 + cp * x " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # Create levels of w1, the moderators w1levels <- mod_levels("w1", fit = fit) w1levels # Conditional effects from x to y when w1 is equal to each of the levels boot_out <- fit2boot_out_do_boot(fit, R = 40, seed = 4314, progress = FALSE) out <- cond_indirect_effects(x = "x", y = "y", m = "m1", wlevels = w1levels, fit = fit, boot_ci = TRUE, boot_out = boot_out) out out_ind <- cond_indirect_diff(out, from = 2, to = 1) out_ind coef(out_ind) confint(out_ind)
library(lavaan) dat <- modmed_x1m3w4y1 dat$xw1 <- dat$x * dat$w1 mod <- " m1 ~ a * x + f * w1 + d * xw1 y ~ b * m1 + cp * x " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # Create levels of w1, the moderators w1levels <- mod_levels("w1", fit = fit) w1levels # Conditional effects from x to y when w1 is equal to each of the levels boot_out <- fit2boot_out_do_boot(fit, R = 40, seed = 4314, progress = FALSE) out <- cond_indirect_effects(x = "x", y = "y", m = "m1", wlevels = w1levels, fit = fit, boot_ci = TRUE, boot_out = boot_out) out out_ind <- cond_indirect_diff(out, from = 2, to = 1) out_ind coef(out_ind) confint(out_ind)
Extract the confidence
interval the output of
cond_indirect_diff()
.
## S3 method for class 'cond_indirect_diff' confint(object, parm, level = 0.95, ...)
## S3 method for class 'cond_indirect_diff' confint(object, parm, level = 0.95, ...)
object |
The output of
|
parm |
Ignored. |
level |
The level of confidence for the confidence interval. Default is .95. Must match the level of the stored confidence interval. |
... |
Optional arguments. Ignored. |
The confint
method of the
cond_indirect_diff
-class object.
The type of confidence intervals depends on the call used to create the object. This function merely extracts the stored confidence intervals.
A one-row-two-column data
frame of the confidence limits. If
confidence interval is not available,
the limits are NA
s.
Return the confidence
intervals of the conditional indirect
effects or conditional effects in the
output of cond_indirect_effects()
.
## S3 method for class 'cond_indirect_effects' confint(object, parm, level = 0.95, ...)
## S3 method for class 'cond_indirect_effects' confint(object, parm, level = 0.95, ...)
object |
The output of
|
parm |
Ignored. Always returns the confidence intervals of the effects for all levels stored. |
level |
The level of confidence, default is .95, returning the 95% confidence interval. Ignored for now and will use the level of the stored intervals. |
... |
Additional arguments. Ignored by the function. |
It extracts and returns the columns for confidence intervals, if available.
The type of confidence intervals depends on the call used to compute the effects. If confidence intervals have already been formed (e.g., by bootstrapping or Monte Carlo), then this function merely retrieves the confidence intervals stored.
If the following conditions are met, the stored standard errors, if available, will be used test an effect and form it confidence interval:
Confidence intervals have not been formed (e.g., by bootstrapping or Monte Carlo).
The path has no mediators.
The model has only one group.
The path is moderated by one or more moderator.
Both the x
-variable and the
y
-variable are not standardized.
If the model is fitted by OLS
regression (e.g., using stats::lm()
),
then the variance-covariance matrix
of the coefficient estimates will be
used, and confidence
intervals are computed from the t
statistic.
If the model is fitted by structural
equation modeling using lavaan
, then
the variance-covariance computed by
lavaan
will be used,
and confidence intervals are computed
from the z statistic.
If the model is fitted by structural equation modeling and has moderators, the standard errors, p-values, and confidence interval computed from the variance-covariance matrices for conditional effects can only be trusted if all covariances involving the product terms are free. If any of them are fixed, for example, fixed to zero, it is possible that the model is not invariant to linear transformation of the variables.
A data frame with two
columns, one for each confidence
limit of the confidence intervals.
The number of rows is equal to the
number of rows of object
.
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ x + w1 + x:w1 m2 ~ m1 y ~ m2 + x + w4 + m2:w4 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # Examples for cond_indirect(): # Create levels of w1 and w4 w1levels <- mod_levels("w1", fit = fit) w1levels w4levels <- mod_levels("w4", fit = fit) w4levels w1w4levels <- merge_mod_levels(w1levels, w4levels) # Conditional effects from x to m1 when w1 is equal to each of the levels # R should be at least 2000 or 5000 in real research. out1 <- suppressWarnings(cond_indirect_effects(x = "x", y = "m1", wlevels = w1levels, fit = fit, boot_ci = TRUE, R = 20, seed = 54151, parallel = FALSE, progress = FALSE)) confint(out1)
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ x + w1 + x:w1 m2 ~ m1 y ~ m2 + x + w4 + m2:w4 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # Examples for cond_indirect(): # Create levels of w1 and w4 w1levels <- mod_levels("w1", fit = fit) w1levels w4levels <- mod_levels("w4", fit = fit) w4levels w1w4levels <- merge_mod_levels(w1levels, w4levels) # Conditional effects from x to m1 when w1 is equal to each of the levels # R should be at least 2000 or 5000 in real research. out1 <- suppressWarnings(cond_indirect_effects(x = "x", y = "m1", wlevels = w1levels, fit = fit, boot_ci = TRUE, R = 20, seed = 54151, parallel = FALSE, progress = FALSE)) confint(out1)
Return the confidence
interval of the Delta_Med in the
output of delta_med()
.
## S3 method for class 'delta_med' confint(object, parm, level = NULL, boot_type, ...)
## S3 method for class 'delta_med' confint(object, parm, level = NULL, boot_type, ...)
object |
The output of
|
parm |
Not used because only one parameter, the Delta_Med, is allowed. |
level |
The level of confidence,
default is |
boot_type |
If bootstrap
confidence interval is to be formed,
the type of bootstrap confidence
interval. The supported types
are |
... |
Optional arguments. Ignored. |
It returns the nonparametric
bootstrap
percentile confidence interval of
Delta_Med, proposed byLiu, Yuan, and
Li (2023). The object must be the
output of delta_med()
, with
bootstrap confidence interval
requested when calling delta_med()
.
However, the level of confidence
can be different from that used when
call delta_med()
.
A one-row matrix of the confidence
interval. All values are NA
if
bootstrap confidence interval was
not requested when calling
delta_med()
.
Shu Fai Cheung https://orcid.org/0000-0002-9871-9448
library(lavaan) dat <- data_med mod <- " m ~ x y ~ m + x " fit <- sem(mod, dat) # Call do_boot() to generate # bootstrap estimates # Use 2000 or even 5000 for R in real studies # Set parallel to TRUE in real studies for faster bootstrapping boot_out <- do_boot(fit, R = 45, seed = 879, parallel = FALSE, progress = FALSE) # Remove 'progress = FALSE' in practice dm_boot <- delta_med(x = "x", y = "y", m = "m", fit = fit, boot_out = boot_out, progress = FALSE) dm_boot confint(dm_boot)
library(lavaan) dat <- data_med mod <- " m ~ x y ~ m + x " fit <- sem(mod, dat) # Call do_boot() to generate # bootstrap estimates # Use 2000 or even 5000 for R in real studies # Set parallel to TRUE in real studies for faster bootstrapping boot_out <- do_boot(fit, R = 45, seed = 879, parallel = FALSE, progress = FALSE) # Remove 'progress = FALSE' in practice dm_boot <- delta_med(x = "x", y = "y", m = "m", fit = fit, boot_out = boot_out, progress = FALSE) dm_boot confint(dm_boot)
Return the
confidence interval of the indirect
effect or conditional indirect effect
stored in the output of
indirect_effect()
or
cond_indirect()
.
## S3 method for class 'indirect' confint(object, parm, level = 0.95, boot_type, ...)
## S3 method for class 'indirect' confint(object, parm, level = 0.95, boot_type, ...)
object |
The output of
|
parm |
Ignored because the stored object always has only one parameter. |
level |
The level of confidence, default is .95, returning the 95% confidence interval. |
boot_type |
If bootstrap
confidence interval is to be formed,
the type of bootstrap confidence
interval. The supported types
are |
... |
Additional arguments. Ignored by the function. |
It extracts and returns the stored confidence interval if available.
The type of confidence interval depends on the call used to compute the effect. This function merely retrieves the stored estimates, which could be generated by nonparametric bootstrapping, Monte Carlo simulation, or other methods to be supported in the future, and uses them to form the percentile confidence interval.
If the following conditions are met, the stored standard errors, if available, will be used test an effect and form it confidence interval:
Confidence intervals have not been formed (e.g., by bootstrapping or Monte Carlo).
The path has no mediators.
The model has only one group.
The path is moderated by one or more moderator.
Both the x
-variable and the
y
-variable are not standardized.
If the model is fitted by OLS
regression (e.g., using stats::lm()
),
then the variance-covariance matrix
of the coefficient estimates will be
used, and confidence
intervals are computed from the t
statistic.
If the model is fitted by structural
equation modeling using lavaan
, then
the variance-covariance computed by
lavaan
will be used,
and confidence intervals are computed
from the z statistic.
If the model is fitted by structural equation modeling and has moderators, the standard errors, p-values, and confidence interval computed from the variance-covariance matrices for conditional effects can only be trusted if all covariances involving the product terms are free. If any of them are fixed, for example, fixed to zero, it is possible that the model is not invariant to linear transformation of the variables.
A numeric vector of two elements, the limits of the confidence interval.
indirect_effect()
and
cond_indirect()
dat <- modmed_x1m3w4y1 # Indirect Effect library(lavaan) mod1 <- " m1 ~ x m2 ~ m1 y ~ m2 + x " fit <- sem(mod1, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) # R should be at least 2000 or 5000 in real research. out1 <- indirect_effect(x = "x", y = "y", m = c("m1", "m2"), fit = fit, boot_ci = TRUE, R = 45, seed = 54151, parallel = FALSE, progress = FALSE) out1 confint(out1)
dat <- modmed_x1m3w4y1 # Indirect Effect library(lavaan) mod1 <- " m1 ~ x m2 ~ m1 y ~ m2 + x " fit <- sem(mod1, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) # R should be at least 2000 or 5000 in real research. out1 <- indirect_effect(x = "x", y = "y", m = c("m1", "m2"), fit = fit, boot_ci = TRUE, R = 45, seed = 54151, parallel = FALSE, progress = FALSE) out1 confint(out1)
Return the
confidence intervals of the indirect
effects
stored in the output of
many_indirect_effects()
.
## S3 method for class 'indirect_list' confint(object, parm = NULL, level = 0.95, ...)
## S3 method for class 'indirect_list' confint(object, parm = NULL, level = 0.95, ...)
object |
The output of
|
parm |
Ignored for now. |
level |
The level of confidence, default is .95, returning the 95% confidence interval. |
... |
Additional arguments. Ignored by the function. |
It extracts and returns the stored confidence interval if available.
The type of confidence intervals depends on the call used to compute the effects. This function merely retrieves the stored estimates, which could be generated by nonparametric bootstrapping, Monte Carlo simulation, or other methods to be supported in the future, and uses them to form the percentile confidence interval.
A two-column data frame. The columns are the limits of the confidence intervals.
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths from x to y paths <- all_indirect_paths(fit, x = "x", y = "y") paths # Indirect effect estimates # R should be 2000 or even 5000 in real research # parallel should be used in real research. fit_boot <- do_boot(fit, R = 45, seed = 8974, parallel = FALSE, progress = FALSE) out <- many_indirect_effects(paths, fit = fit, boot_ci = TRUE, boot_out = fit_boot) out confint(out)
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths from x to y paths <- all_indirect_paths(fit, x = "x", y = "y") paths # Indirect effect estimates # R should be 2000 or even 5000 in real research # parallel should be used in real research. fit_boot <- do_boot(fit, R = 45, seed = 8974, parallel = FALSE, progress = FALSE) out <- many_indirect_effects(paths, fit = fit, boot_ci = TRUE, boot_out = fit_boot) out confint(out)
A simple mediation model.
data_med
data_med
A data frame with 100 rows and 5 variables:
Predictor. Numeric.
Mediator. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
library(lavaan) data(data_med) mod <- " m ~ a * x + c1 + c2 y ~ b * m + x + c1 + c2 ab := a * b " fit <- sem(mod, data_med, fixed.x = FALSE) parameterEstimates(fit)
library(lavaan) data(data_med) mod <- " m ~ a * x + c1 + c2 y ~ b * m + x + c1 + c2 ab := a * b " fit <- sem(mod, data_med, fixed.x = FALSE) parameterEstimates(fit)
A mediation model with two predictors, two pathways,
data_med_complicated
data_med_complicated
A data frame with 300 rows and 5 variables:
Predictor 1. Numeric.
Predictor 2. Numeric.
Mediator 1 in Path 1. Numeric.
Mediator 2 in Path 1. Numeric.
Mediator in Path 2. Numeric.
Outcome variable 1. Numeric.
Outcome variable 2. Numeric.
Control variable. Numeric.
Control variable. Numeric.
data(data_med_complicated) dat <- data_med_complicated summary(lm_m11 <- lm(m11 ~ x1 + x1 + x2 + c1 + c2, dat)) summary(lm_m12 <- lm(m12 ~ m11 + x1 + x2 + c1 + c2, dat)) summary(lm_m2 <- lm(m2 ~ x1 + x2 + c1 + c2, dat)) summary(lm_y1 <- lm(y1 ~ m11 + m12 + m2 + x1 + x2 + c1 + c2, dat)) summary(lm_y2 <- lm(y2 ~ m11 + m12 + m2 + x1 + x2 + c1 + c2, dat))
data(data_med_complicated) dat <- data_med_complicated summary(lm_m11 <- lm(m11 ~ x1 + x1 + x2 + c1 + c2, dat)) summary(lm_m12 <- lm(m12 ~ m11 + x1 + x2 + c1 + c2, dat)) summary(lm_m2 <- lm(m2 ~ x1 + x2 + c1 + c2, dat)) summary(lm_y1 <- lm(y1 ~ m11 + m12 + m2 + x1 + x2 + c1 + c2, dat)) summary(lm_y2 <- lm(y2 ~ m11 + m12 + m2 + x1 + x2 + c1 + c2, dat))
A mediation model with two predictors, two pathways, and two groups.
data_med_complicated_mg
data_med_complicated_mg
A data frame with 300 rows and 5 variables:
Predictor 1. Numeric.
Predictor 2. Numeric.
Mediator 1 in Path 1. Numeric.
Mediator 2 in Path 1. Numeric.
Mediator in Path 2. Numeric.
Outcome variable 1. Numeric.
Outcome variable 2. Numeric.
Control variable. Numeric.
Control variable. Numeric.
Group variable. Character. 'Group A' or 'Group B'
library(lavaan) data(data_med_complicated_mg) dat <- data_med_complicated_mg mod <- " m11 ~ x1 + x2 + c1 + c2 m12 ~ m11 + c1 + c2 m2 ~ x1 + x2 + c1 + c2 y1 ~ m11 + m12 + x1 + x2 + c1 + c2 y2 ~ m2 + x1 + x2 + c1 + c2 " fit <- sem(mod, dat, group = "group") summary(fit)
library(lavaan) data(data_med_complicated_mg) dat <- data_med_complicated_mg mod <- " m11 ~ x1 + x2 + c1 + c2 m12 ~ m11 + c1 + c2 m2 ~ x1 + x2 + c1 + c2 y1 ~ m11 + m12 + x1 + x2 + c1 + c2 y2 ~ m2 + x1 + x2 + c1 + c2 " fit <- sem(mod, dat, group = "group") summary(fit)
A simple mediation model with two groups.
data_med_mg
data_med_mg
A data frame with 100 rows and 5 variables:
Predictor. Numeric.
Mediator. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
Group variable. Character. "Group A" or "Group B"
library(lavaan) data(data_med_mg) mod <- " m ~ c(a1, a2) * x + c1 + c2 y ~ c(b1, b2) * m + x + c1 + c2 a1b1 := a1 * b1 a2b2 := a2 * b2 abdiff := a2b2 - a1b1 " fit <- sem(mod, data_med_mg, fixed.x = FALSE, group = "group") parameterEstimates(fit)
library(lavaan) data(data_med_mg) mod <- " m ~ c(a1, a2) * x + c1 + c2 y ~ c(b1, b2) * m + x + c1 + c2 a1b1 := a1 * b1 a2b2 := a2 * b2 abdiff := a2b2 - a1b1 " fit <- sem(mod, data_med_mg, fixed.x = FALSE, group = "group") parameterEstimates(fit)
A simple mediation model with a-path moderated.
data_med_mod_a
data_med_mod_a
A data frame with 100 rows and 6 variables:
Predictor. Numeric.
Moderator. Numeric.
Mediator. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
library(lavaan) data(data_med_mod_a) data_med_mod_a$xw <- data_med_mod_a$x * data_med_mod_a$w mod <- " m ~ a * x + w + d * xw + c1 + c2 y ~ b * m + x + w + c1 + c2 w ~~ v_w * w w ~ m_w * 1 ab := a * b ab_lo := (a + d * (m_w - sqrt(v_w))) * b ab_hi := (a + d * (m_w + sqrt(v_w))) * b " fit <- sem(mod, data_med_mod_a, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 3, 6, 11, 12, 31:33), ]
library(lavaan) data(data_med_mod_a) data_med_mod_a$xw <- data_med_mod_a$x * data_med_mod_a$w mod <- " m ~ a * x + w + d * xw + c1 + c2 y ~ b * m + x + w + c1 + c2 w ~~ v_w * w w ~ m_w * 1 ab := a * b ab_lo := (a + d * (m_w - sqrt(v_w))) * b ab_hi := (a + d * (m_w + sqrt(v_w))) * b " fit <- sem(mod, data_med_mod_a, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 3, 6, 11, 12, 31:33), ]
A simple mediation model with a-path and b-path each moderated by a moderator.
data_med_mod_ab
data_med_mod_ab
A data frame with 100 rows and 7 variables:
Predictor. Numeric.
Moderator 1. Numeric.
Moderator 2. Numeric.
Mediator. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
library(lavaan) data(data_med_mod_ab) data_med_mod_ab$xw1 <- data_med_mod_ab$x * data_med_mod_ab$w1 data_med_mod_ab$mw2 <- data_med_mod_ab$m * data_med_mod_ab$w2 mod <- " m ~ a * x + w1 + d1 * xw1 + c1 + c2 y ~ b * m + x + w1 + w2 + d2 * mw2 + c1 + c2 w1 ~~ v_w1 * w1 w1 ~ m_w1 * 1 w2 ~~ v_w2 * w2 w2 ~ m_w2 * 1 ab := a * b ab_lolo := (a + d1 * (m_w1 - sqrt(v_w1))) * (b + d2 * (m_w2 - sqrt(v_w2))) ab_lohi := (a + d1 * (m_w1 - sqrt(v_w1))) * (b + d2 * (m_w2 + sqrt(v_w2))) ab_hilo := (a + d1 * (m_w1 + sqrt(v_w1))) * (b + d2 * (m_w2 - sqrt(v_w2))) ab_hihi := (a + d1 * (m_w1 + sqrt(v_w1))) * (b + d2 * (m_w2 + sqrt(v_w2))) " fit <- sem(mod, data_med_mod_ab, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 3, 6, 10, 41:45), ]
library(lavaan) data(data_med_mod_ab) data_med_mod_ab$xw1 <- data_med_mod_ab$x * data_med_mod_ab$w1 data_med_mod_ab$mw2 <- data_med_mod_ab$m * data_med_mod_ab$w2 mod <- " m ~ a * x + w1 + d1 * xw1 + c1 + c2 y ~ b * m + x + w1 + w2 + d2 * mw2 + c1 + c2 w1 ~~ v_w1 * w1 w1 ~ m_w1 * 1 w2 ~~ v_w2 * w2 w2 ~ m_w2 * 1 ab := a * b ab_lolo := (a + d1 * (m_w1 - sqrt(v_w1))) * (b + d2 * (m_w2 - sqrt(v_w2))) ab_lohi := (a + d1 * (m_w1 - sqrt(v_w1))) * (b + d2 * (m_w2 + sqrt(v_w2))) ab_hilo := (a + d1 * (m_w1 + sqrt(v_w1))) * (b + d2 * (m_w2 - sqrt(v_w2))) ab_hihi := (a + d1 * (m_w1 + sqrt(v_w1))) * (b + d2 * (m_w2 + sqrt(v_w2))) " fit <- sem(mod, data_med_mod_ab, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 3, 6, 10, 41:45), ]
A simple mediation model with a-path and b-path moderated by one moderator.
data_med_mod_ab1
data_med_mod_ab1
A data frame with 100 rows and 6 variables:
Predictor. Numeric.
Moderator. Numeric.
Mediator. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
library(lavaan) data(data_med_mod_ab1) data_med_mod_ab1$xw <- data_med_mod_ab1$x * data_med_mod_ab1$w data_med_mod_ab1$mw <- data_med_mod_ab1$m * data_med_mod_ab1$w mod <- " m ~ a * x + w + da * xw + c1 + c2 y ~ b * m + x + w + db * mw + c1 + c2 w ~~ v_w * w w ~ m_w * 1 ab := a * b ab_lo := (a + da * (m_w - sqrt(v_w))) * (b + db * (m_w - sqrt(v_w))) ab_hi := (a + da * (m_w + sqrt(v_w))) * (b + db * (m_w + sqrt(v_w))) " fit <- sem(mod, data_med_mod_ab1, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 3, 6, 9, 38:40), ]
library(lavaan) data(data_med_mod_ab1) data_med_mod_ab1$xw <- data_med_mod_ab1$x * data_med_mod_ab1$w data_med_mod_ab1$mw <- data_med_mod_ab1$m * data_med_mod_ab1$w mod <- " m ~ a * x + w + da * xw + c1 + c2 y ~ b * m + x + w + db * mw + c1 + c2 w ~~ v_w * w w ~ m_w * 1 ab := a * b ab_lo := (a + da * (m_w - sqrt(v_w))) * (b + db * (m_w - sqrt(v_w))) ab_hi := (a + da * (m_w + sqrt(v_w))) * (b + db * (m_w + sqrt(v_w))) " fit <- sem(mod, data_med_mod_ab1, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 3, 6, 9, 38:40), ]
A simple mediation model with b-path moderated.
data_med_mod_b
data_med_mod_b
A data frame with 100 rows and 6 variables:
Predictor. Numeric.
Moderator. Numeric.
Mediator. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
library(lavaan) data(data_med_mod_b) data_med_mod_b$mw <- data_med_mod_b$m * data_med_mod_b$w mod <- " m ~ a * x + w + c1 + c2 y ~ b * m + x + d * mw + c1 + c2 w ~~ v_w * w w ~ m_w * 1 ab := a * b ab_lo := a * (b + d * (m_w - sqrt(v_w))) ab_hi := a * (b + d * (m_w + sqrt(v_w))) " fit <- sem(mod, data_med_mod_b, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 5, 7, 10, 11, 30:32), ]
library(lavaan) data(data_med_mod_b) data_med_mod_b$mw <- data_med_mod_b$m * data_med_mod_b$w mod <- " m ~ a * x + w + c1 + c2 y ~ b * m + x + d * mw + c1 + c2 w ~~ v_w * w w ~ m_w * 1 ab := a * b ab_lo := a * (b + d * (m_w - sqrt(v_w))) ab_hi := a * (b + d * (m_w + sqrt(v_w))) " fit <- sem(mod, data_med_mod_b, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 5, 7, 10, 11, 30:32), ]
A simple mediation model with moderated-mediation on the b-path.
data_med_mod_b_mod
data_med_mod_b_mod
A data frame with 100 rows and 5 variables:
Predictor. Numeric.
Moderator on b-path. Numeric.
Moderator on the moderating effect of w1. Numeric.
Mediator. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
data(data_med_mod_b_mod) dat <- data_med_mod_b_mod summary(lm_m <- lm(m ~ x + c1 + c2, dat)) summary(lm_y <- lm(y ~ m*w1*w2 + x + c1 + c2, dat))
data(data_med_mod_b_mod) dat <- data_med_mod_b_mod summary(lm_m <- lm(m ~ x + c1 + c2, dat)) summary(lm_y <- lm(y ~ m*w1*w2 + x + c1 + c2, dat))
A parallel mediation model with a1-path and b2-path moderated.
data_med_mod_parallel
data_med_mod_parallel
A data frame with 100 rows and 8 variables:
Predictor. Numeric.
Moderator 1. Numeric.
Moderator 2. Numeric.
Mediator 1. Numeric.
Mediator 2. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
library(lavaan) data(data_med_mod_parallel) data_med_mod_parallel$xw1 <- data_med_mod_parallel$x * data_med_mod_parallel$w1 data_med_mod_parallel$m2w2 <- data_med_mod_parallel$m2 * data_med_mod_parallel$w2 mod <- " m1 ~ a1 * x + w1 + da1 * xw1 + c1 + c2 m2 ~ a2 * x + w1 + c1 + c2 y ~ b1 * m1 + b2 * m2 + x + w1 + w2 + db2 * m2w2 + c1 + c2 w1 ~~ v_w1 * w1 w1 ~ m_w1 * 1 w2 ~~ v_w2 * w2 w2 ~ m_w2 * 1 a1b1 := a1 * b1 a2b2 := a2 * b2 a1b1_w1lo := (a1 + da1 * (m_w1 - sqrt(v_w1))) * b1 a1b1_w1hi := (a1 + da1 * (m_w1 + sqrt(v_w1))) * b2 a2b2_w2lo := a2 * (b2 + db2 * (m_w2 - sqrt(v_w2))) a2b2_w2hi := a2 * (b2 + db2 * (m_w2 + sqrt(v_w2))) " fit <- sem(mod, data_med_mod_parallel, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 3, 6, 10, 11, 15, 48:53), ]
library(lavaan) data(data_med_mod_parallel) data_med_mod_parallel$xw1 <- data_med_mod_parallel$x * data_med_mod_parallel$w1 data_med_mod_parallel$m2w2 <- data_med_mod_parallel$m2 * data_med_mod_parallel$w2 mod <- " m1 ~ a1 * x + w1 + da1 * xw1 + c1 + c2 m2 ~ a2 * x + w1 + c1 + c2 y ~ b1 * m1 + b2 * m2 + x + w1 + w2 + db2 * m2w2 + c1 + c2 w1 ~~ v_w1 * w1 w1 ~ m_w1 * 1 w2 ~~ v_w2 * w2 w2 ~ m_w2 * 1 a1b1 := a1 * b1 a2b2 := a2 * b2 a1b1_w1lo := (a1 + da1 * (m_w1 - sqrt(v_w1))) * b1 a1b1_w1hi := (a1 + da1 * (m_w1 + sqrt(v_w1))) * b2 a2b2_w2lo := a2 * (b2 + db2 * (m_w2 - sqrt(v_w2))) a2b2_w2hi := a2 * (b2 + db2 * (m_w2 + sqrt(v_w2))) " fit <- sem(mod, data_med_mod_parallel, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 3, 6, 10, 11, 15, 48:53), ]
A parallel mediation model with two categorical moderators.
data_med_mod_parallel_cat
data_med_mod_parallel_cat
A data frame with 300 rows and 8 variables:
Predictor. Numeric.
Moderator. String. Values: "group1", "group2", "group3"
Moderator. String. Values: "team1", "team2"
Mediator 1. Numeric.
Mediator 2. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
data(data_med_mod_parallel_cat) dat <- data_med_mod_parallel_cat summary(lm_m1 <- lm(m1 ~ x*w1 + c1 + c2, dat)) summary(lm_m2 <- lm(m2 ~ x*w1 + c1 + c2, dat)) summary(lm_y <- lm(y ~ m1*w2 + m2*w2 + m1 + x + w1 + c1 + c2, dat))
data(data_med_mod_parallel_cat) dat <- data_med_mod_parallel_cat summary(lm_m1 <- lm(m1 ~ x*w1 + c1 + c2, dat)) summary(lm_m2 <- lm(m2 ~ x*w1 + c1 + c2, dat)) summary(lm_y <- lm(y ~ m1*w2 + m2*w2 + m1 + x + w1 + c1 + c2, dat))
A simple mediation model with a-path and b2-path moderated.
data_med_mod_serial
data_med_mod_serial
A data frame with 100 rows and 8 variables:
Predictor. Numeric.
Moderator 1. Numeric.
Moderator 2. Numeric.
Mediator 1. Numeric.
Mediator 2. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
library(lavaan) data(data_med_mod_serial) data_med_mod_serial$xw1 <- data_med_mod_serial$x * data_med_mod_serial$w1 data_med_mod_serial$m2w2 <- data_med_mod_serial$m2 * data_med_mod_serial$w2 mod <- " m1 ~ a * x + w1 + da1 * xw1 + c1 + c2 m2 ~ b1 * m1 + x + w1 + c1 + c2 y ~ b2 * m2 + m1 + x + w1 + w2 + db2 * m2w2 + c1 + c2 w1 ~~ v_w1 * w1 w1 ~ m_w1 * 1 w2 ~~ v_w2 * w2 w2 ~ m_w2 * 1 ab1b2 := a * b1 * b2 ab1b2_lolo := (a + da1 * (m_w1 - sqrt(v_w1))) * b1 * (b2 + db2 * (m_w2 - sqrt(v_w2))) ab1b2_lohi := (a + da1 * (m_w1 - sqrt(v_w1))) * b1 * (b2 + db2 * (m_w2 + sqrt(v_w2))) ab1b2_hilo := (a + da1 * (m_w1 + sqrt(v_w1))) * b1 * (b2 + db2 * (m_w2 - sqrt(v_w2))) ab1b2_hihi := (a + da1 * (m_w1 + sqrt(v_w1))) * b1 * (b2 + db2 * (m_w2 + sqrt(v_w2))) " fit <- sem(mod, data_med_mod_serial, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 3, 6, 11, 16, 49:53), ]
library(lavaan) data(data_med_mod_serial) data_med_mod_serial$xw1 <- data_med_mod_serial$x * data_med_mod_serial$w1 data_med_mod_serial$m2w2 <- data_med_mod_serial$m2 * data_med_mod_serial$w2 mod <- " m1 ~ a * x + w1 + da1 * xw1 + c1 + c2 m2 ~ b1 * m1 + x + w1 + c1 + c2 y ~ b2 * m2 + m1 + x + w1 + w2 + db2 * m2w2 + c1 + c2 w1 ~~ v_w1 * w1 w1 ~ m_w1 * 1 w2 ~~ v_w2 * w2 w2 ~ m_w2 * 1 ab1b2 := a * b1 * b2 ab1b2_lolo := (a + da1 * (m_w1 - sqrt(v_w1))) * b1 * (b2 + db2 * (m_w2 - sqrt(v_w2))) ab1b2_lohi := (a + da1 * (m_w1 - sqrt(v_w1))) * b1 * (b2 + db2 * (m_w2 + sqrt(v_w2))) ab1b2_hilo := (a + da1 * (m_w1 + sqrt(v_w1))) * b1 * (b2 + db2 * (m_w2 - sqrt(v_w2))) ab1b2_hihi := (a + da1 * (m_w1 + sqrt(v_w1))) * b1 * (b2 + db2 * (m_w2 + sqrt(v_w2))) " fit <- sem(mod, data_med_mod_serial, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 3, 6, 11, 16, 49:53), ]
A serial mediation model with two categorical moderators.
data_med_mod_serial_cat
data_med_mod_serial_cat
A data frame with 300 rows and 8 variables:
Predictor. Numeric.
Moderator. String. Values: "group1", "group2", "group3"
Moderator. String. Values: "team1", "team2"
Mediator 1. Numeric.
Mediator 2. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
data(data_med_mod_serial_cat) dat <- data_med_mod_serial_cat summary(lm_m1 <- lm(m1 ~ x*w1 + c1 + c2, dat)) summary(lm_m2 <- lm(m2 ~ m1 + x + w1 + c1 + c2, dat)) summary(lm_y <- lm(y ~ m2*w2 + m1 + x + w1 + c1 + c2, dat))
data(data_med_mod_serial_cat) dat <- data_med_mod_serial_cat summary(lm_m1 <- lm(m1 ~ x*w1 + c1 + c2, dat)) summary(lm_m2 <- lm(m2 ~ m1 + x + w1 + c1 + c2, dat)) summary(lm_y <- lm(y ~ m2*w2 + m1 + x + w1 + c1 + c2, dat))
A serial-parallel mediation model with some paths moderated.
data_med_mod_serial_parallel
data_med_mod_serial_parallel
A data frame with 100 rows and 9 variables:
Predictor. Numeric.
Moderator 1. Numeric.
Moderator 2. Numeric.
Mediator 1 in Path 1. Numeric.
Mediator 2 in Path 2. Numeric.
Mediator 2. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
library(lavaan) data(data_med_mod_serial_parallel) data_med_mod_serial_parallel$xw1 <- data_med_mod_serial_parallel$x * data_med_mod_serial_parallel$w1 data_med_mod_serial_parallel$m2w2 <- data_med_mod_serial_parallel$m2 * data_med_mod_serial_parallel$w2 mod <- " m11 ~ a1 * x + w1 + da11 * xw1 + c1 + c2 m12 ~ b11 * m11 + x + w1 + c1 + c2 m2 ~ a2 * x + c1 + c2 y ~ b12 * m12 + b2 * m2 + m11 + x + w1 + w2 + db2 * m2w2 + c1 + c2 w1 ~~ v_w1 * w1 w1 ~ m_w1 * 1 w2 ~~ v_w2 * w2 w2 ~ m_w2 * 1 a1b11b22 := a1 * b11 * b12 a2b2 := a2 * b2 ab := a1b11b22 + a2b2 a1b11b12_w1lo := (a1 + da11 * (m_w1 - sqrt(v_w1))) * b11 * b12 a1b11b12_w1hi := (a1 + da11 * (m_w1 + sqrt(v_w1))) * b11 * b12 a2b2_w2lo := a2 * (b2 + db2 * (m_w2 - sqrt(v_w2))) a2b2_w2hi := a2 * (b2 + db2 * (m_w2 + sqrt(v_w2))) " fit <- sem(mod, data_med_mod_serial_parallel, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[parameterEstimates(fit)$label != "", ]
library(lavaan) data(data_med_mod_serial_parallel) data_med_mod_serial_parallel$xw1 <- data_med_mod_serial_parallel$x * data_med_mod_serial_parallel$w1 data_med_mod_serial_parallel$m2w2 <- data_med_mod_serial_parallel$m2 * data_med_mod_serial_parallel$w2 mod <- " m11 ~ a1 * x + w1 + da11 * xw1 + c1 + c2 m12 ~ b11 * m11 + x + w1 + c1 + c2 m2 ~ a2 * x + c1 + c2 y ~ b12 * m12 + b2 * m2 + m11 + x + w1 + w2 + db2 * m2w2 + c1 + c2 w1 ~~ v_w1 * w1 w1 ~ m_w1 * 1 w2 ~~ v_w2 * w2 w2 ~ m_w2 * 1 a1b11b22 := a1 * b11 * b12 a2b2 := a2 * b2 ab := a1b11b22 + a2b2 a1b11b12_w1lo := (a1 + da11 * (m_w1 - sqrt(v_w1))) * b11 * b12 a1b11b12_w1hi := (a1 + da11 * (m_w1 + sqrt(v_w1))) * b11 * b12 a2b2_w2lo := a2 * (b2 + db2 * (m_w2 - sqrt(v_w2))) a2b2_w2hi := a2 * (b2 + db2 * (m_w2 + sqrt(v_w2))) " fit <- sem(mod, data_med_mod_serial_parallel, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[parameterEstimates(fit)$label != "", ]
A serial-parallel mediation model with two categorical moderators.
data_med_mod_serial_parallel_cat
data_med_mod_serial_parallel_cat
A data frame with 300 rows and 8 variables:
Predictor. Numeric.
Moderator. String. Values: "group1", "group2", "group3"
Moderator. String. Values: "team1", "team2"
Mediator 1 in Path 1. Numeric.
Mediator 2 in Path 1. Numeric.
Mediator in Path 2. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
data(data_med_mod_serial_parallel_cat) dat <- data_med_mod_serial_parallel_cat summary(lm_m11 <- lm(m11 ~ x*w1 + c1 + c2, dat)) summary(lm_m12 <- lm(m12 ~ m11 + x + w1 + c1 + c2, dat)) summary(lm_m2 <- lm(m2 ~ x + w1 + c1 + c2, dat)) summary(lm_y <- lm(y ~ m12 + m2*w2 + m12 + x + c1 + c2, dat))
data(data_med_mod_serial_parallel_cat) dat <- data_med_mod_serial_parallel_cat summary(lm_m11 <- lm(m11 ~ x*w1 + c1 + c2, dat)) summary(lm_m12 <- lm(m12 ~ m11 + x + w1 + c1 + c2, dat)) summary(lm_m2 <- lm(m2 ~ x + w1 + c1 + c2, dat)) summary(lm_y <- lm(y ~ m12 + m2*w2 + m12 + x + c1 + c2, dat))
A one-moderator model.
data_mod
data_mod
A data frame with 100 rows and 5 variables:
Predictor. Numeric.
Moderator. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
library(lavaan) data(data_mod) data_mod$xw <- data_mod$x * data_mod$w mod <- " y ~ a * x + w + d * xw + c1 + c2 w ~~ v_w * w w ~ m_w * 1 a_lo := a + d * (m_w - sqrt(v_w)) a_hi := a + d * (m_w + sqrt(v_w)) " fit <- sem(mod, data_mod, fixed.x = FALSE) parameterEstimates(fit)[c(1, 3, 6, 7, 24, 25), ]
library(lavaan) data(data_mod) data_mod$xw <- data_mod$x * data_mod$w mod <- " y ~ a * x + w + d * xw + c1 + c2 w ~~ v_w * w w ~ m_w * 1 a_lo := a + d * (m_w - sqrt(v_w)) a_hi := a + d * (m_w + sqrt(v_w)) " fit <- sem(mod, data_mod, fixed.x = FALSE) parameterEstimates(fit)[c(1, 3, 6, 7, 24, 25), ]
A moderation model with a categorical moderator.
data_mod_cat
data_mod_cat
A data frame with 300 rows and 5 variables:
Predictor. Numeric.
Moderator. String. Values: "group1", "group2", "group3"
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
data(data_mod_cat) dat <- data_mod_cat summary(lm_y <- lm(y ~ x*w + c1 + c2, dat))
data(data_mod_cat) dat <- data_mod_cat summary(lm_y <- lm(y ~ x*w + c1 + c2, dat))
A two-moderator model.
data_mod2
data_mod2
A data frame with 100 rows and 6 variables:
Predictor. Numeric.
Moderator 1. Numeric.
Moderator 2. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
library(lavaan) data(data_mod2) data_mod2$xw1 <- data_mod2$x * data_mod2$w1 data_mod2$xw2 <- data_mod2$x * data_mod2$w2 mod <- " y ~ a * x + w1 + w2 + d1 * xw1 + d2 * xw2 + c1 + c2 w1 ~~ v_w1 * w1 w1 ~ m_w1 * 1 w2 ~~ v_w2 * w2 w2 ~ m_w2 * 1 a_lolo := a + d1 * (m_w1 - sqrt(v_w1)) + d2 * (m_w2 - sqrt(v_w2)) a_lohi := a + d1 * (m_w1 - sqrt(v_w1)) + d2 * (m_w2 + sqrt(v_w2)) a_hilo := a + d1 * (m_w1 + sqrt(v_w1)) + d2 * (m_w2 - sqrt(v_w2)) a_hihi := a + d1 * (m_w1 + sqrt(v_w1)) + d2 * (m_w2 + sqrt(v_w2)) " fit <- sem(mod, data_mod2, fixed.x = FALSE) parameterEstimates(fit)[c(1, 4, 5, 8:11, 34:37), ]
library(lavaan) data(data_mod2) data_mod2$xw1 <- data_mod2$x * data_mod2$w1 data_mod2$xw2 <- data_mod2$x * data_mod2$w2 mod <- " y ~ a * x + w1 + w2 + d1 * xw1 + d2 * xw2 + c1 + c2 w1 ~~ v_w1 * w1 w1 ~ m_w1 * 1 w2 ~~ v_w2 * w2 w2 ~ m_w2 * 1 a_lolo := a + d1 * (m_w1 - sqrt(v_w1)) + d2 * (m_w2 - sqrt(v_w2)) a_lohi := a + d1 * (m_w1 - sqrt(v_w1)) + d2 * (m_w2 + sqrt(v_w2)) a_hilo := a + d1 * (m_w1 + sqrt(v_w1)) + d2 * (m_w2 - sqrt(v_w2)) a_hihi := a + d1 * (m_w1 + sqrt(v_w1)) + d2 * (m_w2 + sqrt(v_w2)) " fit <- sem(mod, data_mod2, fixed.x = FALSE) parameterEstimates(fit)[c(1, 4, 5, 8:11, 34:37), ]
Generated from a complicated moderated-mediation model for demonstration.
data_mome_demo
data_mome_demo
A data frame with 200 rows and 11 variables:
Predictor 1. Numeric.
Predictor 2. Numeric.
Mediator 1. Numeric.
Mediator 2. Numeric.
Mediator 3. Numeric.
Outcome Variable 1. Numeric.
Outcome Variable 2. Numeric.
Moderator 1. Numeric.
Moderator 21. Numeric.
Control Variable 1. Numeric.
Control Variable 2. Numeric.
The model:
# w1x1 <- x1 * w1 # w2m2 <- w2 * m2 m1 ~ x1 + w1 + w1x1 + x2 + c1 + c2 m2 ~ m1 + c1 + c2 m3 ~ x2 + x1 + c1 + c2 y1 ~ m2 + w2 + w2m2 + x1 + x2 + m3 + c1 + c2 y2 ~ m3 + x2 + x1 + m2 + c1 + c2 # Covariances excluded for brevity
Generated from a complicated moderated-mediation model for demonstration, with missing data
data_mome_demo_missing
data_mome_demo_missing
A data frame with 200 rows and 11 variables:
Predictor 1. Numeric.
Predictor 2. Numeric.
Mediator 1. Numeric.
Mediator 2. Numeric.
Mediator 3. Numeric.
Outcome Variable 1. Numeric.
Outcome Variable 2. Numeric.
Moderator 1. Numeric.
Moderator 21. Numeric.
Control Variable 1. Numeric.
Control Variable 2. Numeric.
A copy of data_mome_demo with some
randomly selected cells changed to
NA
. The number of cases with no
missing data is 169.
The model:
# w1x1 <- x1 * w1 # w2m2 <- w2 * m2 m1 ~ x1 + w1 + w1x1 + x2 + c1 + c2 m2 ~ m1 + c1 + c2 m3 ~ x2 + x1 + c1 + c2 y1 ~ m2 + w2 + w2m2 + x1 + x2 + m3 + c1 + c2 y2 ~ m3 + x2 + x1 + m2 + c1 + c2 # Covariances excluded for brevity
A parallel mediation model.
data_parallel
data_parallel
A data frame with 100 rows and 6 variables:
Predictor. Numeric.
Mediator 1. Numeric.
Mediator 2. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
library(lavaan) data(data_parallel) mod <- " m1 ~ a1 * x + c1 + c2 m2 ~ a2 * x + c1 + c2 y ~ b2 * m2 + b1 * m1 + x + c1 + c2 indirect1 := a1 * b1 indirect2 := a2 * b2 indirect := a1 * b1 + a2 * b2 " fit <- sem(mod, data_parallel, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 4, 7, 8, 27:29), ]
library(lavaan) data(data_parallel) mod <- " m1 ~ a1 * x + c1 + c2 m2 ~ a2 * x + c1 + c2 y ~ b2 * m2 + b1 * m1 + x + c1 + c2 indirect1 := a1 * b1 indirect2 := a2 * b2 indirect := a1 * b1 + a2 * b2 " fit <- sem(mod, data_parallel, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 4, 7, 8, 27:29), ]
This data set is for testing functions in a four-factor structural model.
data_sem
data_sem
A data frame with 200 rows and 14 variables:
Indicator. Numeric.
Indicator. Numeric.
Indicator. Numeric.
Indicator. Numeric.
Indicator. Numeric.
Indicator. Numeric.
Indicator. Numeric.
Indicator. Numeric.
Indicator. Numeric.
Indicator. Numeric.
Indicator. Numeric.
Indicator. Numeric.
Indicator. Numeric.
Indicator. Numeric.
data(data_sem) dat <- data_med_mod_b_mod mod <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ a1*f1 + a2*f2 f4 ~ b1*f1 + b3*f3 a1b3 := a1 * b3 a2b3 := a2 * b3 ' fit <- lavaan::sem(model = mod, data = data_sem) summary(fit)
data(data_sem) dat <- data_med_mod_b_mod mod <- 'f1 =~ x01 + x02 + x03 f2 =~ x04 + x05 + x06 + x07 f3 =~ x08 + x09 + x10 f4 =~ x11 + x12 + x13 + x14 f3 ~ a1*f1 + a2*f2 f4 ~ b1*f1 + b3*f3 a1b3 := a1 * b3 a2b3 := a2 * b3 ' fit <- lavaan::sem(model = mod, data = data_sem) summary(fit)
A serial mediation model.
data_serial
data_serial
A data frame with 100 rows and 6 variables:
Predictor. Numeric.
Mediator 1. Numeric.
Mediator 2. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
library(lavaan) data(data_serial) mod <- " m1 ~ a * x + c1 + c2 m2 ~ b1 * m1 + x + c1 + c2 y ~ b2 * m2 + m1 + x + c1 + c2 indirect := a * b1 * b2 " fit <- sem(mod, data_serial, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 4, 8, 28), ]
library(lavaan) data(data_serial) mod <- " m1 ~ a * x + c1 + c2 m2 ~ b1 * m1 + x + c1 + c2 y ~ b2 * m2 + m1 + x + c1 + c2 indirect := a * b1 * b2 " fit <- sem(mod, data_serial, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 4, 8, 28), ]
A mediation model with both serial and parallel components.
data_serial_parallel
data_serial_parallel
A data frame with 100 rows and 7 variables:
Predictor. Numeric.
Mediator 1 in Path 1. Numeric.
Mediator 2 in Path 1. Numeric.
Mediator in Path 2. Numeric.
Outcome variable. Numeric.
Control variable. Numeric.
Control variable. Numeric.
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ a11 * x + c1 + c2 m12 ~ b11 * m11 + x + c1 + c2 m2 ~ a2 * x + c1 + c2 y ~ b12 * m12 + b2 * m2 + m11 + x + c1 + c2 indirect1 := a11 * b11 * b12 indirect2 := a2 * b2 indirect := a11 * b11 * b12 + a2 * b2 " fit <- sem(mod, data_serial_parallel, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 4, 8, 11, 12, 34:36), ]
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ a11 * x + c1 + c2 m12 ~ b11 * m11 + x + c1 + c2 m2 ~ a2 * x + c1 + c2 y ~ b12 * m12 + b2 * m2 + m11 + x + c1 + c2 indirect1 := a11 * b11 * b12 indirect2 := a2 * b2 indirect := a11 * b11 * b12 + a2 * b2 " fit <- sem(mod, data_serial_parallel, meanstructure = TRUE, fixed.x = FALSE) parameterEstimates(fit)[c(1, 4, 8, 11, 12, 34:36), ]
Generated from a 3-mediator
mediation model among eight latent
factors, fx1
, fx2
, fm11
, fm12
,
fy1
, and fy2
, each
has three indicators.
data_serial_parallel_latent
data_serial_parallel_latent
A data frame with 500 rows and 21 variables:
Indicator of fx1
. Numeric.
Indicator of fx1
. Numeric.
Indicator of fx1
. Numeric.
Indicator of fx2
. Numeric.
Indicator of fx2
. Numeric.
Indicator of fx2
. Numeric.
Indicator of fm11
. Numeric.
Indicator of fm11
. Numeric.
Indicator of fm11
. Numeric.
Indicator of fm12
. Numeric.
Indicator of fm12
. Numeric.
Indicator of fm12
. Numeric.
Indicator of fm2
. Numeric.
Indicator of fm2
. Numeric.
Indicator of fm2
. Numeric.
Indicator of fy1
. Numeric.
Indicator of fy1
. Numeric.
Indicator of fy1
. Numeric.
Indicator of fy2
. Numeric.
Indicator of fy2
. Numeric.
Indicator of fy2
. Numeric.
The model:
fx1 =~ x1 + x2 + x3 fx2 =~ x4 + x5 + x6 fm11 =~ m11a + m11b + m11c fm12 =~ m12a + m12b + m12c fm2 =~ m2a + m2b + m2c fy1 =~ y1 + y2 + y3 fy2 =~ y3 + y4 + y5 fm11 ~ a1 * fx1 fm12 ~ b11 * fm11 + a2m * fx2 fm2 ~ a2 * fx2 fy1 ~ b12 * fm12 + b11y1 * fm11 + cp1 * fx1 fy2 ~ b2 * fm2 + cp2 * fx2 a1b11b12 := a1 * b11 * b12 a1b11y1 := a1 * b11y1 a2b2 := a2 * b2 a2mb12 := a2m * b12
It computes the
Delta_Med proposed by Liu, Yuan,
and Li (2023), an -like measure of
indirect effect.
delta_med( x, y, m, fit, paths_to_remove = NULL, boot_out = NULL, level = 0.95, progress = TRUE, skip_check_single_x = FALSE, skip_check_m_between_x_y = FALSE, skip_check_x_to_y = FALSE, skip_check_latent_variables = FALSE, boot_type = c("perc", "bc") )
delta_med( x, y, m, fit, paths_to_remove = NULL, boot_out = NULL, level = 0.95, progress = TRUE, skip_check_single_x = FALSE, skip_check_m_between_x_y = FALSE, skip_check_x_to_y = FALSE, skip_check_latent_variables = FALSE, boot_type = c("perc", "bc") )
x |
The name of the |
y |
The name of the |
m |
A vector of the variable
names of the mediator(s). If more
than one mediators, they do not have
to be on the same path from |
fit |
The fit object. Must be a lavaan::lavaan object. |
paths_to_remove |
A character
vector of paths users want to
manually remove, specified in
|
boot_out |
The
output of |
level |
The level of confidence of the bootstrap confidence interval. Default is .95. |
progress |
Logical. Display
bootstrapping progress or not.
Default is |
skip_check_single_x |
Logical
Check whether the model has one and
only one x-variable. Default is |
skip_check_m_between_x_y |
Logical. Check whether all |
skip_check_x_to_y |
Logical.
Check whether there is a direct path
from |
skip_check_latent_variables |
Logical. Check whether the model
has any latent variables. Default
is |
boot_type |
If bootstrap
confidence interval is to be formed,
the type of bootstrap confidence
interval. The supported types
are |
It computes Delta_Med, an
-like effect
size measure for the indirect effect
from one variable (the
y
-variable)
to another variable (the x
-variable)
through one or more mediators
(m
, or m1
, m2
, etc. when
there are more than one mediator).
The Delta_Med of one or more
mediators was computed as the
difference between
two s:
, the
when
y
is predicted by x
and all
mediators.
, the
when the
mediator(s) of
interest is/are removed from the
models, while the error term(s)
of the mediator(s) is/are kept.
Delta_Med is given by
.
Please refer to Liu et al. (2023) for the technical details.
The function can also form a nonparametric percentile bootstrap confidence of Delta_Med.
A delta_med
class object.
It is a list-like object with these
major elements:
delta_med
: The Delta_Med.
x
: The name of the x
-variable.
y
: The name of the y
-variable.
m
: A character vector of the
mediator(s) along a path. The path
runs from the first element to the
last element.
This class has a print
method,
a coef
method, and a confint
method. See print.delta_med()
,
coef.delta_med()
, and
confint.delta_med()
.
The function identifies all the
path(s) pointing to the mediator(s)
of concern and fixes the path(s) to
zero, effectively removing the
mediator(s). However, the model is
not refitted, hence keeping the
estimates of all other parameters
unchanged.
It then uses lavaan::lav_model_set_parameters()
to update the parameters,
lavaan::lav_model_implied()
to
update the implied statistics, and
then calls lavaan::lavInspect()
to
retrieve the implied variance of the
predicted values of y
for computing
the . Subtracting this
from
of
y
can then yield Delta_Med.
For now, by default, it only computes Delta_Med for the types of models discussed in Liu et al. (2023):
Having one predictor (the
x
-variable).
Having one or more mediators, the
m
-variables, with
arbitrary way to mediate the effect
of x
on the outcome variable
(y
-variable).
Having one or more outcome variables. Although their models only have outcome variables, the computation of the Delta_Med is not affected by the presence of other outcome variables.
Having no control variables.
The mediator(s), m
, and the
y
-variable are continuous.
x
can be continuous
or categorical. If categorical, it
needs to be handle appropriately
when fitting the model.
x
has a direct
path to y
.
All the mediators listed in the
argument m
is present in at least
one path from x
to y.
None of the paths from x
to y
are moderated.
It can be used for other kinds
of models but support for them is
disabled by default. To use
this function for
cases not discussed in
Liu et al. (2023), please disable
relevant requirements stated above
using the relevant
skip_check_*
arguments. An error
will be raised if the models failed
any of the checks not skipped by
users.
Liu, H., Yuan, K.-H., & Li, H. (2023). A systematic framework for defining R-squared measures in mediation analysis. Psychological Methods. Advance online publication. https://doi.org/10.1037/met0000571
print.delta_med()
,
coef.delta_med()
, and
confint.delta_med()
.
library(lavaan) dat <- data_med mod <- " m ~ x y ~ m + x " fit <- sem(mod, dat) dm <- delta_med(x = "x", y = "y", m = "m", fit = fit) dm print(dm, full = TRUE) # Call do_boot() to generate # bootstrap estimates # Use 2000 or even 5000 for R in real studies # Set parallel to TRUE in real studies for faster bootstrapping boot_out <- do_boot(fit, R = 45, seed = 879, parallel = FALSE, progress = FALSE) # Remove 'progress = FALSE' in practice dm_boot <- delta_med(x = "x", y = "y", m = "m", fit = fit, boot_out = boot_out, progress = FALSE) dm_boot confint(dm_boot)
library(lavaan) dat <- data_med mod <- " m ~ x y ~ m + x " fit <- sem(mod, dat) dm <- delta_med(x = "x", y = "y", m = "m", fit = fit) dm print(dm, full = TRUE) # Call do_boot() to generate # bootstrap estimates # Use 2000 or even 5000 for R in real studies # Set parallel to TRUE in real studies for faster bootstrapping boot_out <- do_boot(fit, R = 45, seed = 879, parallel = FALSE, progress = FALSE) # Remove 'progress = FALSE' in practice dm_boot <- delta_med(x = "x", y = "y", m = "m", fit = fit, boot_out = boot_out, progress = FALSE) dm_boot confint(dm_boot)
Generate bootstrap
estimates to be used by
cond_indirect_effects()
,
indirect_effect()
, and
cond_indirect()
,
do_boot( fit, R = 100, seed = NULL, parallel = TRUE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE )
do_boot( fit, R = 100, seed = NULL, parallel = TRUE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE )
fit |
It can be (a) a list of |
R |
The number of bootstrap samples. Default is 100. |
seed |
The seed for the
bootstrapping. Default is |
parallel |
Logical. Whether
parallel processing will be used.
Default is |
ncores |
Integer. The number of
CPU cores to use when |
make_cluster_args |
A named list
of additional arguments to be passed
to |
progress |
Logical. Display
progress or not. Default is |
It does nonparametric
bootstrapping to generate bootstrap
estimates of the parameter estimates
in a model fitted either by
lavaan::sem()
or by a sequence of
calls to lm()
. The stored estimates
can then be used by
cond_indirect_effects()
,
indirect_effect()
, and
cond_indirect()
to form
bootstrapping confidence intervals.
This approach removes the need to
repeat bootstrapping in each call to
cond_indirect_effects()
,
indirect_effect()
, and
cond_indirect()
. It also ensures
that the same set of bootstrap
samples is used in all subsequent
analysis.
It determines the type of the fit
object automatically and then calls
lm2boot_out()
, fit2boot_out()
, or
fit2boot_out_do_boot()
.
Since Version 0.1.14.2, support for
multigroup models has been added for models
fitted by lavaan
. The implementation
of bootstrapping is identical to
that used by lavaan
, with resampling
done within each group.
A boot_out
-class object
that can be used for the boot_out
argument of
cond_indirect_effects()
,
indirect_effect()
, and
cond_indirect()
for forming
bootstrap confidence intervals. The
object is a list with the number of
elements equal to the number of
bootstrap samples. Each element is a
list of the parameter estimates and
sample variances and covariances of
the variables in each bootstrap
sample.
lm2boot_out()
,
fit2boot_out()
, and
fit2boot_out_do_boot()
, which
implements the bootstrapping.
data(data_med_mod_ab1) dat <- data_med_mod_ab1 lm_m <- lm(m ~ x*w + c1 + c2, dat) lm_y <- lm(y ~ m*w + x + c1 + c2, dat) lm_out <- lm2list(lm_m, lm_y) # In real research, R should be 2000 or even 5000 # In real research, no need to set parallel and progress to FALSE # Parallel processing is enabled by default and # progress is displayed by default. lm_boot_out <- do_boot(lm_out, R = 50, seed = 1234, parallel = FALSE, progress = FALSE) wlevels <- mod_levels(w = "w", fit = lm_out) wlevels out <- cond_indirect_effects(wlevels = wlevels, x = "x", y = "y", m = "m", fit = lm_out, boot_ci = TRUE, boot_out = lm_boot_out) out
data(data_med_mod_ab1) dat <- data_med_mod_ab1 lm_m <- lm(m ~ x*w + c1 + c2, dat) lm_y <- lm(y ~ m*w + x + c1 + c2, dat) lm_out <- lm2list(lm_m, lm_y) # In real research, R should be 2000 or even 5000 # In real research, no need to set parallel and progress to FALSE # Parallel processing is enabled by default and # progress is displayed by default. lm_boot_out <- do_boot(lm_out, R = 50, seed = 1234, parallel = FALSE, progress = FALSE) wlevels <- mod_levels(w = "w", fit = lm_out) wlevels out <- cond_indirect_effects(wlevels = wlevels, x = "x", y = "y", m = "m", fit = lm_out, boot_ci = TRUE, boot_out = lm_boot_out) out
Generate Monte Carlo
estimates to be used by
cond_indirect_effects()
,
indirect_effect()
, and
cond_indirect()
,
do_mc( fit, R = 100, seed = NULL, parallel = TRUE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE ) gen_mc_est(fit, R = 100, seed = NULL)
do_mc( fit, R = 100, seed = NULL, parallel = TRUE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE ) gen_mc_est(fit, R = 100, seed = NULL)
fit |
The output of
|
R |
The number of replications. Default is 100. |
seed |
The seed for the
generating Monte Carlo estimates.
Default is |
parallel |
Not used. Kept
for compatibility with |
ncores |
Not used. Kept
for compatibility with |
make_cluster_args |
Not used. Kept
for compatibility with |
progress |
Logical. Display
progress or not. Default is |
It uses the parameter
estimates and their variance-covariance
matrix to generate Monte Carlo
estimates of the parameter estimates
in a model fitted by
lavaan::sem()
. The stored estimates
can then be used by
cond_indirect_effects()
,
indirect_effect()
, and
cond_indirect()
to form
Monte Carlo confidence intervals.
It also supports a model estimated
by multiple imputation using
semTools::runMI()
or its wrapper,
such as semTools::sem.mi()
.
The pooled estimates and their
variance-covariance matrix will be used
to generate the Monte Carlo estimates.
This approach removes the need to
repeat Monte Carlo simulation in
each call to
cond_indirect_effects()
,
indirect_effect()
, and
cond_indirect()
. It also ensures
that the same set of Monte Carlo
estimates is used in all subsequent
analysis.
Since Version 0.1.14.2, support for
multigroup models has been added for models
fitted by lavaan
.
A mc_out
-class object
that can be used for the mc_out
argument of
cond_indirect_effects()
,
indirect_effect()
, and
cond_indirect()
for forming
Monte Carlo confidence intervals. The
object is a list with the number of
elements equal to the number of
Monte Carlo replications. Each element
is a
list of the parameter estimates and
sample variances and covariances of
the variables in each Monte Carlo
replication.
do_mc()
: A general purpose function for
creating Monte Carlo estimates to be reused
by other functions. It returns a
mc_out
-class object.
gen_mc_est()
: Generate Monte Carlo
estimates and store them in the external
slot: external$manymome$mc
. For advanced
users.
fit2mc_out()
, which
implements the Monte Carlo simulation.
library(lavaan) data(data_med_mod_ab1) dat <- data_med_mod_ab1 mod <- " m ~ x + w + x:w + c1 + c2 y ~ m + w + m:w + x + c1 + c2 " fit <- sem(mod, dat) # In real research, R should be 5000 or even 10000 mc_out <- do_mc(fit, R = 100, seed = 1234) wlevels <- mod_levels(w = "w", fit = fit) wlevels out <- cond_indirect_effects(wlevels = wlevels, x = "x", y = "y", m = "m", fit = fit, mc_ci = TRUE, mc_out = mc_out) out
library(lavaan) data(data_med_mod_ab1) dat <- data_med_mod_ab1 mod <- " m ~ x + w + x:w + c1 + c2 y ~ m + w + m:w + x + c1 + c2 " fit <- sem(mod, dat) # In real research, R should be 5000 or even 10000 mc_out <- do_mc(fit, R = 100, seed = 1234) wlevels <- mod_levels(w = "w", fit = fit) wlevels out <- cond_indirect_effects(wlevels = wlevels, x = "x", y = "y", m = "m", fit = fit, mc_ci = TRUE, mc_out = mc_out) out
Create dummy variables from a categorical variable.
factor2var( x_value, x_contrasts = "contr.treatment", prefix = "", add_rownames = TRUE )
factor2var( x_value, x_contrasts = "contr.treatment", prefix = "", add_rownames = TRUE )
x_value |
The vector of categorical variable. |
x_contrasts |
The contrast to be
used. Default is
|
prefix |
The prefix to be added
to the variables to be created.
Default is |
add_rownames |
Whether row names
will be added to the output. Default
is |
Its main use is for creating
dummy variables (indicator variables)
from a categorical variable, to be
used in lavaan::sem()
.
Optionally, the other contrasts can
be used through the argument
x_contrasts
.
It always returns a matrix
with the number of rows equal to the
length of the vector (x_value
). If
the categorical has only two
categories and so only one dummy
variable is needed, the output is
still a one-column "matrix" in R.
dat <- data_mod_cat dat <- data.frame(dat, factor2var(dat$w, prefix = "gp", add_rownames = FALSE)) head(dat[, c("w", "gpgroup2", "gpgroup3")], 15)
dat <- data_mod_cat dat <- data.frame(dat, factor2var(dat$w, prefix = "gp", add_rownames = FALSE)) head(dat[, c("w", "gpgroup2", "gpgroup3")], 15)
lavaan
OutputGenerate bootstrap
estimates from the output of
lavaan::sem()
.
fit2boot_out(fit) fit2boot_out_do_boot( fit, R = 100, seed = NULL, parallel = FALSE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE, internal = list() )
fit2boot_out(fit) fit2boot_out_do_boot( fit, R = 100, seed = NULL, parallel = FALSE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE, internal = list() )
fit |
The fit object. This function only supports a lavaan::lavaan object. |
R |
The number of bootstrap samples. Default is 100. |
seed |
The seed for the random
resampling. Default is |
parallel |
Logical. Whether
parallel processing will be used.
Default is |
ncores |
Integer. The number of
CPU cores to use when |
make_cluster_args |
A named list
of additional arguments to be passed
to |
progress |
Logical. Display
progress or not. Default is |
internal |
A list of arguments
to be used internally for debugging.
Default is |
This function is for
advanced users. do_boot()
is a
function users should try first
because do_boot()
has a general
interface for input-specific
functions like this one.
If bootstrapping confidence intervals
was requested when calling
lavaan::sem()
by setting se = "boot"
, fit2boot_out()
can be used
to extract the stored bootstrap
estimates so that they can be reused
by indirect_effect()
,
cond_indirect_effects()
and related
functions to form bootstrapping
confidence intervals for effects such
as indirect effects and conditional
indirect effects.
If bootstrapping confidence was not
requested when fitting the model by
lavaan::sem()
,
fit2boot_out_do_boot()
can be used
to generate nonparametric bootstrap
estimates from the output of
lavaan::sem()
and store them for
use by indirect_effect()
,
cond_indirect_effects()
, and
related functions.
This approach removes the need to
repeat bootstrapping in each call to
indirect_effect()
,
cond_indirect_effects()
, and
related functions. It also ensures
that the same set of bootstrap
samples is used in all subsequent
analyses.
A boot_out
-class object
that can be used for the boot_out
argument of indirect_effect()
,
cond_indirect_effects()
, and
related functions for forming
bootstrapping confidence intervals.
The object is a list with the number of elements equal to the number of bootstrap samples. Each element is a list of the parameter estimates and sample variances and covariances of the variables in each bootstrap sample.
fit2boot_out()
: Process
stored bootstrap estimates for
functions such as
cond_indirect_effects()
.
fit2boot_out_do_boot()
: Do
bootstrapping and store information
to be used by
cond_indirect_effects()
and related
functions. Support parallel
processing.
do_boot()
, the general
purpose function that users should
try first before using this function.
library(lavaan) data(data_med_mod_ab1) dat <- data_med_mod_ab1 dat$"x:w" <- dat$x * dat$w dat$"m:w" <- dat$m * dat$w mod <- " m ~ x + w + x:w + c1 + c2 y ~ m + w + m:w + x + c1 + c2 " # Bootstrapping not requested in calling lavaan::sem() fit <- sem(model = mod, data = dat, fixed.x = FALSE, se = "none", baseline = FALSE) fit_boot_out <- fit2boot_out_do_boot(fit = fit, R = 40, seed = 1234, progress = FALSE) out <- cond_indirect_effects(wlevels = "w", x = "x", y = "y", m = "m", fit = fit, boot_ci = TRUE, boot_out = fit_boot_out) out
library(lavaan) data(data_med_mod_ab1) dat <- data_med_mod_ab1 dat$"x:w" <- dat$x * dat$w dat$"m:w" <- dat$m * dat$w mod <- " m ~ x + w + x:w + c1 + c2 y ~ m + w + m:w + x + c1 + c2 " # Bootstrapping not requested in calling lavaan::sem() fit <- sem(model = mod, data = dat, fixed.x = FALSE, se = "none", baseline = FALSE) fit_boot_out <- fit2boot_out_do_boot(fit = fit, R = 40, seed = 1234, progress = FALSE) out <- cond_indirect_effects(wlevels = "w", x = "x", y = "y", m = "m", fit = fit, boot_ci = TRUE, boot_out = fit_boot_out) out
lavaan
OutputGenerate Monte Carlo
estimates from the output of
lavaan::sem()
.
fit2mc_out(fit, progress = TRUE)
fit2mc_out(fit, progress = TRUE)
fit |
The fit object. This
function only supports a
lavaan::lavaan object.
It can also be
a |
progress |
Logical. Display
progress or not. Default is |
This function is for
advanced users. do_mc()
is a
function users should try first
because do_mc()
has a general
interface for input-specific
functions like this one.
fit2mc_out()
can be used
to extract the stored Monte Carlo
estimates so that they can be reused
by indirect_effect()
,
cond_indirect_effects()
and related
functions to form Monte Carlo
confidence intervals for effects such
as indirect effects and conditional
indirect effects.
This approach removes the need to
repeat Monte Carlo simulation in each
call to
indirect_effect()
,
cond_indirect_effects()
, and
related functions. It also ensures
that the same set of Monte Carlo
estimates is used in all subsequent
analyses.
A mc_out
-class object
that can be used for the mc_out
argument of indirect_effect()
,
cond_indirect_effects()
, and
related functions for forming
Monte Carlo confidence intervals.
The object is a list with the number of elements equal to the number of Monte Carlo replications. Each element is a list of the parameter estimates and sample variances and covariances of the variables in each Monte Carlo replication.
do_mc()
, the general
purpose function that users should
try first before using this function.
library(lavaan) data(data_med_mod_ab1) dat <- data_med_mod_ab1 dat$"x:w" <- dat$x * dat$w dat$"m:w" <- dat$m * dat$w mod <- " m ~ x + w + x:w + c1 + c2 y ~ m + w + m:w + x + c1 + c2 " fit <- sem(model = mod, data = dat, fixed.x = FALSE, baseline = FALSE) # In real research, R should be 5000 or even 10000. fit <- gen_mc_est(fit, R = 100, seed = 453253) fit_mc_out <- fit2mc_out(fit) out <- cond_indirect_effects(wlevels = "w", x = "x", y = "y", m = "m", fit = fit, mc_ci = TRUE, mc_out = fit_mc_out) out
library(lavaan) data(data_med_mod_ab1) dat <- data_med_mod_ab1 dat$"x:w" <- dat$x * dat$w dat$"m:w" <- dat$m * dat$w mod <- " m ~ x + w + x:w + c1 + c2 y ~ m + w + m:w + x + c1 + c2 " fit <- sem(model = mod, data = dat, fixed.x = FALSE, baseline = FALSE) # In real research, R should be 5000 or even 10000. fit <- gen_mc_est(fit, R = 100, seed = 453253) fit_mc_out <- fit2mc_out(fit) out <- cond_indirect_effects(wlevels = "w", x = "x", y = "y", m = "m", fit = fit, mc_ci = TRUE, mc_out = fit_mc_out) out
Return the conditional
indirect effect of one row of the
output of cond_indirect_effects()
.
get_one_cond_indirect_effect(object, row) get_one_cond_effect(object, row) print_all_cond_indirect_effects(object, ...) print_all_cond_effects(object, ...)
get_one_cond_indirect_effect(object, row) get_one_cond_effect(object, row) print_all_cond_indirect_effects(object, ...) print_all_cond_effects(object, ...)
object |
The output of
|
row |
The row number of the row to be retrieved. |
... |
Optional arguments to be
passed to teh |
get_one_cond_indirect_effect()
extracts the
corresponding output of
cond_indirect()
from the requested
row.
get_one_cond_effect()
is an
alias of get_one_cond_indirect_effect()
.
print_all_cond_indirect_effects()
loops over
the conditional effects and print all
of them.
print_all_cond_effects()
is an
alias of print_all_cond_indirect_effects()
.
get_one_cond_indirect_effect()
returns an indirect
-class object,
similar to the output of
indirect_effect()
and
cond_indirect()
. See
indirect_effect()
and
cond_indirect()
for details on
these classes.
print_all_cond_indirect_effects()
returns the object invisibly. Called
for its side effect.
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ x + w1 + x:w1 m2 ~ m1 y ~ m2 + x + w4 + m2:w4 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # Examples for cond_indirect(): # Conditional effects from x to m1 # when w1 is equal to each of the default levels out1 <- cond_indirect_effects(x = "x", y = "m1", wlevels = c("w1", "w4"), fit = fit) get_one_cond_indirect_effect(out1, 3) # Conditional Indirect effect from x1 through m1 to y, # when w1 is equal to each of the levels out2 <- cond_indirect_effects(x = "x", y = "y", m = c("m1", "m2"), wlevels = c("w1", "w4"), fit = fit) get_one_cond_indirect_effect(out2, 4) print_all_cond_indirect_effects(out2, digits = 2)
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ x + w1 + x:w1 m2 ~ m1 y ~ m2 + x + w4 + m2:w4 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # Examples for cond_indirect(): # Conditional effects from x to m1 # when w1 is equal to each of the default levels out1 <- cond_indirect_effects(x = "x", y = "m1", wlevels = c("w1", "w4"), fit = fit) get_one_cond_indirect_effect(out1, 3) # Conditional Indirect effect from x1 through m1 to y, # when w1 is equal to each of the levels out2 <- cond_indirect_effects(x = "x", y = "y", m = c("m1", "m2"), wlevels = c("w1", "w4"), fit = fit) get_one_cond_indirect_effect(out2, 4) print_all_cond_indirect_effects(out2, digits = 2)
Identify the product term(s), if any, along a path in a model and return the term(s), with the variables involved and the coefficient(s) of the term(s).
get_prod( x, y, operator = ":", fit = NULL, est = NULL, data = NULL, expand = FALSE )
get_prod( x, y, operator = ":", fit = NULL, est = NULL, data = NULL, expand = FALSE )
x |
Character. Variable name. |
y |
Character. Variable name. |
operator |
Character. The string
used to indicate a product term.
Default is |
fit |
The fit object. Currently
only supports a
lavaan::lavaan object.
It can also be
a |
est |
The output of
|
data |
Data frame (optional). If supplied, it will be used to identify the product terms. |
expand |
Whether products of
more than two terms will be searched.
|
This function is used
by several functions in manymome
to identify product terms along
a path. If possible, this is done
by numerically checking whether a
column in a dataset is the product
of two other columns. If not possible
(e.g., the "product term" is the
"product" of two latent variables,
formed by the products of indicators),
then it requires the user to specify
an operator.
The detailed workflow of this function can be found in the article https://sfcheung.github.io/manymome/articles/get_prod.html
This function is not intended to be used by users. It is exported such that advanced users or developers can use it.
If at least one product term is found, it returns a list with these elements:
prod
: The names of the product terms
found.
b
: The coefficients of these
product terms.
w
: The variable, other than
x
, in each product term.
x
: The x
-variable, that is,
where the path starts.
y
: The y
-variable, that is,
where the path ends.
It returns NA
if no product term
is found along the path.
dat <- modmed_x1m3w4y1 library(lavaan) mod <- " m1 ~ x + w1 + x:w1 m2 ~ m1 + w2 + m1:w2 m3 ~ m2 y ~ m3 + w4 + m3:w4 + x + w3 + x:w3 + x:w4 " fit <- sem(model = mod, data = dat, meanstructure = TRUE, fixed.x = FALSE) # One product term get_prod(x = "x", y = "m1", fit = fit) # Two product terms get_prod(x = "x", y = "y", fit = fit) # No product term get_prod(x = "m2", y = "m3", fit = fit)
dat <- modmed_x1m3w4y1 library(lavaan) mod <- " m1 ~ x + w1 + x:w1 m2 ~ m1 + w2 + m1:w2 m3 ~ m2 y ~ m3 + w4 + m3:w4 + x + w3 + x:w3 + x:w4 " fit <- sem(model = mod, data = dat, meanstructure = TRUE, fixed.x = FALSE) # One product term get_prod(x = "x", y = "m1", fit = fit) # Two product terms get_prod(x = "x", y = "y", fit = fit) # No product term get_prod(x = "m2", y = "m3", fit = fit)
It computes the index of moderated mediation and the index of moderated moderated mediation proposed by Hayes (2015, 2018).
index_of_mome( x, y, m = NULL, w = NULL, fit = NULL, boot_ci = FALSE, level = 0.95, boot_out = NULL, R = 100, seed = NULL, progress = TRUE, mc_ci = FALSE, mc_out = NULL, ci_type = NULL, ci_out = NULL, boot_type = c("perc", "bc"), ... ) index_of_momome( x, y, m = NULL, w = NULL, z = NULL, fit = NULL, boot_ci = FALSE, level = 0.95, boot_out = NULL, R = 100, seed = NULL, progress = TRUE, mc_ci = FALSE, mc_out = NULL, ci_type = NULL, ci_out = NULL, boot_type = c("perc", "bc"), ... )
index_of_mome( x, y, m = NULL, w = NULL, fit = NULL, boot_ci = FALSE, level = 0.95, boot_out = NULL, R = 100, seed = NULL, progress = TRUE, mc_ci = FALSE, mc_out = NULL, ci_type = NULL, ci_out = NULL, boot_type = c("perc", "bc"), ... ) index_of_momome( x, y, m = NULL, w = NULL, z = NULL, fit = NULL, boot_ci = FALSE, level = 0.95, boot_out = NULL, R = 100, seed = NULL, progress = TRUE, mc_ci = FALSE, mc_out = NULL, ci_type = NULL, ci_out = NULL, boot_type = c("perc", "bc"), ... )
x |
Character. The name of the predictor at the start of the path. |
y |
Character. The name of the outcome variable at the end of the path. |
m |
A vector of the variable
names of the mediator(s). The path
goes from the first mediator
successively to the last mediator. If
|
w |
Character. The name of the moderator. |
fit |
The fit object. Can be a
|
boot_ci |
Logical. Whether
bootstrap confidence interval will be
formed. Default is |
level |
The level of confidence for the bootstrap confidence interval. Default is .95. |
boot_out |
If |
R |
Integer. If |
seed |
If bootstrapping
or Monte Carlo simulation is
conducted, this is the seed for the
bootstrapping or simulation.
Default is |
progress |
Logical. Display
bootstrapping progress or not.
Default is |
mc_ci |
Logical. Whether
Monte Carlo confidence interval will be
formed. Default is |
mc_out |
If |
ci_type |
The type of
confidence intervals to be formed.
Can be either |
ci_out |
If |
boot_type |
If bootstrap
confidence interval is to be formed,
the type of bootstrap confidence
interval. The supported types
are |
... |
Arguments to be passed to
|
z |
Character. The name of the second moderator, for computing the index of moderated moderated mediation. |
The function
index_of_mome()
computes the index
of moderated mediation proposed by
Hayes (2015). It supports any path in
a model with one (and only one)
component path moderated. For
example, x->m1->m2->y
with x->m1
moderated by w
. It measures the
change in indirect effect when the
moderator increases by one unit.
The function index_of_momome()
computes the index of moderated
moderated mediation proposed by
Hayes (2018). It supports any path in
a model, with two component paths
moderated, each by one moderator. For
example, x->m1->m2->y
with x->m1
moderated by w
and m2->y
moderated by z
. It measures the
change in the index of moderated
mediation of one moderator when the
other moderator increases by one
unit.
It returns a
cond_indirect_diff
-class object.
This class has a print
method
(print.cond_indirect_diff()
), a
coef
method for extracting the
index (coef.cond_indirect_diff()
),
and a confint
method for extracting
the confidence interval if
available
(confint.cond_indirect_diff()
).
index_of_mome()
: Compute the
index of moderated mediation.
index_of_momome()
: Compute the
index of moderated moderated
mediation.
Hayes, A. F. (2015). An index and test of linear moderated mediation. Multivariate Behavioral Research, 50(1), 1-22. doi:10.1080/00273171.2014.962683
Hayes, A. F. (2018). Partial, conditional, and moderated moderated mediation: Quantification, inference, and interpretation. Communication Monographs, 85(1), 4-40. doi:10.1080/03637751.2017.1352100
library(lavaan) dat <- modmed_x1m3w4y1 dat$xw1 <- dat$x * dat$w1 mod <- " m1 ~ a * x + f * w1 + d * xw1 y ~ b * m1 + cp * x ind_mome := d * b " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # R should be at least 2000 or even 5000 in real research. # parallel is set to TRUE by default. # Therefore, in research, the argument parallel can be omitted. out_mome <- index_of_mome(x = "x", y = "y", m = "m1", w = "w1", fit = fit, boot_ci = TRUE, R = 42, seed = 4314, parallel = FALSE, progress = FALSE) out_mome coef(out_mome) # From lavaan print(est[19, ], nd = 8) confint(out_mome) library(lavaan) dat <- modmed_x1m3w4y1 dat$xw1 <- dat$x * dat$w1 dat$m1w4 <- dat$m1 * dat$w4 mod <- " m1 ~ a * x + f1 * w1 + d1 * xw1 y ~ b * m1 + f4 * w4 + d4 * m1w4 + cp * x ind_momome := d1 * d4 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # See the example of index_of_mome on how to request # bootstrap confidence interval. out_momome <- index_of_momome(x = "x", y = "y", m = "m1", w = "w1", z = "w4", fit = fit) out_momome coef(out_momome) print(est[32, ], nd = 8)
library(lavaan) dat <- modmed_x1m3w4y1 dat$xw1 <- dat$x * dat$w1 mod <- " m1 ~ a * x + f * w1 + d * xw1 y ~ b * m1 + cp * x ind_mome := d * b " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # R should be at least 2000 or even 5000 in real research. # parallel is set to TRUE by default. # Therefore, in research, the argument parallel can be omitted. out_mome <- index_of_mome(x = "x", y = "y", m = "m1", w = "w1", fit = fit, boot_ci = TRUE, R = 42, seed = 4314, parallel = FALSE, progress = FALSE) out_mome coef(out_mome) # From lavaan print(est[19, ], nd = 8) confint(out_mome) library(lavaan) dat <- modmed_x1m3w4y1 dat$xw1 <- dat$x * dat$w1 dat$m1w4 <- dat$m1 * dat$w4 mod <- " m1 ~ a * x + f1 * w1 + d1 * xw1 y ~ b * m1 + f4 * w4 + d4 * m1w4 + cp * x ind_momome := d1 * d4 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # See the example of index_of_mome on how to request # bootstrap confidence interval. out_momome <- index_of_momome(x = "x", y = "y", m = "m1", w = "w1", z = "w4", fit = fit) out_momome coef(out_momome) print(est[32, ], nd = 8)
Create a coefficient table
for the point estimates and
confidence intervals (if available)
in the
output of many_indirect_effects()
.
indirect_effects_from_list(object, add_sig = TRUE, pvalue = FALSE, se = FALSE)
indirect_effects_from_list(object, add_sig = TRUE, pvalue = FALSE, se = FALSE)
object |
The output of
|
add_sig |
Whether a column
of significance test results
will be added. Default is |
pvalue |
Logical. If |
se |
Logical. If |
If bootstrapping confidence interval was requested, this method has the option to add p-values computed by the method presented in Asparouhov and Muthén (2021). Note that these p-values is asymmetric bootstrap p-values based on the distribution of the bootstrap estimates. They are not computed based on the distribution under the null hypothesis.
For a p-value of a, it means that a 100(1 - a)% bootstrapping confidence interval will have one of its limits equal to 0. A confidence interval with a higher confidence level will include zero, while a confidence interval with a lower confidence level will exclude zero.
A data frame with the
indirect effect estimates and
confidence intervals (if available).
It also has A string column, "Sig"
,
for #' significant test results
if add_sig
is TRUE
and
confidence intervals are available.
Asparouhov, A., & Muthén, B. (2021). Bootstrap p-value computation. Retrieved from https://www.statmodel.com/download/FAQ-Bootstrap%20-%20Pvalue.pdf
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths from x to y paths <- all_indirect_paths(fit, x = "x", y = "y") paths # Indirect effect estimates out <- many_indirect_effects(paths, fit = fit) out # Create a data frame of the indirect effect estimates out_df <- indirect_effects_from_list(out) out_df
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths from x to y paths <- all_indirect_paths(fit, x = "x", y = "y") paths # Indirect effect estimates out <- many_indirect_effects(paths, fit = fit) out # Create a data frame of the indirect effect estimates out_df <- indirect_effects_from_list(out) out_df
It computes an indirect effect, optionally conditional on the value(s) of moderator(s) if present.
indirect_i( x, y, m = NULL, fit = NULL, est = NULL, implied_stats = NULL, wvalues = NULL, standardized_x = FALSE, standardized_y = FALSE, computation_digits = 5, prods = NULL, get_prods_only = FALSE, data = NULL, expand = TRUE, warn = TRUE, allow_mixing_lav_and_obs = TRUE, group = NULL, est_vcov = NULL, df_residual = NULL )
indirect_i( x, y, m = NULL, fit = NULL, est = NULL, implied_stats = NULL, wvalues = NULL, standardized_x = FALSE, standardized_y = FALSE, computation_digits = 5, prods = NULL, get_prods_only = FALSE, data = NULL, expand = TRUE, warn = TRUE, allow_mixing_lav_and_obs = TRUE, group = NULL, est_vcov = NULL, df_residual = NULL )
x |
Character. The name of the predictor at the start of the path. |
y |
Character. The name of the outcome variable at the end of the path. |
m |
A vector of the variable
names of the mediator(s). The path
goes from the first mediator
successively to the last mediator. If
|
fit |
The fit object. Currently
only supports lavaan::lavaan
objects. Support for lists of |
est |
The output of
|
implied_stats |
Implied means,
variances, and covariances of
observed variables and latent
variables (if any), of the form of
the output of |
wvalues |
A numeric vector of
named elements. The names are the
variable names of the moderators, and
the values are the values to which
the moderators will be set to.
Default is |
standardized_x |
Logical.
Whether |
standardized_y |
Logical.
Whether |
computation_digits |
The number of digits in storing the computation in text. Default is 3. |
prods |
The product terms found. For internal use. |
get_prods_only |
IF |
data |
Data frame (optional). If supplied, it will be used to identify the product terms. For internal use. |
expand |
Whether products of
more than two terms will be searched.
|
warn |
If |
allow_mixing_lav_and_obs |
If
|
group |
Either the group number
as appeared in the |
est_vcov |
A list of
variance-covariance matrix of
estimates, one for each response
variable ( |
df_residual |
A numeric
vector of the residual degrees of
freedom for the model of each
response variable ( |
This function is a low-level
function called by
indirect_effect()
,
cond_indirect_effects()
, and
cond_indirect()
, which call this
function multiple times if bootstrap
confidence interval is requested.
This function usually should not be used directly. It is exported for advanced users and developers
It returns an
indirect
-class object. This class
has the following methods:
coef.indirect()
,
print.indirect()
. The
confint.indirect()
method is used
only when called by cond_indirect()
or cond_indirect_effects()
.
indirect_effect()
,
cond_indirect_effects()
, and
cond_indirect()
, the high level
functions that should usually be
used.
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ a1 * x + b1 * w1 + d1 * x:w1 m2 ~ a2 * m1 + b2 * w2 + d2 * m1:w2 m3 ~ a3 * m2 + b3 * w3 + d3 * m2:w3 y ~ a4 * m3 + b4 * w4 + d4 * m3:w4 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) wvalues <- c(w1 = 5, w2 = 4, w3 = 2, w4 = 3) # Compute the conditional indirect effect by indirect_i() indirect_1 <- indirect_i(x = "x", y = "y", m = c("m1", "m2", "m3"), fit = fit, wvalues = wvalues) # Manually compute the conditional indirect effect indirect_2 <- (est[est$label == "a1", "est"] + wvalues["w1"] * est[est$label == "d1", "est"]) * (est[est$label == "a2", "est"] + wvalues["w2"] * est[est$label == "d2", "est"]) * (est[est$label == "a3", "est"] + wvalues["w3"] * est[est$label == "d3", "est"]) * (est[est$label == "a4", "est"] + wvalues["w4"] * est[est$label == "d4", "est"]) # They should be the same coef(indirect_1) indirect_2
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ a1 * x + b1 * w1 + d1 * x:w1 m2 ~ a2 * m1 + b2 * w2 + d2 * m1:w2 m3 ~ a3 * m2 + b3 * w3 + d3 * m2:w3 y ~ a4 * m3 + b4 * w4 + d4 * m3:w4 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) wvalues <- c(w1 = 5, w2 = 4, w3 = 2, w4 = 3) # Compute the conditional indirect effect by indirect_i() indirect_1 <- indirect_i(x = "x", y = "y", m = c("m1", "m2", "m3"), fit = fit, wvalues = wvalues) # Manually compute the conditional indirect effect indirect_2 <- (est[est$label == "a1", "est"] + wvalues["w1"] * est[est$label == "d1", "est"]) * (est[est$label == "a2", "est"] + wvalues["w2"] * est[est$label == "d2", "est"]) * (est[est$label == "a3", "est"] + wvalues["w3"] * est[est$label == "d3", "est"]) * (est[est$label == "a4", "est"] + wvalues["w4"] * est[est$label == "d4", "est"]) # They should be the same coef(indirect_1) indirect_2
It computes the proportion of effect mediated along a pathway.
indirect_proportion(x, y, m = NULL, fit = NULL)
indirect_proportion(x, y, m = NULL, fit = NULL)
x |
The name of the |
y |
The name of the |
m |
A vector of the variable
names of the mediator(s). The path
goes from the first mediator
successively to the last mediator.
Cannot be |
fit |
The fit object. Can be a
lavaan::lavaan object or a
list of |
The proportion of effect
mediated along a path from x
to
y
is the indirect effect along
this path divided by the total
effect from x
to y
(Alwin & Hauser, 1975). This total
effect is equal to the sum of all
indirect effects from x
to y
and the direct effect from x
to
y
.
To ensure that the proportion can
indeed be interpreted as a proportion,
this function computes the the
proportion only if the signs of
all the indirect and direct effects
from x
to y
are same (i.e., all effects
positive or all effects negative).
An indirect_proportion
class object.
It is a list-like object with these
major elements:
proportion
: The proportion of
effect mediated.
x
: The name of the x
-variable.
y
: The name of the y
-variable.
m
: A character vector of the
mediator(s) along a path. The path
runs from the first element to the
last element.
This class has a print
method
and a coef
method.
Alwin, D. F., & Hauser, R. M. (1975). The decomposition of effects in path analysis. American Sociological Review, 40(1), 37. doi:10.2307/2094445
print.indirect_proportion()
for the print
method, and
coef.indirect_proportion()
for
the coef
method.
library(lavaan) dat <- data_med head(dat) mod <- " m ~ x + c1 + c2 y ~ m + x + c1 + c2 " fit <- sem(mod, dat, fixed.x = FALSE) out <- indirect_proportion(x = "x", y = "y", m = "m", fit = fit) out
library(lavaan) dat <- data_med head(dat) mod <- " m ~ x + c1 + c2 y ~ m + x + c1 + c2 " fit <- sem(mod, dat, fixed.x = FALSE) out <- indirect_proportion(x = "x", y = "y", m = "m", fit = fit) out
Converts the regression
models in a lavaan
-class model to
an lm_from_lavaan_list
-class
object.
lm_from_lavaan_list(fit)
lm_from_lavaan_list(fit)
fit |
A |
It identifies all dependent
variables in a lavaan
model and
creates an lm_from_lavaan
-class
object for each of them.
This is an advanced helper used by
plot.cond_indirect_effects()
.
Exported for advanced users and
developers.
An
lm_from_lavaan_list
-class object,
which is a list of lm_from_lavaan
objects. It has a predict
-method
(predict.lm_from_lavaan_list()
) for
computing the predicted values from
one variable to another.
library(lavaan) data(data_med) mod <- " m ~ a * x + c1 + c2 y ~ b * m + x + c1 + c2 " fit <- sem(mod, data_med, fixed.x = FALSE) fit_list <- lm_from_lavaan_list(fit) tmp <- data.frame(x = 1, c1 = 2, c2 = 3, m = 4) predict(fit_list, x = "x", y = "y", m = "m", newdata = tmp)
library(lavaan) data(data_med) mod <- " m ~ a * x + c1 + c2 y ~ b * m + x + c1 + c2 " fit <- sem(mod, data_med, fixed.x = FALSE) fit_list <- lm_from_lavaan_list(fit) tmp <- data.frame(x = 1, c1 = 2, c2 = 3, m = 4) predict(fit_list, x = "x", y = "y", m = "m", newdata = tmp)
lm
OutputsGenerate bootstrap estimates for models in a list of 'lm' outputs.
lm2boot_out(outputs, R = 100, seed = NULL, progress = TRUE) lm2boot_out_parallel( outputs, R = 100, seed = NULL, parallel = FALSE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE )
lm2boot_out(outputs, R = 100, seed = NULL, progress = TRUE) lm2boot_out_parallel( outputs, R = 100, seed = NULL, parallel = FALSE, ncores = max(parallel::detectCores(logical = FALSE) - 1, 1), make_cluster_args = list(), progress = TRUE )
outputs |
A list of |
R |
The number of bootstrap samples. Default is 100. |
seed |
The seed for the random
resampling. Default is |
progress |
Logical. Display
progress or not. Default is |
parallel |
Logical. Whether
parallel processing will be used.
Default is |
ncores |
Integer. The number of
CPU cores to use when |
make_cluster_args |
A named list
of additional arguments to be passed
to |
This function is for
advanced users. do_boot()
is a
function users should try first
because do_boot()
has a general
interface for input-specific
functions like this one.
It does nonparametric bootstrapping
to generate bootstrap estimates of
the regression coefficients in the
regression models of a list of lm()
outputs, or an lm_list
-class object
created by lm2list()
. The stored
estimates can be used by
indirect_effect()
,
cond_indirect_effects()
, and
related functions in forming
bootstrapping confidence intervals
for effects such as indirect effect
and conditional indirect effects.
This approach removes the need to
repeat bootstrapping in each call to
indirect_effect()
,
cond_indirect_effects()
, and
related functions. It also ensures
that the same set of bootstrap
samples is used in all subsequent
analyses.
A boot_out
-class object
that can be used for the boot_out
argument of indirect_effect()
,
cond_indirect_effects()
, and
related functions for forming
bootstrapping confidence intervals.
The object is a list with the number
of elements equal to the number of
bootstrap samples. Each element is a
list of the parameter estimates and
sample variances and covariances of
the variables in each bootstrap
sample.
lm2boot_out()
: Generate
bootstrap estimates using one process
(serial, without parallelization).
lm2boot_out_parallel()
: Generate
bootstrap estimates using parallel
processing.
do_boot()
, the general
purpose function that users should
try first before using this function.
data(data_med_mod_ab1) dat <- data_med_mod_ab1 lm_m <- lm(m ~ x*w + c1 + c2, dat) lm_y <- lm(y ~ m*w + x + c1 + c2, dat) lm_out <- lm2list(lm_m, lm_y) # In real research, R should be 2000 or even 5000 # In real research, no need to set progress to FALSE # Progress is displayed by default. lm_boot_out <- lm2boot_out(lm_out, R = 100, seed = 1234, progress = FALSE) out <- cond_indirect_effects(wlevels = "w", x = "x", y = "y", m = "m", fit = lm_out, boot_ci = TRUE, boot_out = lm_boot_out) out
data(data_med_mod_ab1) dat <- data_med_mod_ab1 lm_m <- lm(m ~ x*w + c1 + c2, dat) lm_y <- lm(y ~ m*w + x + c1 + c2, dat) lm_out <- lm2list(lm_m, lm_y) # In real research, R should be 2000 or even 5000 # In real research, no need to set progress to FALSE # Progress is displayed by default. lm_boot_out <- lm2boot_out(lm_out, R = 100, seed = 1234, progress = FALSE) out <- cond_indirect_effects(wlevels = "w", x = "x", y = "y", m = "m", fit = lm_out, boot_ci = TRUE, boot_out = lm_boot_out) out
The resulting model can
be used by indirect_effect()
,
cond_indirect_effects()
, or
cond_indirect()
as a path method,
as if fitted by lavaan::sem()
.
lm2list(...)
lm2list(...)
... |
The |
If a path model with
mediation and/or moderation is fitted
by a set of regression models using
lm()
, this function can combine
them to an object of the class
lm_list
that represents a path
model, as one fitted by structural
equation model functions such as
lavaan::sem()
. This class of object
can be used by some functions, such
as indirect_effect()
,
cond_indirect_effects()
, and
cond_indirect()
as if they were the
output of fitting a path model, with
the regression coefficients treated
as path coefficients.
The regression outputs to be combined need to meet the following requirements:
All models must be connected to at least one another model. That is, a regression model must either have (a) at least on predictor that is the outcome variable of another model, or (b) its outcome variable the predictor of another model.
All models must be fitted to the same sample. This implies that the sample size must be the same in all analysis.
It returns an lm_list
-class
object that forms a path model
represented by a set of regression
models. This class has a summary
method that shows the summary of
each regression model stored (see
summary.lm_list()
), and a print
method that prints the models stored
(see print.lm_list()
).
summary.lm_list()
and
print.lm_list()
for related
methods, indirect_effect()
and
cond_indirect_effects()
which
accept lm_list
-class objects as
input.
data(data_serial_parallel) lm_m11 <- lm(m11 ~ x + c1 + c2, data_serial_parallel) lm_m12 <- lm(m12 ~ m11 + x + c1 + c2, data_serial_parallel) lm_m2 <- lm(m2 ~ x + c1 + c2, data_serial_parallel) lm_y <- lm(y ~ m11 + m12 + m2 + x + c1 + c2, data_serial_parallel) # Join them to form a lm_list-class object lm_serial_parallel <- lm2list(lm_m11, lm_m12, lm_m2, lm_y) lm_serial_parallel summary(lm_serial_parallel) # Compute indirect effect from x to y through m11 and m12 outm11m12 <- cond_indirect(x = "x", y = "y", m = c("m11", "m12"), fit = lm_serial_parallel) outm11m12 # Compute indirect effect from x to y # through m11 and m12 with bootstrapping CI # R should be at least 2000 or even 5000 in read study. # In real research, parallel and progress can be omitted. # They are est to TRUE by default. outm11m12 <- cond_indirect(x = "x", y = "y", m = c("m11", "m12"), fit = lm_serial_parallel, boot_ci = TRUE, R = 100, seed = 1234, parallel = FALSE, progress = FALSE) outm11m12
data(data_serial_parallel) lm_m11 <- lm(m11 ~ x + c1 + c2, data_serial_parallel) lm_m12 <- lm(m12 ~ m11 + x + c1 + c2, data_serial_parallel) lm_m2 <- lm(m2 ~ x + c1 + c2, data_serial_parallel) lm_y <- lm(y ~ m11 + m12 + m2 + x + c1 + c2, data_serial_parallel) # Join them to form a lm_list-class object lm_serial_parallel <- lm2list(lm_m11, lm_m12, lm_m2, lm_y) lm_serial_parallel summary(lm_serial_parallel) # Compute indirect effect from x to y through m11 and m12 outm11m12 <- cond_indirect(x = "x", y = "y", m = c("m11", "m12"), fit = lm_serial_parallel) outm11m12 # Compute indirect effect from x to y # through m11 and m12 with bootstrapping CI # R should be at least 2000 or even 5000 in read study. # In real research, parallel and progress can be omitted. # They are est to TRUE by default. outm11m12 <- cond_indirect(x = "x", y = "y", m = c("m11", "m12"), fit = lm_serial_parallel, boot_ci = TRUE, R = 100, seed = 1234, parallel = FALSE, progress = FALSE) outm11m12
Mathematic operators for
'indirect'-class object, the output
of indirect_effect()
and
cond_indirect()
.
## S3 method for class 'indirect' e1 + e2 ## S3 method for class 'indirect' e1 - e2
## S3 method for class 'indirect' e1 + e2 ## S3 method for class 'indirect' e1 - e2
e1 |
An 'indirect'-class object. |
e2 |
An 'indirect'-class object. |
For now, only +
operator
and -
operator are supported. These
operators can be used to estimate and
test a function of effects between
the same pair of variables.
For example, they can be used to compute and test the total effects along different paths. They can also be used to compute and test the difference between the effects along two paths.
The operators will check whether an operation is valid. An operation is not valid if
the two paths do not start from the same variable,
the two paths do not end at the same variable,
moderators are involved but they are not set to the same values in both objects, and
bootstrap estimates stored in
boot_out
, if any, are not identical.
Monte Carlo simulated
estimates stored in
mc_out
, if any, are not identical.
If bootstrap estimates are stored and both objects used the same type of bootstrap confidence interval, that type will be used. Otherwise, percentile bootstrap confidence interval, the recommended method, will be used.
Since Version 0.1.14.2, support for
multigroup models has been added for models
fitted by lavaan
. Both bootstrapping
and Monte Carlo confidence intervals
are supported. These operators can
be used to compute and test the
difference of an indirect effect
between two groups. This can also
be used to compute and test the
difference between a function of
effects between groups, for example,
the total indirect effects between
two groups.
The operators are flexible and allow users to do many possible computations. Therefore, users need to make sure that the function of effects is meaningful.
An 'indirect'-class object
with a list of effects stored. See
indirect_effect()
on details for
this class.
indirect_effect()
and
cond_indirect()
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ a1 * x + d1 * w1 + e1 * x:w1 m2 ~ m1 + a2 * x y ~ b1 * m1 + b2 * m2 + cp * x " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) hi_w1 <- mean(dat$w1) + sd(dat$w1) # Examples for cond_indirect(): # Conditional effect from x to m1 when w1 is 1 SD above mean out1 <- cond_indirect(x = "x", y = "y", m = c("m1", "m2"), wvalues = c(w1 = hi_w1), fit = fit) out2 <- cond_indirect(x = "x", y = "y", m = c("m2"), wvalues = c(w1 = hi_w1), fit = fit) out3 <- cond_indirect(x = "x", y = "y", wvalues = c(w1 = hi_w1), fit = fit) out12 <- out1 + out2 out12 out123 <- out1 + out2 + out3 out123 coef(out1) + coef(out2) + coef(out3) # Multigroup model with indirect effects dat <- data_med_mg mod <- " m ~ x + c1 + c2 y ~ m + x + c1 + c2 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE, group = "group") # If a model has more than one group, # the argument 'group' must be set. ind1 <- indirect_effect(x = "x", y = "y", m = "m", fit = fit, group = "Group A") ind1 ind2 <- indirect_effect(x = "x", y = "y", m = "m", fit = fit, group = 2) ind2 # Compute the difference in indirect effects between groups ind2 - ind1
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ a1 * x + d1 * w1 + e1 * x:w1 m2 ~ m1 + a2 * x y ~ b1 * m1 + b2 * m2 + cp * x " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) hi_w1 <- mean(dat$w1) + sd(dat$w1) # Examples for cond_indirect(): # Conditional effect from x to m1 when w1 is 1 SD above mean out1 <- cond_indirect(x = "x", y = "y", m = c("m1", "m2"), wvalues = c(w1 = hi_w1), fit = fit) out2 <- cond_indirect(x = "x", y = "y", m = c("m2"), wvalues = c(w1 = hi_w1), fit = fit) out3 <- cond_indirect(x = "x", y = "y", wvalues = c(w1 = hi_w1), fit = fit) out12 <- out1 + out2 out12 out123 <- out1 + out2 + out3 out123 coef(out1) + coef(out2) + coef(out3) # Multigroup model with indirect effects dat <- data_med_mg mod <- " m ~ x + c1 + c2 y ~ m + x + c1 + c2 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE, group = "group") # If a model has more than one group, # the argument 'group' must be set. ind1 <- indirect_effect(x = "x", y = "y", m = "m", fit = fit, group = "Group A") ind1 ind2 <- indirect_effect(x = "x", y = "y", m = "m", fit = fit, group = 2) ind2 # Compute the difference in indirect effects between groups ind2 - ind1
Merge the levels of
moderators generated by
mod_levels()
into a data frame.
merge_mod_levels(...)
merge_mod_levels(...)
... |
The output from
|
It merges the levels of
moderators generated by
mod_levels()
into a data frame,
with each row represents a
combination of the levels. The output
is to be used by
cond_indirect_effects()
.
Users usually do not need to use this
function because
cond_indirect_effects()
will merge
the levels internally if necessary.
This function is used when users need
to customize the levels for each
moderator and so cannot use
mod_levels_list()
or the default
levels in cond_indirect_effects()
.
A wlevels
-class object,
which is a data frame of the
combinations of levels, with
additional attributes about the
levels.
mod_levels()
on generating
the levels of a moderator.
data(data_med_mod_ab) dat <- data_med_mod_ab # Form the levels from a list of lm() outputs lm_m <- lm(m ~ x*w1 + c1 + c2, dat) lm_y <- lm(y ~ m*w2 + x + w1 + c1 + c2, dat) lm_out <- lm2list(lm_m, lm_y) w1_levels <- mod_levels(lm_out, w = "w1") w1_levels w2_levels <- mod_levels(lm_out, w = "w2") w2_levels merge_mod_levels(w1_levels, w2_levels)
data(data_med_mod_ab) dat <- data_med_mod_ab # Form the levels from a list of lm() outputs lm_m <- lm(m ~ x*w1 + c1 + c2, dat) lm_y <- lm(y ~ m*w2 + x + w1 + c1 + c2, dat) lm_out <- lm2list(lm_m, lm_y) w1_levels <- mod_levels(lm_out, w = "w1") w1_levels w2_levels <- mod_levels(lm_out, w = "w2") w2_levels merge_mod_levels(w1_levels, w2_levels)
Create levels of
moderators to be used by
indirect_effect()
,
cond_indirect_effects()
, and
cond_indirect()
.
mod_levels( w, fit, w_type = c("auto", "numeric", "categorical"), w_method = c("sd", "percentile"), sd_from_mean = c(-1, 0, 1), percentiles = c(0.16, 0.5, 0.84), extract_gp_names = TRUE, prefix = NULL, values = NULL, reference_group_label = NULL, descending = TRUE ) mod_levels_list( ..., fit, w_type = "auto", w_method = "sd", sd_from_mean = NULL, percentiles = NULL, extract_gp_names = TRUE, prefix = NULL, descending = TRUE, merge = FALSE )
mod_levels( w, fit, w_type = c("auto", "numeric", "categorical"), w_method = c("sd", "percentile"), sd_from_mean = c(-1, 0, 1), percentiles = c(0.16, 0.5, 0.84), extract_gp_names = TRUE, prefix = NULL, values = NULL, reference_group_label = NULL, descending = TRUE ) mod_levels_list( ..., fit, w_type = "auto", w_method = "sd", sd_from_mean = NULL, percentiles = NULL, extract_gp_names = TRUE, prefix = NULL, descending = TRUE, merge = FALSE )
w |
Character. The names of the moderator. If the moderator is categorical with 3 or more groups, this is the vector of the indicator variables. |
fit |
The fit object. Can be a
lavaan::lavaan object or a
list of |
w_type |
Character. Whether the
moderator is a |
w_method |
Character, either
|
sd_from_mean |
A numeric vector.
Specify the distance in standard
deviation from the mean for each
level. Default is |
percentiles |
A numeric vector.
Specify the percentile (in
proportion) for each level. Default
is |
extract_gp_names |
Logical. If
|
prefix |
Character. If
|
values |
For numeric moderators,
a numeric vector. These are the
values to be used and will override
other options. For categorical
moderators, a named list of numeric
vector, each vector has length equal
to the number of indicator variables.
If the vector is named, the names
will be used to label the values. For
example, if set to |
reference_group_label |
For
categorical moderator, if the label
for the reference group (group with
all indicators equal to zero) cannot
be determined, the default label is
|
descending |
If |
... |
The names of moderators variables. For a categorical variable, it should be a vector of variable names. |
merge |
If |
It creates values of a moderator that can be used to compute conditional effect or conditional indirect effect. By default, for a numeric moderator, it uses one standard deviation below mean, mean, and one standard deviation above mean. The percentiles of these three levels in a normal distribution (16th, 50th, and 84th) can also be used. For categorical variable, it will simply collect the unique categories in the data.
The generated levels are then used by
cond_indirect()
and
cond_indirect_effects()
.
If a model has more than one
moderator, mod_levels_list()
can be
used to generate combinations of
levels. The output can then passed to
cond_indirect_effects()
to compute
the conditional effects or
conditional indirect effects for all
the combinations.
mod_levels()
returns a
wlevels
-class object which is a
data frame with additional attributes
about the levels.
mod_levels_list()
returns a list of
wlevels
-class objects, or a
wlevels
-class object which is a
data frame of the merged levels if
merge = TRUE
.
mod_levels()
: Generate
levels for one moderator.
mod_levels_list()
: Generate
levels for several moderators.
cond_indirect_effects()
for computing conditional
indiret effects; merge_mod_levels()
for merging
levels of moderators.
library(lavaan) data(data_med_mod_ab) dat <- data_med_mod_ab # Form the levels from a list of lm() outputs lm_m <- lm(m ~ x*w1 + c1 + c2, dat) lm_y <- lm(y ~ m*w2 + x + w1 + c1 + c2, dat) lm_out <- lm2list(lm_m, lm_y) w1_levels <- mod_levels(lm_out, w = "w1") w1_levels w2_levels <- mod_levels(lm_out, w = "w2") w2_levels # Indirect effect from x to y through m, at the first levels of w1 and w2 cond_indirect(x = "x", y = "y", m = "m", fit = lm_out, wvalues = c(w1 = w1_levels$w1[1], w2 = w2_levels$w2[1])) # Can form the levels based on percentiles w1_levels2 <- mod_levels(lm_out, w = "w1", w_method = "percentile") w1_levels2 # Form the levels from a lavaan output # Compute the product terms before fitting the model dat$mw2 <- dat$m * dat$w2 mod <- " m ~ x + w1 + x:w1 + c1 + c2 y ~ m + x + w1 + w2 + mw2 + c1 + c2 " fit <- sem(mod, dat, fixed.x = FALSE) cond_indirect(x = "x", y = "y", m = "m", fit = fit, wvalues = c(w1 = w1_levels$w1[1], w2 = w2_levels$w2[1])) # Can pass all levels to cond_indirect_effects() # First merge the levels by merge_mod_levels() w1w2_levels <- merge_mod_levels(w1_levels, w2_levels) cond_indirect_effects(x = "x", y = "y", m = "m", fit = fit, wlevels = w1w2_levels) # mod_levels_list() forms a combinations of levels in one call # It returns a list, by default. # Form the levels from a list of lm() outputs # "merge = TRUE" is optional. cond_indirect_effects will merge the levels # automatically. w1w2_levels <- mod_levels_list("w1", "w2", fit = fit, merge = TRUE) w1w2_levels cond_indirect_effects(x = "x", y = "y", m = "m", fit = fit, wlevels = w1w2_levels) # Can work without merge = TRUE: w1w2_levels <- mod_levels_list("w1", "w2", fit = fit) w1w2_levels cond_indirect_effects(x = "x", y = "y", m = "m", fit = fit, wlevels = w1w2_levels)
library(lavaan) data(data_med_mod_ab) dat <- data_med_mod_ab # Form the levels from a list of lm() outputs lm_m <- lm(m ~ x*w1 + c1 + c2, dat) lm_y <- lm(y ~ m*w2 + x + w1 + c1 + c2, dat) lm_out <- lm2list(lm_m, lm_y) w1_levels <- mod_levels(lm_out, w = "w1") w1_levels w2_levels <- mod_levels(lm_out, w = "w2") w2_levels # Indirect effect from x to y through m, at the first levels of w1 and w2 cond_indirect(x = "x", y = "y", m = "m", fit = lm_out, wvalues = c(w1 = w1_levels$w1[1], w2 = w2_levels$w2[1])) # Can form the levels based on percentiles w1_levels2 <- mod_levels(lm_out, w = "w1", w_method = "percentile") w1_levels2 # Form the levels from a lavaan output # Compute the product terms before fitting the model dat$mw2 <- dat$m * dat$w2 mod <- " m ~ x + w1 + x:w1 + c1 + c2 y ~ m + x + w1 + w2 + mw2 + c1 + c2 " fit <- sem(mod, dat, fixed.x = FALSE) cond_indirect(x = "x", y = "y", m = "m", fit = fit, wvalues = c(w1 = w1_levels$w1[1], w2 = w2_levels$w2[1])) # Can pass all levels to cond_indirect_effects() # First merge the levels by merge_mod_levels() w1w2_levels <- merge_mod_levels(w1_levels, w2_levels) cond_indirect_effects(x = "x", y = "y", m = "m", fit = fit, wlevels = w1w2_levels) # mod_levels_list() forms a combinations of levels in one call # It returns a list, by default. # Form the levels from a list of lm() outputs # "merge = TRUE" is optional. cond_indirect_effects will merge the levels # automatically. w1w2_levels <- mod_levels_list("w1", "w2", fit = fit, merge = TRUE) w1w2_levels cond_indirect_effects(x = "x", y = "y", m = "m", fit = fit, wlevels = w1w2_levels) # Can work without merge = TRUE: w1w2_levels <- mod_levels_list("w1", "w2", fit = fit) w1w2_levels cond_indirect_effects(x = "x", y = "y", m = "m", fit = fit, wlevels = w1w2_levels)
Generated from a serial mediation model with one predictor, three mediators, and one outcome variable, with one moderator in each stage.
modmed_x1m3w4y1
modmed_x1m3w4y1
A data frame with 200 rows and 11 variables:
Predictor. Numeric.
Moderator 1. Numeric.
Moderator 2. Numeric.
Moderator 3. Numeric.
Moderator 4. Numeric.
Mediator 1. Numeric.
Mediator 2. Numeric.
Mediator 3. Numeric.
Outcome variable. Numeric.
Three values: "earth", "mars", "venus". String.
Four values: "alpha", "beta", "gamma", "sigma". String.
It plots an effect, direct or indirect, against a moderator, with confidence band if available.
plot_effect_vs_w( object, w = NULL, w_label = NULL, effect_label = NULL, add_zero_line = TRUE, always_draw_zero_line = FALSE, line_linewidth = 1, line_color = "blue", shade_the_band = TRUE, draw_the_intervals = TRUE, band_fill_color = "lightgrey", band_alpha = 0.5, intervals_color = "black", intervals_linetype = "longdash", intervals_linewidth = 1, zero_line_color = "grey", zero_line_linewidth = 1, zero_line_linetype = "solid", line_args = list(), band_args = list(), intervals_args = list(), zero_line_args = list(), level = 0.95 ) fill_wlevels(to_fill, cond_out = NULL, k = 21)
plot_effect_vs_w( object, w = NULL, w_label = NULL, effect_label = NULL, add_zero_line = TRUE, always_draw_zero_line = FALSE, line_linewidth = 1, line_color = "blue", shade_the_band = TRUE, draw_the_intervals = TRUE, band_fill_color = "lightgrey", band_alpha = 0.5, intervals_color = "black", intervals_linetype = "longdash", intervals_linewidth = 1, zero_line_color = "grey", zero_line_linewidth = 1, zero_line_linetype = "solid", line_args = list(), band_args = list(), intervals_args = list(), zero_line_args = list(), level = 0.95 ) fill_wlevels(to_fill, cond_out = NULL, k = 21)
object |
The output of
|
w |
The name of the moderator.
Must be present in |
w_label |
The label of the
horizontal axis. If |
effect_label |
The label of the
vertical axis. If |
add_zero_line |
Whether a
horizontal line at zero will be drawn.
Default is |
always_draw_zero_line |
If |
line_linewidth |
The width of
the line of the effect for each
level of the moderator, to be used
by |
line_color |
The color of the line
of the effect for each level of the
moderator, to be used
by |
shade_the_band |
If |
draw_the_intervals |
If |
band_fill_color |
The color of
of the confidence band, to be used
by |
band_alpha |
A number from
0 to 1 for the level
of transparency
of the confidence band, to be used
by |
intervals_color |
The color of
the lines of the confidence intervals,
to be used
by |
intervals_linetype |
The line
type of
the lines of the confidence intervals,
to be used
by |
intervals_linewidth |
The line
width of
the lines of the confidence intervals,
to be used
by |
zero_line_color |
The color of
the line at zero,
to be used
by |
zero_line_linewidth |
The line
width of
the line at zero,
to be used
by |
zero_line_linetype |
The line
type of
the line at zero,
to be used
by |
line_args |
A named list of
additional arguments to be passed
to |
band_args |
A named list of
additional arguments to be passed
to |
intervals_args |
A named list of
additional arguments to be passed
to |
zero_line_args |
A named list of
additional arguments to be passed
to |
level |
The level of confidence
for the confidence intervals computed
from the original standard errors. Used only for
paths without mediators and both
|
to_fill |
The output of
|
cond_out |
If |
k |
The desired number of levels of the moderator. |
It receives an output
of cond_indirect_effects()
and
plot the effect against the
moderator. The effect can be an
indirect effect or a direct effect.
It uses the levels of the moderator
stored in the output of
cond_indirect_effects()
. Therefore,
the desired levels of the moderator
to be plotted needs to be specified
when calling cond_indirect_effects()
,
as illustrated in the example.
Currently, this function only supports a path with exactly one moderator, and the moderator is a numeric variable.
If the following conditions are met, the stored standard errors, if available, will be used to form the confidence intervals:
Confidence intervals have not been formed (e.g., by bootstrapping or Monte Carlo).
The path has no mediators.
The model has only one group.
The path is moderated by one or more moderator.
Both the x
-variable and the
y
-variable are not standardized.
If the model is fitted by OLS
regression (e.g., using stats::lm()
),
then the variance-covariance matrix
of the coefficient estimates will be
used, and confidence
intervals are computed from the t
statistic.
If the model is fitted by structural
equation modeling using lavaan
, then
the variance-covariance computed by
lavaan
will be used,
and confidence intervals are computed
from the z statistic.
If the model is fitted by structural equation modeling and has moderators, the standard errors, p-values, and confidence interval computed from the variance-covariance matrices for conditional effects can only be trusted if all covariances involving the product terms are free. If any of them are fixed, for example, fixed to zero, it is possible that the model is not invariant to linear transformation of the variables.
The function fill_wlevels()
is
a helper to automatically
fill in additional levels
of the moderators, to plot a graph
with smooth confidence band. It
accepts the output of cond_indirect_effects()
or pseudo_johnson_neyman()
, finds
the range of the values of the
moderator, and returns an output
of cond_indirect_effects()
with
the desired number of levels within
this range. It is intended to be a
helper. If it does not work, users
can still get the desired number of
levels by setting the values manually
when calling cond_indirect_effects()
.
plot_effect_vs_w()
returns a
ggplot2
graph. Plotted if
not assigned to a name. It can be
further modified like a usual
ggplot2
graph.
fill_wlevels()
returns an updated
output of cond_indirect_effects()
with the desired number of levels of
the moderator.
dat <- data_med_mod_a lm_m <- lm(m ~ x*w + c1 + c2, dat) lm_y <- lm(y ~ m + x + c1 + c2, dat) fit_lm <- lm2list(lm_m, lm_y) # Set R to a large value in real research. boot_out_lm <- do_boot(fit_lm, R = 50, seed = 54532, parallel = FALSE, progress = FALSE) # Compute the conditional indirect effects # from 2 SD below mean to 2 SD above mean of the moderator, # by setting sd_from_mean of cond_indirect_effects(). # Set length.out to a larger number for a smooth graph. out_lm <- cond_indirect_effects(wlevels = "w", x = "x", y = "y", m = "m", fit = fit_lm, sd_from_mean = seq(-2, 2, length.out = 10), boot_ci = TRUE, boot_out = boot_out_lm) p <- plot_effect_vs_w(out_lm) p # The output is a ggplot2 graph and so can be further customized library(ggplot2) # Add the line for the mean of w, the moderator p2 <- p + geom_vline(xintercept = mean(dat$w), color = "red") p2 # Use fill_wlevels to add moderator levels: dat <- data_med_mod_a lm_m <- lm(m ~ x*w + c1 + c2, dat) lm_y <- lm(y ~ m + x + c1 + c2, dat) fit_lm <- lm2list(lm_m, lm_y) wlevels <- mod_levels(w = "w", sd_from_mean = c(-3, 0, 3), fit = fit_lm) wlevels cond_out <- cond_indirect_effects(wlevels = wlevels, x = "x", y = "m", fit = fit_lm) cond_out # Only 3 points p1 <- plot_effect_vs_w(cond_out) p1 # Increase the number of levels to 15 cond_out_filled <- fill_wlevels(cond_out, k = 15) cond_out_filled p2 <- plot_effect_vs_w(cond_out_filled) p2
dat <- data_med_mod_a lm_m <- lm(m ~ x*w + c1 + c2, dat) lm_y <- lm(y ~ m + x + c1 + c2, dat) fit_lm <- lm2list(lm_m, lm_y) # Set R to a large value in real research. boot_out_lm <- do_boot(fit_lm, R = 50, seed = 54532, parallel = FALSE, progress = FALSE) # Compute the conditional indirect effects # from 2 SD below mean to 2 SD above mean of the moderator, # by setting sd_from_mean of cond_indirect_effects(). # Set length.out to a larger number for a smooth graph. out_lm <- cond_indirect_effects(wlevels = "w", x = "x", y = "y", m = "m", fit = fit_lm, sd_from_mean = seq(-2, 2, length.out = 10), boot_ci = TRUE, boot_out = boot_out_lm) p <- plot_effect_vs_w(out_lm) p # The output is a ggplot2 graph and so can be further customized library(ggplot2) # Add the line for the mean of w, the moderator p2 <- p + geom_vline(xintercept = mean(dat$w), color = "red") p2 # Use fill_wlevels to add moderator levels: dat <- data_med_mod_a lm_m <- lm(m ~ x*w + c1 + c2, dat) lm_y <- lm(y ~ m + x + c1 + c2, dat) fit_lm <- lm2list(lm_m, lm_y) wlevels <- mod_levels(w = "w", sd_from_mean = c(-3, 0, 3), fit = fit_lm) wlevels cond_out <- cond_indirect_effects(wlevels = wlevels, x = "x", y = "m", fit = fit_lm) cond_out # Only 3 points p1 <- plot_effect_vs_w(cond_out) p1 # Increase the number of levels to 15 cond_out_filled <- fill_wlevels(cond_out, k = 15) cond_out_filled p2 <- plot_effect_vs_w(cond_out_filled) p2
Plot the conditional effects for different levels of moderators.
## S3 method for class 'cond_indirect_effects' plot( x, x_label, w_label = "Moderator(s)", y_label, title, x_from_mean_in_sd = 1, x_method = c("sd", "percentile"), x_percentiles = c(0.16, 0.84), x_sd_to_percentiles = NA, note_standardized = TRUE, no_title = FALSE, line_width = 1, point_size = 5, graph_type = c("default", "tumble"), use_implied_stats = TRUE, facet_grid_cols = NULL, facet_grid_rows = NULL, facet_grid_args = list(as.table = FALSE, labeller = "label_both"), digits = 4, ... )
## S3 method for class 'cond_indirect_effects' plot( x, x_label, w_label = "Moderator(s)", y_label, title, x_from_mean_in_sd = 1, x_method = c("sd", "percentile"), x_percentiles = c(0.16, 0.84), x_sd_to_percentiles = NA, note_standardized = TRUE, no_title = FALSE, line_width = 1, point_size = 5, graph_type = c("default", "tumble"), use_implied_stats = TRUE, facet_grid_cols = NULL, facet_grid_rows = NULL, facet_grid_args = list(as.table = FALSE, labeller = "label_both"), digits = 4, ... )
x |
The output of
|
x_label |
The label for the
X-axis. Default is the value of the
predictor in the output of
|
w_label |
The label for the
legend for the lines. Default is
|
y_label |
The label for the Y-axis. Default is the name of the response variable in the model. |
title |
The title of the graph.
If not supplied, it will be generated
from the variable names or labels (in
|
x_from_mean_in_sd |
How many SD from mean is used to define "low" and "high" for the focal variable. Default is 1. |
x_method |
How to define "high"
and "low" for the focal variable
levels. Default is in terms of the
standard deviation of the focal
variable, |
x_percentiles |
If |
x_sd_to_percentiles |
If
|
note_standardized |
If |
no_title |
If |
line_width |
The width of the
lines as used in
|
point_size |
The size of the
points as used in
|
graph_type |
If |
use_implied_stats |
For a
multigroup model, if |
facet_grid_cols , facet_grid_rows
|
If either or both of them are set
to character vector(s) of moderator
names, then |
facet_grid_args |
The list of
arguments to be used in calling
|
digits |
The number of decimal
places to be printed for numerical
moderators when |
... |
Additional arguments. Ignored. |
This function is a plot
method of the output of
cond_indirect_effects()
. It will
use the levels of moderators in the
output.
It plots the conditional effect from
x
to y
in a model for different
levels of the moderators. For
multigroup models, the group will
be the 'moderator' and one line is
drawn for each group.
It does not support conditional
indirect effects. If there is one or
more mediators in x
, it will raise
an error.
Since Version 0.1.14.2, support for
multigroup models has been added for models
fitted by lavaan
. If the effect
for each group is drawn, the
graph_type
is automatically switched
to "tumble"
and the means and SDs
in each group will be used to determine
the locations of the points.
If the multigroup model has any equality
constraints, the implied means and/or
SDs may be different from those of
the raw data. For example, the mean
of the x
-variable may be constrained
to be equal in this model. To plot
the tumble graph using the model implied
means and SDs, set use_implied_stats
to TRUE
.
A path that involves a latent x
-variable
and/or a latent y
-variable can be
plotted. Because the latent variables
have no observed data, the model
implied statistics will always be used
to get the means and SDs to compute
values such as the low and high points
of the x
-variable.
A ggplot2
graph. Plotted if
not assigned to a name. It can be
further modified like a usual
ggplot2
graph.
Bodner, T. E. (2016). Tumble graphs: Avoiding misleading end point extrapolation when graphing interactions from a moderated multiple regression analysis. Journal of Educational and Behavioral Statistics, 41(6), 593-604. doi:10.3102/1076998616657080
library(lavaan) dat <- modmed_x1m3w4y1 n <- nrow(dat) set.seed(860314) dat$gp <- sample(c("gp1", "gp2", "gp3"), n, replace = TRUE) dat <- cbind(dat, factor2var(dat$gp, prefix = "gp", add_rownames = FALSE)) # Categorical moderator mod <- " m3 ~ m1 + x + gpgp2 + gpgp3 + x:gpgp2 + x:gpgp3 y ~ m2 + m3 + x " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE) out_mm_1 <- mod_levels(c("gpgp2", "gpgp3"), sd_from_mean = c(-1, 1), fit = fit) out_1 <- cond_indirect_effects(wlevels = out_mm_1, x = "x", y = "m3", fit = fit) plot(out_1) plot(out_1, graph_type = "tumble") # Numeric moderator dat <- modmed_x1m3w4y1 mod2 <- " m3 ~ m1 + x + w1 + x:w1 y ~ m3 + x " fit2 <- sem(mod2, dat, meanstructure = TRUE, fixed.x = FALSE) out_mm_2 <- mod_levels("w1", w_method = "percentile", percentiles = c(.16, .84), fit = fit2) out_mm_2 out_2 <- cond_indirect_effects(wlevels = out_mm_2, x = "x", y = "m3", fit = fit2) plot(out_2) plot(out_2, graph_type = "tumble") # Multigroup models dat <- data_med_mg mod <- " m ~ x + c1 + c2 y ~ m + x + c1 + c2 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE, group = "group") # For a multigroup model, group will be used as # a moderator out <- cond_indirect_effects(x = "m", y = "y", fit = fit) out plot(out)
library(lavaan) dat <- modmed_x1m3w4y1 n <- nrow(dat) set.seed(860314) dat$gp <- sample(c("gp1", "gp2", "gp3"), n, replace = TRUE) dat <- cbind(dat, factor2var(dat$gp, prefix = "gp", add_rownames = FALSE)) # Categorical moderator mod <- " m3 ~ m1 + x + gpgp2 + gpgp3 + x:gpgp2 + x:gpgp3 y ~ m2 + m3 + x " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE) out_mm_1 <- mod_levels(c("gpgp2", "gpgp3"), sd_from_mean = c(-1, 1), fit = fit) out_1 <- cond_indirect_effects(wlevels = out_mm_1, x = "x", y = "m3", fit = fit) plot(out_1) plot(out_1, graph_type = "tumble") # Numeric moderator dat <- modmed_x1m3w4y1 mod2 <- " m3 ~ m1 + x + w1 + x:w1 y ~ m3 + x " fit2 <- sem(mod2, dat, meanstructure = TRUE, fixed.x = FALSE) out_mm_2 <- mod_levels("w1", w_method = "percentile", percentiles = c(.16, .84), fit = fit2) out_mm_2 out_2 <- cond_indirect_effects(wlevels = out_mm_2, x = "x", y = "m3", fit = fit2) plot(out_2) plot(out_2, graph_type = "tumble") # Multigroup models dat <- data_med_mg mod <- " m ~ x + c1 + c2 y ~ m + x + c1 + c2 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE, group = "group") # For a multigroup model, group will be used as # a moderator out <- cond_indirect_effects(x = "m", y = "y", fit = fit) out plot(out)
Compute the predicted values based on the model stored in a 'lm_from_lavaan'-class object.
## S3 method for class 'lm_from_lavaan' predict(object, newdata, ...)
## S3 method for class 'lm_from_lavaan' predict(object, newdata, ...)
object |
A 'lm_from_lavaan'-class object. |
newdata |
Required. A data frame of the new data. It must be a data frame. |
... |
Additional arguments. Ignored. |
An lm_from_lavaan
-class
method that converts a regression
model for a variable in a lavaan
model to a formula
object. This
function uses the stored model to
compute predicted values using
user-supplied data.
This is an advanced helper used by
plot.cond_indirect_effects()
.
Exported for advanced users and
developers.
A numeric vector of the predicted values, with length equal to the number of rows of user-supplied data.
library(lavaan) data(data_med) mod <- " m ~ a * x + c1 + c2 y ~ b * m + x + c1 + c2 " fit <- sem(mod, data_med, fixed.x = FALSE) fit_list <- lm_from_lavaan_list(fit) tmp <- data.frame(x = 1, c1 = 2, c2 = 3, m = 4) predict(fit_list$m, newdata = tmp) predict(fit_list$y, newdata = tmp)
library(lavaan) data(data_med) mod <- " m ~ a * x + c1 + c2 y ~ b * m + x + c1 + c2 " fit <- sem(mod, data_med, fixed.x = FALSE) fit_list <- lm_from_lavaan_list(fit) tmp <- data.frame(x = 1, c1 = 2, c2 = 3, m = 4) predict(fit_list$m, newdata = tmp) predict(fit_list$y, newdata = tmp)
It computes the predicted values based on the models stored in an 'lm_from_lavaan_list'-class object.
## S3 method for class 'lm_from_lavaan_list' predict(object, x = NULL, y = NULL, m = NULL, newdata, ...)
## S3 method for class 'lm_from_lavaan_list' predict(object, x = NULL, y = NULL, m = NULL, newdata, ...)
object |
A 'lm_from_lavaan'-class object. |
x |
The variable name at the start of a path. |
y |
The variable name at the end of a path. |
m |
Optional. The mediator(s)
from |
newdata |
Required. A data frame of the new data. It must be a data frame. |
... |
Additional arguments. Ignored. |
An
lm_from_lavaan_list
-class object
is a list of lm_from_lavaan
-class
objects.
This is an advanced helper used by
plot.cond_indirect_effects()
.
Exported for advanced users and
developers.
A numeric vector of the predicted values, with length equal to the number of rows of user-supplied data.
library(lavaan) data(data_med) mod <- " m ~ a * x + c1 + c2 y ~ b * m + x + c1 + c2 " fit <- sem(mod, data_med, fixed.x = FALSE) fit_list <- lm_from_lavaan_list(fit) tmp <- data.frame(x = 1, c1 = 2, c2 = 3, m = 4) predict(fit_list, x = "x", y = "y", m = "m", newdata = tmp)
library(lavaan) data(data_med) mod <- " m ~ a * x + c1 + c2 y ~ b * m + x + c1 + c2 " fit <- sem(mod, data_med, fixed.x = FALSE) fit_list <- lm_from_lavaan_list(fit) tmp <- data.frame(x = 1, c1 = 2, c2 = 3, m = 4) predict(fit_list, x = "x", y = "y", m = "m", newdata = tmp)
Compute the predicted values based on the models stored in an 'lm_list'-class object.
## S3 method for class 'lm_list' predict(object, x = NULL, y = NULL, m = NULL, newdata, ...)
## S3 method for class 'lm_list' predict(object, x = NULL, y = NULL, m = NULL, newdata, ...)
object |
An 'lm_list'-class object. |
x |
The variable name at the start of a path. |
y |
The variable name at the end of a path. |
m |
Optional. The mediator(s)
from |
newdata |
Required. A data frame of the new data. It must be a data frame. |
... |
Additional arguments. Ignored. |
An lm_list
-class object is
a list of lm
-class objects, this
function is similar to the
stats::predict()
method of lm()
but it works on a system defined by a
list of regression models.
This is an advanced helper used by some functions in this package. Exported for advanced users.
A numeric vector of the predicted values, with length equal to the number of rows of user-supplied data.
data(data_serial_parallel) lm_m11 <- lm(m11 ~ x + c1 + c2, data_serial_parallel) lm_m12 <- lm(m12 ~ m11 + x + c1 + c2, data_serial_parallel) lm_m2 <- lm(m2 ~ x + c1 + c2, data_serial_parallel) lm_y <- lm(y ~ m11 + m12 + m2 + x + c1 + c2, data_serial_parallel) # Join them to form a lm_list-class object lm_serial_parallel <- lm2list(lm_m11, lm_m12, lm_m2, lm_y) lm_serial_parallel summary(lm_serial_parallel) newdat <- data_serial_parallel[3:5, ] predict(lm_serial_parallel, x = "x", y = "y", m = "m2", newdata = newdat)
data(data_serial_parallel) lm_m11 <- lm(m11 ~ x + c1 + c2, data_serial_parallel) lm_m12 <- lm(m12 ~ m11 + x + c1 + c2, data_serial_parallel) lm_m2 <- lm(m2 ~ x + c1 + c2, data_serial_parallel) lm_y <- lm(y ~ m11 + m12 + m2 + x + c1 + c2, data_serial_parallel) # Join them to form a lm_list-class object lm_serial_parallel <- lm2list(lm_m11, lm_m12, lm_m2, lm_y) lm_serial_parallel summary(lm_serial_parallel) newdat <- data_serial_parallel[3:5, ] predict(lm_serial_parallel, x = "x", y = "y", m = "m2", newdata = newdat)
Print the content of 'all_paths'-class object,
which can be generated by all_indirect_paths()
.
## S3 method for class 'all_paths' print(x, ...)
## S3 method for class 'all_paths' print(x, ...)
x |
A 'all_paths'-class object. |
... |
Optional arguments. |
This function is used to print the paths identified in a readable format.
x
is returned invisibly. Called for its side effect.
Shu Fai Cheung https://orcid.org/0000-0002-9871-9448
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths out1 <- all_indirect_paths(fit) out1
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths out1 <- all_indirect_paths(fit) out1
boot_out
-Class
ObjectPrint the content of the
output of do_boot()
or related
functions.
## S3 method for class 'boot_out' print(x, ...)
## S3 method for class 'boot_out' print(x, ...)
x |
The output of |
... |
Other arguments. Not used. |
x
is returned invisibly.
Called for its side effect.
data(data_med_mod_ab1) dat <- data_med_mod_ab1 lm_m <- lm(m ~ x*w + c1 + c2, dat) lm_y <- lm(y ~ m*w + x + c1 + c2, dat) lm_out <- lm2list(lm_m, lm_y) # In real research, R should be 2000 or even 5000 # In real research, no need to set parallel to FALSE # In real research, no need to set progress to FALSE # Progress is displayed by default. lm_boot_out <- do_boot(lm_out, R = 100, seed = 1234, progress = FALSE, parallel = FALSE) # Print the output of do_boot() lm_boot_out library(lavaan) data(data_med_mod_ab1) dat <- data_med_mod_ab1 dat$"x:w" <- dat$x * dat$w dat$"m:w" <- dat$m * dat$w mod <- " m ~ x + w + x:w + c1 + c2 y ~ m + w + m:w + x + c1 + c2 " fit <- sem(model = mod, data = dat, fixed.x = FALSE, se = "none", baseline = FALSE) # In real research, R should be 2000 or even 5000 # In real research, no need to set progress to FALSE # In real research, no need to set parallel to FALSE # Progress is displayed by default. fit_boot_out <- do_boot(fit = fit, R = 40, seed = 1234, parallel = FALSE, progress = FALSE) # Print the output of do_boot() fit_boot_out
data(data_med_mod_ab1) dat <- data_med_mod_ab1 lm_m <- lm(m ~ x*w + c1 + c2, dat) lm_y <- lm(y ~ m*w + x + c1 + c2, dat) lm_out <- lm2list(lm_m, lm_y) # In real research, R should be 2000 or even 5000 # In real research, no need to set parallel to FALSE # In real research, no need to set progress to FALSE # Progress is displayed by default. lm_boot_out <- do_boot(lm_out, R = 100, seed = 1234, progress = FALSE, parallel = FALSE) # Print the output of do_boot() lm_boot_out library(lavaan) data(data_med_mod_ab1) dat <- data_med_mod_ab1 dat$"x:w" <- dat$x * dat$w dat$"m:w" <- dat$m * dat$w mod <- " m ~ x + w + x:w + c1 + c2 y ~ m + w + m:w + x + c1 + c2 " fit <- sem(model = mod, data = dat, fixed.x = FALSE, se = "none", baseline = FALSE) # In real research, R should be 2000 or even 5000 # In real research, no need to set progress to FALSE # In real research, no need to set parallel to FALSE # Progress is displayed by default. fit_boot_out <- do_boot(fit = fit, R = 40, seed = 1234, parallel = FALSE, progress = FALSE) # Print the output of do_boot() fit_boot_out
Print the output of
cond_indirect_diff()
.
## S3 method for class 'cond_indirect_diff' print(x, digits = 3, pvalue = FALSE, pvalue_digits = 3, se = FALSE, ...)
## S3 method for class 'cond_indirect_diff' print(x, digits = 3, pvalue = FALSE, pvalue_digits = 3, se = FALSE, ...)
x |
The output of
|
digits |
The number of decimal places in the printout. |
pvalue |
Logical. If |
pvalue_digits |
Number of decimal places to display for the p-value. Default is 3. |
se |
Logical. If |
... |
Optional arguments. Ignored. |
The print
method of the
cond_indirect_diff
-class object.
If bootstrapping confidence interval was requested, this method has the option to print a p-value computed by the method presented in Asparouhov and Muthén (2021). Note that this p-value is asymmetric bootstrap p-value based on the distribution of the bootstrap estimates. It is not computed based on the distribution under the null hypothesis.
For a p-value of a, it means that a 100(1 - a)% bootstrapping confidence interval will have one of its limits equal to 0. A confidence interval with a higher confidence level will include zero, while a confidence interval with a lower confidence level will exclude zero.
It returns x
invisibly.
Called for its side effect.
Asparouhov, A., & Muthén, B. (2021). Bootstrap p-value computation. Retrieved from https://www.statmodel.com/download/FAQ-Bootstrap%20-%20Pvalue.pdf
Print the content of the
output of cond_indirect_effects()
## S3 method for class 'cond_indirect_effects' print( x, digits = 3, annotation = TRUE, pvalue = NULL, pvalue_digits = 3, se = NULL, level = 0.95, se_ci = TRUE, ... ) ## S3 method for class 'cond_indirect_effects' as.data.frame( x, row.names = NULL, optional = NULL, digits = 3, add_sig = TRUE, pvalue = NULL, pvalue_digits = 3, se = NULL, level = 0.95, se_ci = TRUE, to_string = FALSE, ... )
## S3 method for class 'cond_indirect_effects' print( x, digits = 3, annotation = TRUE, pvalue = NULL, pvalue_digits = 3, se = NULL, level = 0.95, se_ci = TRUE, ... ) ## S3 method for class 'cond_indirect_effects' as.data.frame( x, row.names = NULL, optional = NULL, digits = 3, add_sig = TRUE, pvalue = NULL, pvalue_digits = 3, se = NULL, level = 0.95, se_ci = TRUE, to_string = FALSE, ... )
x |
The output of
|
digits |
Number of digits to display. Default is 3. |
annotation |
Logical. Whether
the annotation after the table of
effects is to be printed. Default is
|
pvalue |
Logical. If |
pvalue_digits |
Number of decimal places to display for the p-values. Default is 3. |
se |
Logical. If |
level |
The level of confidence
for the confidence intervals computed
from the original standard errors. Used only for
paths without mediators and both
|
se_ci |
Logical. If |
... |
Other arguments. Not used. |
row.names |
Not used. Included to be compatible with the generic method. |
optional |
Not used. Included to be compatible with the generic method. |
add_sig |
Whether a column
of significance test results
will be added. Default is |
to_string |
If |
The print
method of the
cond_indirect_effects
-class object.
If bootstrapping confidence intervals were requested, this method has the option to print p-values computed by the method presented in Asparouhov and Muthén (2021). Note that these p-values are asymmetric bootstrap p-values based on the distribution of the bootstrap estimates. They not computed based on the distribution under the null hypothesis.
For a p-value of a, it means that a 100(1 - a)% bootstrapping confidence interval will have one of its limits equal to 0. A confidence interval with a higher confidence level will include zero, while a confidence interval with a lower confidence level will exclude zero.
If these conditions are met, the stored standard errors, if available, will be used test an effect and form it confidence interval:
Confidence intervals have not been formed (e.g., by bootstrapping or Monte Carlo).
The path has no mediators.
The model has only one group.
The path is moderated by one or more moderator.
Both the x
-variable and the
y
-variable are not standardized.
If the model is fitted by OLS
regression (e.g., using stats::lm()
),
then the variance-covariance matrix
of the coefficient estimates will be
used, and the p-value and confidence
intervals are computed from the t
statistic.
If the model is fitted by structural
equation modeling using lavaan
, then
the variance-covariance computed by
lavaan
will be used, and the p-value
and confidence intervals are computed
from the z statistic.
If the model is fitted by structural equation modeling and has moderators, the standard errors, p-values, and confidence interval computed from the variance-covariance matrices for conditional effects can only be trusted if all covariances involving the product terms are free. If any of them are fixed, for example, fixed to zero, it is possible that the model is not invariant to linear transformation of the variables.
The method as.data.frame()
for
cond_indirect_effects
objects is
used to convert this class of objects
to data frames. Used internally by the
print method but can also be used for
getting a data frame with columns such
as p-values and standard errors added.
The print
-method
returns x
invisibly.
Called for its side effect.
The as.data.frame
-method
returns a data frame with
the conditional effects and
confidence intervals (if available),
as well as other columns requested.
as.data.frame(cond_indirect_effects)
: The as.data.frame
-method
for cond_indirect_effects
objects.
Used internally by the print
-method
but can also be used directly.
Asparouhov, A., & Muthén, B. (2021). Bootstrap p-value computation. Retrieved from https://www.statmodel.com/download/FAQ-Bootstrap%20-%20Pvalue.pdf
cond_indirect_effects()
and
cond_effects()
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ a1 * x + d1 * w1 + e1 * x:w1 m2 ~ a2 * x y ~ b1 * m1 + b2 * m2 + cp * x " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) # Conditional effects from x to m1 when w1 is equal to each of the default levels cond_indirect_effects(x = "x", y = "m1", wlevels = "w1", fit = fit) # Conditional Indirect effect from x1 through m1 to y, # when w1 is equal to each of the default levels out <- cond_indirect_effects(x = "x", y = "y", m = "m1", wlevels = "w1", fit = fit) out print(out, digits = 5) print(out, annotation = FALSE) # Convert to data frames as.data.frame(out) as.data.frame(out, to_string = TRUE)
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ a1 * x + d1 * w1 + e1 * x:w1 m2 ~ a2 * x y ~ b1 * m1 + b2 * m2 + cp * x " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) # Conditional effects from x to m1 when w1 is equal to each of the default levels cond_indirect_effects(x = "x", y = "m1", wlevels = "w1", fit = fit) # Conditional Indirect effect from x1 through m1 to y, # when w1 is equal to each of the default levels out <- cond_indirect_effects(x = "x", y = "y", m = "m1", wlevels = "w1", fit = fit) out print(out, digits = 5) print(out, annotation = FALSE) # Convert to data frames as.data.frame(out) as.data.frame(out, to_string = TRUE)
Print the content of
a delta_med
-class object.
## S3 method for class 'delta_med' print(x, digits = 3, level = NULL, full = FALSE, boot_type, ...)
## S3 method for class 'delta_med' print(x, digits = 3, level = NULL, full = FALSE, boot_type, ...)
x |
A |
digits |
The number of digits after the decimal. Default is 3. |
level |
The level of confidence
of bootstrap confidence interval,
if requested when created. If |
full |
Logical. Whether
additional information will be printed.
Default is |
boot_type |
If bootstrap
confidence interval is to be formed,
the type of bootstrap confidence
interval. The supported types
are |
... |
Optional arguments. Ignored. |
It prints the output of
delta_med()
, which is a
delta_med
-class object.
x
is returned invisibly. Called
for its side effect.
Shu Fai Cheung https://orcid.org/0000-0002-9871-9448
library(lavaan) dat <- data_med mod <- " m ~ x y ~ m + x " fit <- sem(mod, dat) dm <- delta_med(x = "x", y = "y", m = "m", fit = fit) dm print(dm, full = TRUE) # Call do_boot() to generate # bootstrap estimates # Use 2000 or even 5000 for R in real studies # Set parallel to TRUE in real studies for faster bootstrapping boot_out <- do_boot(fit, R = 45, seed = 879, parallel = FALSE, progress = FALSE) # Remove 'progress = FALSE' in practice dm_boot <- delta_med(x = "x", y = "y", m = "m", fit = fit, boot_out = boot_out, progress = FALSE) dm_boot confint(dm_boot) confint(dm_boot, level = .90)
library(lavaan) dat <- data_med mod <- " m ~ x y ~ m + x " fit <- sem(mod, dat) dm <- delta_med(x = "x", y = "y", m = "m", fit = fit) dm print(dm, full = TRUE) # Call do_boot() to generate # bootstrap estimates # Use 2000 or even 5000 for R in real studies # Set parallel to TRUE in real studies for faster bootstrapping boot_out <- do_boot(fit, R = 45, seed = 879, parallel = FALSE, progress = FALSE) # Remove 'progress = FALSE' in practice dm_boot <- delta_med(x = "x", y = "y", m = "m", fit = fit, boot_out = boot_out, progress = FALSE) dm_boot confint(dm_boot) confint(dm_boot, level = .90)
Print the content of the
output of indirect_effect()
or
cond_indirect()
.
## S3 method for class 'indirect' print( x, digits = 3, pvalue = NULL, pvalue_digits = 3, se = NULL, level = 0.95, se_ci = TRUE, wrap_computation = TRUE, ... )
## S3 method for class 'indirect' print( x, digits = 3, pvalue = NULL, pvalue_digits = 3, se = NULL, level = 0.95, se_ci = TRUE, wrap_computation = TRUE, ... )
x |
The output of
|
digits |
Number of digits to display. Default is 3. |
pvalue |
Logical. If |
pvalue_digits |
Number of decimal places to display for the p-value. Default is 3. |
se |
Logical. If |
level |
The level of confidence
for the confidence interval computed
from the original standard errors. Used only for
paths without mediators and both
|
se_ci |
Logical. If |
wrap_computation |
Logical.
If |
... |
Other arguments. Not used. |
The print
method of the
indirect
-class object.
If bootstrapping confidence interval was requested, this method has the option to print a p-value computed by the method presented in Asparouhov and Muthén (2021). Note that this p-value is asymmetric bootstrap p-value based on the distribution of the bootstrap estimates. It is not computed based on the distribution under the null hypothesis.
For a p-value of a, it means that a 100(1 - a)% bootstrapping confidence interval will have one of its limits equal to 0. A confidence interval with a higher confidence level will include zero, while a confidence interval with a lower confidence level will exclude zero.
We recommend using confidence interval
directly. Therefore, p-value is not
printed by default. Nevertheless,
users who need it can request it
by setting pvalue
to TRUE
.
If these conditions are met, the stored standard error, if available, will be used to test an effect and form it confidence interval:
Confidence interval has not been formed (e.g., by bootstrapping or Monte Carlo).
The path has no mediators.
The model has only one group.
Both the x
-variable and the
y
-variable are not standardized.
If the model is fitted by OLS
regression (e.g., using stats::lm()
),
then the variance-covariance matrix
of the coefficient estimates will be
used, and the p-value and confidence
interval are computed from the t
statistic.
If the model is fitted by structural
equation modeling using lavaan
, then
the variance-covariance computed by
lavaan
will be used, and the p-value
and confidence interval are computed
from the z statistic.
If the model is fitted by structural equation modeling and has moderators, the standard errors, p-values, and confidence interval computed from the variance-covariance matrices for conditional effects can only be trusted if all covariances involving the product terms are free. If any some of them are fixed, for example, fixed to zero, it is possible that the model is not invariant to linear transformation of the variables.
x
is returned invisibly.
Called for its side effect.
Asparouhov, A., & Muthén, B. (2021). Bootstrap p-value computation. Retrieved from https://www.statmodel.com/download/FAQ-Bootstrap%20-%20Pvalue.pdf
indirect_effect()
and
cond_indirect()
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ a1 * x + b1 * w1 + d1 * x:w1 m2 ~ a2 * m1 + b2 * w2 + d2 * m1:w2 m3 ~ a3 * m2 + b3 * w3 + d3 * m2:w3 y ~ a4 * m3 + b4 * w4 + d4 * m3:w4 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) wvalues <- c(w1 = 5, w2 = 4, w3 = 2, w4 = 3) indirect_1 <- cond_indirect(x = "x", y = "y", m = c("m1", "m2", "m3"), fit = fit, wvalues = wvalues) indirect_1 dat <- modmed_x1m3w4y1 mod2 <- " m1 ~ a1 * x m2 ~ a2 * m1 m3 ~ a3 * m2 y ~ a4 * m3 + x " fit2 <- sem(mod2, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) indirect_2 <- indirect_effect(x = "x", y = "y", m = c("m1", "m2", "m3"), fit = fit2) indirect_2 print(indirect_2, digits = 5)
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ a1 * x + b1 * w1 + d1 * x:w1 m2 ~ a2 * m1 + b2 * w2 + d2 * m1:w2 m3 ~ a3 * m2 + b3 * w3 + d3 * m2:w3 y ~ a4 * m3 + b4 * w4 + d4 * m3:w4 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) wvalues <- c(w1 = 5, w2 = 4, w3 = 2, w4 = 3) indirect_1 <- cond_indirect(x = "x", y = "y", m = c("m1", "m2", "m3"), fit = fit, wvalues = wvalues) indirect_1 dat <- modmed_x1m3w4y1 mod2 <- " m1 ~ a1 * x m2 ~ a2 * m1 m3 ~ a3 * m2 y ~ a4 * m3 + x " fit2 <- sem(mod2, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) indirect_2 <- indirect_effect(x = "x", y = "y", m = c("m1", "m2", "m3"), fit = fit2) indirect_2 print(indirect_2, digits = 5)
Print the content of the
output of many_indirect_effects()
.
## S3 method for class 'indirect_list' print( x, digits = 3, annotation = TRUE, pvalue = FALSE, pvalue_digits = 3, se = FALSE, for_each_path = FALSE, ... )
## S3 method for class 'indirect_list' print( x, digits = 3, annotation = TRUE, pvalue = FALSE, pvalue_digits = 3, se = FALSE, for_each_path = FALSE, ... )
x |
The output of
|
digits |
Number of digits to display. Default is 3. |
annotation |
Logical. Whether
the annotation after the table of
effects is to be printed. Default is
|
pvalue |
Logical. If |
pvalue_digits |
Number of decimal places to display for the p-values. Default is 3. |
se |
Logical. If |
for_each_path |
Logical. If
|
... |
Other arguments. If
|
The print
method of the
indirect_list
-class object.
If bootstrapping confidence interval was requested, this method has the option to print a p-value computed by the method presented in Asparouhov and Muthén (2021). Note that this p-value is asymmetric bootstrap p-value based on the distribution of the bootstrap estimates. It is not computed based on the distribution under the null hypothesis.
For a p-value of a, it means that a 100(1 - a)% bootstrapping confidence interval will have one of its limits equal to 0. A confidence interval with a higher confidence level will include zero, while a confidence interval with a lower confidence level will exclude zero.
x
is returned invisibly.
Called for its side effect.
Asparouhov, A., & Muthén, B. (2021). Bootstrap p-value computation. Retrieved from https://www.statmodel.com/download/FAQ-Bootstrap%20-%20Pvalue.pdf
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths from x to y paths <- all_indirect_paths(fit, x = "x", y = "y") paths # Indirect effect estimates out <- many_indirect_effects(paths, fit = fit) out
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths from x to y paths <- all_indirect_paths(fit, x = "x", y = "y") paths # Indirect effect estimates out <- many_indirect_effects(paths, fit = fit) out
Print the content of an
'indirect_proportion'-class object,
the output of indirect_proportion()
.
## S3 method for class 'indirect_proportion' print(x, digits = 3, annotation = TRUE, ...)
## S3 method for class 'indirect_proportion' print(x, digits = 3, annotation = TRUE, ...)
x |
An 'indirect_proportion'-class object. |
digits |
Number of digits to display. Default is 3. |
annotation |
Logical. Whether
additional information should be
printed. Default is |
... |
Optional arguments. Not used. |
The print
method of the
indirect_proportion
-class object,
which is produced by
indirect_proportion()
. In addition
to the proportion of effect
mediated, it also prints additional
information such as the path for
which the proportion is computed,
and all indirect path(s) from the
x-variable to the y-variable.
To get the proportion as a scalar,
use the coef
method of
indirect_proportion
objects.
x
is returned invisibly. Called for
its side effect.
library(lavaan) dat <- data_med head(dat) mod <- " m ~ x + c1 + c2 y ~ m + x + c1 + c2 " fit <- sem(mod, dat, fixed.x = FALSE) out <- indirect_proportion(x = "x", y = "y", m = "m", fit = fit) out print(out, digits = 5)
library(lavaan) dat <- data_med head(dat) mod <- " m ~ x + c1 + c2 y ~ m + x + c1 + c2 " fit <- sem(mod, dat, fixed.x = FALSE) out <- indirect_proportion(x = "x", y = "y", m = "m", fit = fit) out print(out, digits = 5)
lm_list
-Class
ObjectPrint the content of the
output of lm2list()
.
## S3 method for class 'lm_list' print(x, ...)
## S3 method for class 'lm_list' print(x, ...)
x |
The output of |
... |
Other arguments. Not used. |
x
is returned invisibly.
Called for its side effect.
data(data_serial_parallel) lm_m11 <- lm(m11 ~ x + c1 + c2, data_serial_parallel) lm_m12 <- lm(m12 ~ m11 + x + c1 + c2, data_serial_parallel) lm_m2 <- lm(m2 ~ x + c1 + c2, data_serial_parallel) lm_y <- lm(y ~ m11 + m12 + m2 + x + c1 + c2, data_serial_parallel) # Join them to form a lm_list-class object lm_serial_parallel <- lm2list(lm_m11, lm_m12, lm_m2, lm_y) lm_serial_parallel
data(data_serial_parallel) lm_m11 <- lm(m11 ~ x + c1 + c2, data_serial_parallel) lm_m12 <- lm(m12 ~ m11 + x + c1 + c2, data_serial_parallel) lm_m2 <- lm(m2 ~ x + c1 + c2, data_serial_parallel) lm_y <- lm(y ~ m11 + m12 + m2 + x + c1 + c2, data_serial_parallel) # Join them to form a lm_list-class object lm_serial_parallel <- lm2list(lm_m11, lm_m12, lm_m2, lm_y) lm_serial_parallel
mc_out
-Class
ObjectPrint the content of the
output of do_mc()
or related
functions.
## S3 method for class 'mc_out' print(x, ...)
## S3 method for class 'mc_out' print(x, ...)
x |
The output of |
... |
Other arguments. Not used. |
x
is returned invisibly.
Called for its side effect.
library(lavaan) data(data_med_mod_ab1) dat <- data_med_mod_ab1 mod <- " m ~ x + w + x:w + c1 + c2 y ~ m + w + m:w + x + c1 + c2 " fit <- sem(mod, dat) # In real research, R should be 5000 or even 10000 mc_out <- do_mc(fit, R = 100, seed = 1234) # Print the output of do_boot() mc_out
library(lavaan) data(data_med_mod_ab1) dat <- data_med_mod_ab1 mod <- " m ~ x + w + x:w + c1 + c2 y ~ m + w + m:w + x + c1 + c2 " fit <- sem(mod, dat) # In real research, R should be 5000 or even 10000 mc_out <- do_mc(fit, R = 100, seed = 1234) # Print the output of do_boot() mc_out
Use the pseudo Johnson-Neyman approach (Hayes, 2022) to find the range of values of a moderator in which the conditional effect is not significant.
pseudo_johnson_neyman( object = NULL, w_lower = NULL, w_upper = NULL, optimize_method = c("uniroot", "optimize"), extendInt = c("no", "yes", "downX", "upX"), tol = .Machine$double.eps^0.25, level = 0.95 ) johnson_neyman( object = NULL, w_lower = NULL, w_upper = NULL, optimize_method = c("uniroot", "optimize"), extendInt = c("no", "yes", "downX", "upX"), tol = .Machine$double.eps^0.25, level = 0.95 ) ## S3 method for class 'pseudo_johnson_neyman' print(x, digits = 3, ...)
pseudo_johnson_neyman( object = NULL, w_lower = NULL, w_upper = NULL, optimize_method = c("uniroot", "optimize"), extendInt = c("no", "yes", "downX", "upX"), tol = .Machine$double.eps^0.25, level = 0.95 ) johnson_neyman( object = NULL, w_lower = NULL, w_upper = NULL, optimize_method = c("uniroot", "optimize"), extendInt = c("no", "yes", "downX", "upX"), tol = .Machine$double.eps^0.25, level = 0.95 ) ## S3 method for class 'pseudo_johnson_neyman' print(x, digits = 3, ...)
object |
A
|
w_lower |
The smallest value of
the moderator when doing the search.
If set to |
w_upper |
The largest value of
the moderator when doing the search.
If set to |
optimize_method |
The optimization
method to be used. Either
|
extendInt |
Used by
|
tol |
The tolerance level used
by both |
level |
The level of confidence of the confidence level. One minus this level is the level of significance. Default is .95, equivalent to a level of significance of .05. |
x |
The output of
|
digits |
Number of digits to display. Default is 3. |
... |
Other arguments. Not used. |
This function uses the pseudo Johnson-Neyman approach proposed by Hayes (2022) to find the values of a moderator at which a conditional effect is "nearly just significant" based on confidence interval. If an effect is moderated, there will be two such points (though one can be very large or small) forming a range. The conditional effect is not significant within this range, and significant outside this range, based on the confidence interval.
This function receives the output
of cond_indirect_effects()
and search for, within
a specific range, the two values of
the moderator at which
the conditional effect is "nearly just significant",
that is, the confidence interval
"nearly touches" zero.
Note that numerical method is used to find the points. Therefore, strictly speaking, the effects at the end points are still either significant or not significant, even if the confidence limit is very close to zero.
Though numerical method is used,
if the test is conducted using the
standard error (see below), the result is
equivalent to the (true)
Johnson-Neyman (1936) probing.
The function johnson_neyman()
is
just an alias to pseudo_johnson_neyman()
,
with the name consistent with what
it does in this special case.
This function supports models fitted
by lm()
, lavaan::sem()
,
and semTools::sem.mi()
. This function
also supports both bootstrapping
and Monte Carlo confidence intervals.
It also supports conditional
direct paths (no mediator) and
conditional indirect paths (with one
or more mediator), with x
and/or
y
standardized.
To be eligible for using this function, one of these conditions must be met:
One form of confidence intervals
(e.g, bootstrapping or Monte Carlo)
must has been requested (e.g.,
setting boot_ci = TRUE
or
mc_ci = TRUE
) when calling
cond_indirect_effects()
.
Tests can be done using stored
standard errors: A path with no
mediator and both the x
- and
y
-variables are not standardized.
For pre-computed confidence intervals,
the confidence level of the confidence
intervals adopted when calling
cond_indirect_effects()
will be used
by this function.
For tests conducted by standard
errors, the argument level
is used
to control the level of significance.
Even if a path has only one moderator, it is possible that no solution, or more than one solution, is/are found if the relation between this moderator and the conditional effect is not linear.
Solution may also be not found if the conditional effect is significant over a wide range of value of the moderator.
It is advised to use plot_effect_vs_w()
to examine the relation between the
effect and the moderator first before
calling this function.
Note that, for conditional indirect effects, the search can be slow because the confidence interval needs to be recomputed for each new value of the moderator.
This function currently only supports a path with only one moderator,
This function does not yet support multigroup models.
A list of the class pseudo_johnson_neyman
(with a print method, print.pseudo_johnson_neyman()
).
It has these major elements:
cond_effects
: An output of
cond_indirect_effects()
for the
two levels of the moderator found.
w_min_valid
: Logical. If TRUE
,
the conditional effect is just
significant at the lower level of
the moderator found,
and so is significant below this point.
If FALSE
, then the lower level of
the moderator found is just the
lower bound of the range searched,
that is, w_lower
.
w_max_valid
: Logical. If TRUE
,
the conditional effect is just
significant at the higher level of
the moderator found,
and so is significant above this point.
If FALSE
, then the higher level of
the moderator found is just the
upper bound of the range searched,
that is, w_upper
.
print(pseudo_johnson_neyman)
: Print
method for output of pseudo_johnson_neyman()
.
Johnson, P. O., & Neyman, J. (1936). Test of certain linear hypotheses and their application to some educational problems. Statistical Research Memoirs, 1, 57–93.
Hayes, A. F. (2022). Introduction to mediation, moderation, and conditional process analysis: A regression-based approach (Third edition). The Guilford Press.
library(lavaan) dat <- data_med_mod_a dat$wx <- dat$x * dat$w mod <- " m ~ x + w + wx y ~ m + x " fit <- sem(mod, dat) # In real research, R should be 2000 or even 5000 # In real research, no need to set parallel and progress to FALSE # Parallel processing is enabled by default and # progress is displayed by default. boot_out <- do_boot(fit, R = 50, seed = 4314, parallel = FALSE, progress = FALSE) out <- cond_indirect_effects(x = "x", y = "y", m = "m", wlevels = "w", fit = fit, boot_ci = TRUE, boot_out = boot_out) # Visualize the relation first plot_effect_vs_w(out) out_jn <- pseudo_johnson_neyman(out) out_jn # Plot the range plot_effect_vs_w(out_jn$cond_effects)
library(lavaan) dat <- data_med_mod_a dat$wx <- dat$x * dat$w mod <- " m ~ x + w + wx y ~ m + x " fit <- sem(mod, dat) # In real research, R should be 2000 or even 5000 # In real research, no need to set parallel and progress to FALSE # Parallel processing is enabled by default and # progress is displayed by default. boot_out <- do_boot(fit, R = 50, seed = 4314, parallel = FALSE, progress = FALSE) out <- cond_indirect_effects(x = "x", y = "y", m = "m", wlevels = "w", fit = fit, boot_ci = TRUE, boot_out = boot_out) # Visualize the relation first plot_effect_vs_w(out) out_jn <- pseudo_johnson_neyman(out) out_jn # Plot the range plot_effect_vs_w(out_jn$cond_effects)
Generated from a simple
mediation model among xthree latent
factors, fx
, fm
, and fy
, xeach
has three indicators.
simple_mediation_latent
simple_mediation_latent
A data frame with 200 rows and 11 variables:
Indicator of fx
. Numeric.
Indicator of fx
. Numeric.
Indicator of fx
. Numeric.
Indicator of fm
. Numeric.
Indicator of fm
. Numeric.
Indicator of fm
. Numeric.
Indicator of fy
. Numeric.
Indicator of fy
. Numeric.
Indicator of fy
. Numeric.
The model:
fx =~ x1 + x2 + x3 fm =~ m1 + m2 + m3 fy =~ y1 + y2 + y3 fm ~ a * fx fy ~ b * fm + cp * fx indirect := a * b
For subsetting a 'cond_indirect_effects'-class object.
## S3 method for class 'cond_indirect_effects' x[i, j, drop = if (missing(i)) TRUE else length(j) == 1]
## S3 method for class 'cond_indirect_effects' x[i, j, drop = if (missing(i)) TRUE else length(j) == 1]
x |
A 'cond_indirect_effects'-class object. |
i |
A numeric vector of row number(s), a character vector of row name(s), or a logical vector of row(s) to be selected. |
j |
A numeric vector of column number(s), a character vector of column name(s), or a logical vector of column(s) to be selected. |
drop |
Whether dropping a dimension if it only have one row/column. |
Customized [
for
'cond_indirect_effects'-class
objects, to ensure that these
operations work as they would be on a
data frame object, while information
specific to conditional effects is
modified correctly.
A
'cond_indirect_effects'-class object.
See cond_indirect_effects()
for
details on this class.
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ x + w1 + x:w1 m2 ~ m1 y ~ m2 + x + w4 + m2:w4 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # Examples for cond_indirect(): # Conditional effects from x to m1 when w1 is equal to each of the levels out1 <- cond_indirect_effects(x = "x", y = "m1", wlevels = "w1", fit = fit) out1[2, ] # Conditional Indirect effect from x1 through m1 to y, # when w1 is equal to each of the levels out2 <- cond_indirect_effects(x = "x", y = "y", m = c("m1", "m2"), wlevels = c("w1", "w4"), fit = fit) out2[c(1, 3), ]
library(lavaan) dat <- modmed_x1m3w4y1 mod <- " m1 ~ x + w1 + x:w1 m2 ~ m1 y ~ m2 + x + w4 + m2:w4 " fit <- sem(mod, dat, meanstructure = TRUE, fixed.x = FALSE, se = "none", baseline = FALSE) est <- parameterEstimates(fit) # Examples for cond_indirect(): # Conditional effects from x to m1 when w1 is equal to each of the levels out1 <- cond_indirect_effects(x = "x", y = "m1", wlevels = "w1", fit = fit) out1[2, ] # Conditional Indirect effect from x1 through m1 to y, # when w1 is equal to each of the levels out2 <- cond_indirect_effects(x = "x", y = "y", m = c("m1", "m2"), wlevels = c("w1", "w4"), fit = fit) out2[c(1, 3), ]
For subsetting a 'wlevels'-class object. Attributes related to the levels will be preserved if appropriate.
## S3 method for class 'wlevels' x[i, j, drop = if (missing(i)) TRUE else length(j) == 1] ## S3 replacement method for class 'wlevels' x[i, j] <- value ## S3 replacement method for class 'wlevels' x[[i, j]] <- value
## S3 method for class 'wlevels' x[i, j, drop = if (missing(i)) TRUE else length(j) == 1] ## S3 replacement method for class 'wlevels' x[i, j] <- value ## S3 replacement method for class 'wlevels' x[[i, j]] <- value
x |
A 'wlevels'-class object. |
i |
A numeric vector of row number(s), a character vector of row name(s), or a logical vector of row(s) to be selected. |
j |
A numeric vector of column number(s), a character vector of column name(s), or a logical vector of column(s) to be selected. |
drop |
Whether dropping a dimension if it only have one row/column. |
value |
Ignored. |
Customized [
for
'wlevels'-class objects, to ensure
that these operations work as they
would be on a data frame object,
while information specific to a
wlevels
-class object modified
correctly.
The assignment methods [<-
and [[<-
for
wlevels
-class objects will raise an
error. This class of objects should
be created by mod_levels()
or
related functions.
Subsetting the output of
mod_levels()
is possible but not
recommended. It is more reliable to
generate the levels using
mod_levels()
and related functions.
Nevertheless, there are situations in
which subsetting is preferred.
A 'wlevels'-class object. See
mod_levels()
and
merge_mod_levels()
for details on
this class.
mod_levels()
,
mod_levels_list()
, and
merge_mod_levels()
data(data_med_mod_ab) dat <- data_med_mod_ab # Form the levels from a list of lm() outputs lm_m <- lm(m ~ x*w1 + c1 + c2, dat) lm_y <- lm(y ~ m*w2 + x + w1 + c1 + c2, dat) lm_out <- lm2list(lm_m, lm_y) w1_levels <- mod_levels(lm_out, w = "w1") w1_levels w1_levels[2, ] w1_levels[c(2, 3), ] dat <- data_med_mod_serial_cat lm_m1 <- lm(m1 ~ x*w1 + c1 + c2, dat) lm_y <- lm(y ~ m1 + x + w1 + c1 + c2, dat) lm_out <- lm2list(lm_m1, lm_y) w1gp_levels <- mod_levels(lm_out, w = "w1") w1gp_levels w1gp_levels[2, ] w1gp_levels[3, ] merged_levels <- merge_mod_levels(w1_levels, w1gp_levels) merged_levels merged_levels[4:6, ] merged_levels[1:3, c(2, 3)] merged_levels[c(1, 4, 7), 1, drop = FALSE]
data(data_med_mod_ab) dat <- data_med_mod_ab # Form the levels from a list of lm() outputs lm_m <- lm(m ~ x*w1 + c1 + c2, dat) lm_y <- lm(y ~ m*w2 + x + w1 + c1 + c2, dat) lm_out <- lm2list(lm_m, lm_y) w1_levels <- mod_levels(lm_out, w = "w1") w1_levels w1_levels[2, ] w1_levels[c(2, 3), ] dat <- data_med_mod_serial_cat lm_m1 <- lm(m1 ~ x*w1 + c1 + c2, dat) lm_y <- lm(y ~ m1 + x + w1 + c1 + c2, dat) lm_out <- lm2list(lm_m1, lm_y) w1gp_levels <- mod_levels(lm_out, w = "w1") w1gp_levels w1gp_levels[2, ] w1gp_levels[3, ] merged_levels <- merge_mod_levels(w1_levels, w1gp_levels) merged_levels merged_levels[4:6, ] merged_levels[1:3, c(2, 3)] merged_levels[c(1, 4, 7), 1, drop = FALSE]
lm_list
-Class
ObjectThe summary of content
of the output of lm2list()
.
## S3 method for class 'lm_list' summary(object, ...) ## S3 method for class 'summary_lm_list' print(x, digits = 3, ...)
## S3 method for class 'lm_list' summary(object, ...) ## S3 method for class 'summary_lm_list' print(x, digits = 3, ...)
object |
The output of
|
... |
Other arguments. Not used. |
x |
An object of class
|
digits |
The number of significant digits in printing numerical results. |
summary.lm_list()
returns a
summary_lm_list
-class object, which
is a list of the summary()
outputs
of the lm()
outputs stored.
print.summary_lm_list()
returns x
invisibly. Called for its side
effect.
print(summary_lm_list)
: Print
method for output of summary for
lm_list.
data(data_serial_parallel) lm_m11 <- lm(m11 ~ x + c1 + c2, data_serial_parallel) lm_m12 <- lm(m12 ~ m11 + x + c1 + c2, data_serial_parallel) lm_m2 <- lm(m2 ~ x + c1 + c2, data_serial_parallel) lm_y <- lm(y ~ m11 + m12 + m2 + x + c1 + c2, data_serial_parallel) # Join them to form a lm_list-class object lm_serial_parallel <- lm2list(lm_m11, lm_m12, lm_m2, lm_y) lm_serial_parallel summary(lm_serial_parallel)
data(data_serial_parallel) lm_m11 <- lm(m11 ~ x + c1 + c2, data_serial_parallel) lm_m12 <- lm(m12 ~ m11 + x + c1 + c2, data_serial_parallel) lm_m2 <- lm(m2 ~ x + c1 + c2, data_serial_parallel) lm_y <- lm(y ~ m11 + m12 + m2 + x + c1 + c2, data_serial_parallel) # Join them to form a lm_list-class object lm_serial_parallel <- lm2list(lm_m11, lm_m12, lm_m2, lm_y) lm_serial_parallel summary(lm_serial_parallel)
It extracts the terms
object from an lm_from_lavaan
-class
object.
## S3 method for class 'lm_from_lavaan' terms(x, ...)
## S3 method for class 'lm_from_lavaan' terms(x, ...)
x |
An 'lm_from_lavaan'-class object. |
... |
Additional arguments. Ignored. |
A method for
lm_from_lavaan
-class that converts
a regression model for a variable in
a lavaan
model to a formula
object. This function simply calls
stats::terms()
on the formula
object to extract the predictors of a
variable.
A terms
-class object. See
terms.object for details.
terms.object,
lm_from_lavaan_list()
library(lavaan) data(data_med) mod <- " m ~ a * x + c1 + c2 y ~ b * m + x + c1 + c2 " fit <- sem(mod, data_med, fixed.x = FALSE) fit_list <- lm_from_lavaan_list(fit) terms(fit_list$m) terms(fit_list$y)
library(lavaan) data(data_med) mod <- " m ~ a * x + c1 + c2 y ~ b * m + x + c1 + c2 " fit <- sem(mod, data_med, fixed.x = FALSE) fit_list <- lm_from_lavaan_list(fit) terms(fit_list$m) terms(fit_list$y)
Compute the total
indirect effect between two
variables in the paths estimated by
many_indirect_effects()
.
total_indirect_effect(object, x, y)
total_indirect_effect(object, x, y)
object |
The output of
|
x |
Character. The name of the |
y |
Character. The name of the |
It extracts the
indirect
-class objects
of relevant paths and then add
the indirect effects together
using the +
operator.
An indirect
-class
object.
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths, control variables excluded paths <- all_indirect_paths(fit, exclude = c("c1", "c2")) paths # Indirect effect estimates out <- many_indirect_effects(paths, fit = fit) out # Total indirect effect from x to y total_indirect_effect(out, x = "x", y = "y")
library(lavaan) data(data_serial_parallel) mod <- " m11 ~ x + c1 + c2 m12 ~ m11 + x + c1 + c2 m2 ~ x + c1 + c2 y ~ m12 + m2 + m11 + x + c1 + c2 " fit <- sem(mod, data_serial_parallel, fixed.x = FALSE) # All indirect paths, control variables excluded paths <- all_indirect_paths(fit, exclude = c("c1", "c2")) paths # Indirect effect estimates out <- many_indirect_effects(paths, fit = fit) out # Total indirect effect from x to y total_indirect_effect(out, x = "x", y = "y")