cond_indirect {manymome}R Documentation

Conditional, Indirect, and Conditional Indirect Effects

Description

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,
  group = 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,
  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,
  group = 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. For multigroup models, model implied standard deviation for the selected group will be used.

standardized_y

Logical. Whether y will be standardized. Default is FALSE. For multigroup models, model implied standard deviation for the selected group will be used.

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::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.

group

Either the group number as appeared in the summary() or lavaan::parameterEstimates() output of a lavaan::lavaan object, or the group label as used in the lavaan::lavaan object. Used only when the number of groups is greater than one. Default is NULL.

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

groups

Either a vector of group numbers as appeared in the summary() or lavaan::parameterEstimates() output of a lavaan::lavaan object, or a vector of group labels as used in the lavaan::lavaan object. Used only when the number of groups is greater than one. Default is NULL.

...

For many_indirect_effects(), these are arguments to be passed to indirect_effect().

paths

The output of all_indirect_paths()

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:

  1. 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.

  2. 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().

  3. 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.

Multigroup Models

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:

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.

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)

Functions

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)

# Indirect effect from x1 through m2 to y
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


[Package manymome version 0.2.1 Index]