Compute the conditional
effects, indirect effects, or
conditional indirect effects in a
structural model fitted by `lm()`

,
`lavaan::sem()`

, or `semTools::sem.mi()`

.

## Usage

```
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
)
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,
...
)
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,
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
)
many_indirect_effects(paths, ...)
```

## Arguments

- 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

`NULL`

, the default, the path goes from`x`

to`y`

.- fit
The fit object. Can be a lavaan::lavaan object or a list of

`lm()`

outputs. It can also be a`lavaan.mi`

object returned by`semTools::runMI()`

or its wrapper, such as`semTools::sem.mi()`

.- est
The output of

`lavaan::parameterEstimates()`

. If`NULL`

, the default, it will be generated from`fit`

. If supplied,`fit`

will be ignored.- implied_stats
Implied means, variances, and covariances of observed variables, of the form of the output of

`lavaan::lavInspect()`

with`what`

set to`"implied"`

. The standard deviations are extracted from this object for standardization. Default is`NULL`

, and implied statistics will be computed from`fit`

if required.- 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

`NULL`

.- standardized_x
Logical. Whether

`x`

will be standardized. Default is`FALSE`

.- standardized_y
Logical. Whether

`y`

will be standardized. Default is`FALSE`

.- boot_ci
Logical. Whether bootstrap confidence interval will be formed. Default is

`FALSE`

.- level
The level of confidence for the bootstrap confidence interval. Default is .95.

- boot_out
If

`boot_ci`

is`TRUE`

, users can supply pregenerated bootstrap estimates. This can be the output of`do_boot()`

. For`indirect_effect()`

and`cond_indirect_effects()`

, this can be the output of a previous call to`cond_indirect_effects()`

,`indirect_effect()`

, or`cond_indirect()`

with bootstrap confidence intervals requested. These stored estimates will be reused such that there is no need to do bootstrapping again. If not supplied, the function will try to generate them from`fit`

.- R
Integer. If

`boot_ci`

is`TRUE`

,`boot_out`

is`NULL`

, and bootstrap standard errors not requested if`fit`

is a lavaan object, this function will do bootstrapping on`fit`

.`R`

is the number of bootstrap samples. Default is 100. For Monte Carlo simulation, this is the number of replications.- seed
If bootstrapping or Monte Carlo simulation is conducted, this is the seed for the bootstrapping or simulation. Default is

`NULL`

and seed is not set.- parallel
Logical. If bootstrapping is conducted, whether parallel processing will be used. Default is

`TRUE`

. If`fit`

is a list of`lm()`

outputs, parallel processing will not be used.- ncores
Integer. The number of CPU cores to use when

`parallel`

is`TRUE`

. Default is the number of non-logical cores minus one (one minimum). Will raise an error if greater than the number of cores detected by`parallel::detectCores()`

. If`ncores`

is set, it will override`make_cluster_args`

in`do_boot()`

.- make_cluster_args
A named list of additional arguments to be passed to

`parallel::makeCluster()`

. For advanced users. See`parallel::makeCluster()`

for details. Default is`list()`

.- progress
Logical. Display bootstrapping progress or not. Default is

`TRUE`

.- save_boot_full
If

`TRUE`

, full bootstrapping results will be stored. Default is`FALSE.`

- prods
The product terms found. For internal use.

- get_prods_only
IF

`TRUE`

, will quit early and return the product terms found. The results can be passed to the`prod`

argument when calling this function. Default is`FALSE`

. This function is for internal use.- save_boot_out
If

`boot_out`

is supplied, whether it will be saved in the output. Default is`TRUE`

.- mc_ci
Logical. Whether Monte Carlo confidence interval will be formed. Default is

`FALSE`

.- mc_out
If

`mc_ci`

is`TRUE`

, users can supply pregenerated Monte Carlo estimates. This can be the output of`do_mc()`

. For`indirect_effect()`

and`cond_indirect_effects()`

, this can be the output of a previous call to`cond_indirect_effects()`

,`indirect_effect()`

, or`cond_indirect()`

with Monte Carlo confidence intervals requested. These stored estimates will be reused such that there is no need to do Monte Carlo simulation again. If not supplied, the function will try to generate them from`fit`

.- save_mc_full
If

`TRUE`

, full Monte Carlo results will be stored. Default is`FALSE.`

- save_mc_out
If

`mc_out`

is supplied, whether it will be saved in the output. Default is`TRUE`

.- ci_out
If

`ci_type`

is supplied, this is the corresponding argument. If`ci_type`

is`"boot"`

, this argument will be used as`boot_out`

. If`ci_type`

is`"mc"`

, this argument will be used as`mc_out`

.- save_ci_full
If

`TRUE`

, full bootstrapping or Monte Carlo results will be stored. Default is`FALSE.`

- save_ci_out
If either

`mc_out`

or`boot_out`

is supplied, whether it will be saved in the output. Default is`TRUE`

.- ci_type
The type of confidence intervals to be formed. Can be either

`"boot"`

(bootstrapping) or`"mc"`

(Monte Carlo). If not supplied or is`NULL`

, will check other arguments (e.g,`boot_ci`

and`mc_ci`

). If supplied, will override`boot_ci`

and`mc_ci`

.- wlevels
The output of

`merge_mod_levels()`

, or the moderator(s) to be passed to`mod_levels_list()`

. If all the moderators can be represented by one variable, that is, each moderator is (a) a numeric variable, (b) a dichotomous categorical variable, or (c) a factor or string variable used in`lm()`

in`fit`

, then it is a vector of the names of the moderators as appeared in the data frame. If at least one of the moderators is a categorical variable represented by more than one variable, such as user-created dummy variables used in`lavaan::sem()`

