View source: R/cond_indirect.R
cond_indirect | R Documentation |
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, ...)
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. 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.