‘lavEffects’ computes various ‘effects’ that are functions of the estimated model parameters of a fitted lavaan object. In this version, the focus is on the classic (LISREL-style) total, indirect and direct effects among the variables that appear in the structural part of the model (that is, the variables that are involved in a regression). Both observed and latent variables are supported.
This avoids the need to pre-specify all indirect (and total) effects manually
in the model syntax (using the := operator), which can be tedious when
there are many of them.
lavEffects(object, effects = c("total", "indirect"),
se_def = NULL, level = 0.95, monte_carlo = NULL,
boot_ci_type = "perc", zstat = TRUE, pvalue = TRUE, ci = TRUE,
standardized = FALSE, cov_std = TRUE,
add_class = TRUE, output = "data.frame", ...)If output = "data.frame" (the default), a data.frame (of class
lavaan.effects) with the following columns: effect (the type of
effect: total, indirect or direct), lhs (the outcome variable),
op (always "~"), rhs (the predictor variable), and,
depending on the arguments, group/level/block,
est, se, z, pvalue, ci.lower,
ci.upper and (if standardized is requested) one or more of
std.lv, std.all, std.nox and std.user. The effect
in a given row is the effect of rhs on lhs.
If output = "list", a (possibly nested) list of matrices, where element
\([i,j]\) contains the effect of variable \(j\) on variable \(i\).
An object of class lavaan.
Character vector. One or more of "total",
"indirect" and "direct". The default is
c("total", "indirect"). The order of the elements does not matter;
effects are always reported in the order total, indirect, direct.
Character (or NULL). The method used to compute standard
errors (and confidence intervals) for the effects. If "monte_carlo",
the Monte Carlo method is used: parameters are drawn from a multivariate
normal distribution with mean the parameter estimates and covariance matrix
the estimated covariance matrix of the parameters, and the effects are
recomputed for each draw (percentile confidence intervals). If
"delta", the delta method is used (based on a numerical Jacobian of
the effects with respect to the free parameters; symmetric normal-theory
confidence intervals). If "bootstrap", the bootstrap draws that are
stored in the fitted object (when the model was fitted with
se = "bootstrap") are used to compute bootstrap standard errors and
bootstrap percentile confidence intervals. If "none", no standard
errors are computed.
If se_def = NULL (the default), the choice is inherited from the
fitted object: if the model was fitted with se = "bootstrap" (and
bootstrap draws are available), the bootstrap method is used; else if the
model was fitted with se_def = "monte.carlo", the Monte Carlo method
is used; otherwise the Monte Carlo method is used as the default. An
explicit value always overrides this inheritance.
Numeric. The confidence level for the confidence intervals.
List (or NULL). Settings for the Monte Carlo method
(only used when se_def = "monte.carlo"), with elements R (the
number of Monte Carlo draws) and seed (the random seed). If
NULL (the default), the monte_carlo settings that were used
when the model was fitted are inherited (which themselves default to
list(R = 20000L, seed = NULL)). The state of the global random
number generator is saved and restored, so the seed used here does not
affect the user's random number stream.
Character. Only used when se_def = "bootstrap". The
type of bootstrap confidence interval, as in parameterEstimates:
"norm" (normal-theory using the bootstrap bias and standard error),
"basic" (basic bootstrap), "perc" (the default; percentile
method), "bca.simple" (bias-corrected percentile method, without the
acceleration term) or "bca" (the adjusted bootstrap percentile
method, with both bias and acceleration correction).
Logical. If TRUE, an extra column is added with the
\(z\)-statistic for each effect (the ratio of the estimate to its
standard error).
Logical. If TRUE, an extra column is added with the
(two-sided) p-value, computed using the standard normal distribution.
Logical. If TRUE (the default), two extra columns
(ci.lower and ci.upper) are added with the lower and upper
bounds of the confidence interval. If FALSE, the confidence interval
bounds are not shown.
Logical, character vector, or vector of (observed) variable
names, behaving as in parameterEstimates. If TRUE,
standardized effects are added as extra columns (point estimates only):
std.lv (only the latent variables are standardized), std.all
(both observed and latent variables are standardized) and, if there are
exogenous observed variables and fixed.x = TRUE, std.nox
(like std.all, but the exogenous observed variables are not
standardized). A character vector selects specific types (a subset of
"std.lv", "std.all", "std.nox"); a vector of observed
variable names standardizes only those observed variables (plus the latent
variables), and the result is reported in a std.user column. The
standardized effect of \(j\) on \(i\) equals the unstandardized effect
times the ratio of the model-implied standard deviations
\(sd_j / sd_i\).
Logical. See parameterEstimates. (Has no influence
on the standardized regression-type effects reported here.)
Logical. If TRUE, the result (when
output = "data.frame") is given the class
c("lavaan.effects", "lavaan.data.frame", "data.frame"), so that a
dedicated print method is used.
Character. If "data.frame" (the default), the result is a
(classed) data.frame with one row per effect. If "list", the result
is a list of matrices (one per effect type, and one per block in the
multilevel/multigroup case), where element \([i,j]\) contains the effect
of variable \(j\) on variable \(i\).
Only old names of arguments - with dots - are allowed here.
All effects are derived from the reduced-form matrix
\((I - B)^{-1}\), where \(B\) is the matrix of (direct) regression
coefficients among the structural variables (the beta matrix in the
LISREL representation that lavaan uses internally). Writing \(B[i,j]\) for the
direct effect of variable \(j\) on variable \(i\), we have:
the direct effect of \(j\) on \(i\) equals \(B[i,j]\);
the total effect of \(j\) on \(i\) equals \(\left((I - B)^{-1} - I\right)[i,j]\);
the indirect effect of \(j\) on \(i\) equals the total effect minus the direct effect.
Only effects that are structurally non-zero (that is, for which a directed path from \(j\) to \(i\) exists) are reported. For indirect effects, this means that at least one path of length two or more must exist.
The delta and Monte Carlo methods only require the parameter estimates and the
estimated covariance matrix of the parameters. The delta method produces
(symmetric) normal-theory confidence intervals; the Monte Carlo method produces
percentile-based confidence intervals, which need not be symmetric around the
point estimate. The Monte Carlo method typically behaves better than the delta
method for effects that are products of parameters (such as indirect effects),
in particular in smaller samples. When the model was fitted with
se = "bootstrap", the bootstrap draws are reused to obtain bootstrap
standard errors and bootstrap percentile confidence intervals (this requires
no additional model fitting).
Models fitted with conditional.x = TRUE are also supported: in that
case the structural model is \(eta = B eta + Gamma x + zeta\), where the
exogenous covariates \(x\) are stored in the gamma matrix. The effects
of these covariates on the endogenous variables are then computed as
\((I - B)^{-1} Gamma\) (total), \(Gamma\) (direct) and
\(((I - B)^{-1} - I) Gamma\) (indirect), and reported alongside the effects
among the beta variables.
parameterEstimates, standardizedSolution.
# a simple mediation model
set.seed(1234)
X <- rnorm(300)
M <- 0.5 * X + rnorm(300)
Y <- 0.4 * M + 0.3 * X + rnorm(300)
Data <- data.frame(X = X, Y = Y, M = M)
model <- ' # direct effect
Y ~ c*X
# mediator
M ~ a*X
Y ~ b*M
'
fit <- sem(model, data = Data)
# total and indirect effects, with Monte Carlo standard errors
lavEffects(fit)
# all effects, using the delta method
lavEffects(fit, effects = c("total", "indirect", "direct"), se_def = "delta")
# add standardized (total and indirect) effects
lavEffects(fit, se_def = "delta", standardized = TRUE)
Run the code above in your browser using DataLab