, then it must be a list of the names of the moderators, with such moderators represented by a vector of names. For example:`list("w1", c("gpgp2", "gpgp3")`

, the first moderator`w1`

and the second moderator a three-categorical variable represented by`gpgp2`

and`gpgp3`

.- w_type
Character. Whether the moderator is a

`"numeric"`

variable or a`"categorical"`

variable. If`"auto"`

, the function will try to determine the type automatically. See`mod_levels_list()`

for further information.- w_method
Character, either

`"sd"`

or`"percentile"`

. If`"sd"`

, the levels are defined by the distance from the mean in terms of standard deviation. if`"percentile"`

, the levels are defined in percentiles. See`mod_levels_list()`

for further information.- sd_from_mean
A numeric vector. Specify the distance in standard deviation from the mean for each level. Default is

`c(-1, 0, 1)`

when there is only one moderator, and`c(-1, 1)`

when there are more than one moderator. Ignored if`w_method`

is not equal to`"sd"`

. See`mod_levels_list()`

for further information.- percentiles
A numeric vector. Specify the percentile (in proportion) for each level. Default is

`c(.16, .50, .84)`

if there is one moderator, and`c(.16, .84)`

when there are more than one moderator. Ignored if`w_method`

is not equal to`"percentile"`

. See`mod_levels_list()`

for further information.- output_type
The type of output of

`cond_indirect_effects()`

. If`"data.frame"`

, the default, the output will be converted to a data frame. If any other values, the output is a list of the outputs from`cond_indirect()`

.- mod_levels_list_args
Additional arguments to be passed to

`mod_levels_list()`

if it is called for creating the levels of moderators. Default is`list()`

.- ...
For

`many_indirect_effects()`

, these are arguments to be passed to`indirect_effect()`

.- paths
The output of

`all_indirect_paths()`

## Value

`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)

## Details

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.

## Functions

`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.`many_indirect_effects()`

: Compute the indirect effects along more than one paths. It call`indirect_effect()`

once for each of the path.

## See also

`mod_levels()`

and
`merge_mod_levels()`

for generating
levels of moderators. do_boot for
doing bootstrapping before calling
these functions.

## Examples

```
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)
#>
#> == Conditional Effect ==
#>
#> Path: x -> m1
#> Moderators: w1
#> Conditional Effect: 0.750
#> When: w1 = 1.228
#>
#> Computation Formula:
#> (b.m1~x + (b.x:w1)*(w1))
#> Computation:
#> ((0.46277) + (0.23380)*(1.22806))
# Indirect effect from x1 through m2 to y
indirect_effect(x = "x", y = "y", fit = fit)
#>
#> == Effect ==
#>
#> Path: x -> y
#> Effect 0.312
#>
#> Computation Formula:
#> (b.y~x)
#> Computation:
#> (0.31176)
# 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)
#>
#> == Conditional Indirect Effect ==
#>
#> Path: x -> m1 -> y
#> Moderators: w1
#> Conditional Indirect Effect: -0.031
#> When: w1 = 1.228
#>
#> Computation Formula:
#> (b.m1~x + (b.x:w1)*(w1))*(b.y~m1)
#> Computation:
#> ((0.46277) + (0.23380)*(1.22806))*(-0.04197)
#> Coefficients of Component Paths:
#> Path Conditional Effect Original Coefficient
#> m1~x 0.750 0.463
#> y~m1 -0.042 -0.042
#>
# Examples for cond_indirect_effects():
# Create levels of w1, the moderators
w1levels <- mod_levels("w1", fit = fit)
w1levels
#> w1
#> M+1.0SD 1.2280576
#> Mean 0.2589999
#> M-1.0SD -0.7100578
# 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 effects ==
#>
#> Path: x -> m1
#> Conditional on moderator(s): w1
#> Moderator(s) represented by: w1
#>
#> [w1] (w1) ind m1~x
#> 1 M+1.0SD 1.228 0.750 0.750
#> 2 Mean 0.259 0.523 0.523
#> 3 M-1.0SD -0.710 0.297 0.297
#>
#> - The 'ind' column shows the effects.
#> - ‘m1~x’ is/are the path coefficient(s) along the path conditional on
#> the moderators.
#>
# 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)
#>
#> == Conditional indirect effects ==
#>
#> Path: x -> m1 -> y
#> Conditional on moderator(s): w1
#> Moderator(s) represented by: w1
#>
#> [w1] (w1) ind m1~x y~m1
#> 1 M+1.0SD 1.228 -0.031 0.750 -0.042
#> 2 Mean 0.259 -0.022 0.523 -0.042
#> 3 M-1.0SD -0.710 -0.012 0.297 -0.042
#>
#> - The 'ind' column shows the indirect effects.
#> - ‘m1~x’,‘y~m1’ is/are the path coefficient(s) along the path
#> conditional on the moderators.
#>
# 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
#> Call:
#> all_indirect_paths(fit = fit, x = "x", y = "y")
#> Path(s):
#> path
#> 1 x -> m11 -> m12 -> y
#> 2 x -> m11 -> y
#> 3 x -> m12 -> y
#> 4 x -> m2 -> y
# Indirect effect estimates
out <- many_indirect_effects(paths,
fit = fit)
out
#>
#> == Indirect Effect(s) ==
#> ind
#> x -> m11 -> m12 -> y 0.193
#> x -> m11 -> y 0.163
#> x -> m12 -> y 0.059
#> x -> m2 -> y 0.364
#>
#> - The 'ind' column shows the indirect effects.
#>
```