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()
.
Arguments
- fit
A fit object. Either the output of
lavaan::lavaan()
or its wrapper such aslavaan::sem()
, or a list of the output oflm()
or the output oflm2list()
.- 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
x
variables. If supplied, only paths that start from these variables will be included in the search. IfNULL
, the default, then all variables that are one of the predictors in at least one regression equation will be included in the search.- y
A character vector of variables that will be included as the
y
variables. If supplied, only paths that start from these variables will be included in the search. IfNULL
, the default, then all variables that are the outcome variables in at least one regression equation will be included in the search.- all_paths
An
all_paths
-class object. For example, the output ofall_indirect_paths()
.
Value
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()
.
Details
It makes use of igraph::all_simple_paths()
to identify paths in a model.
Functions
all_indirect_paths()
: Enumerate all indirect paths.all_paths_to_df()
: Convert the output ofall_indirect_paths()
to a data frame with three columns:x
,y
, andm
.
Author
Shu Fai Cheung https://orcid.org/0000-0002-9871-9448
Examples
library(lavaan)
#> This is lavaan 0.6-16
#> lavaan is FREE software! Please report any bugs.
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
#> Call:
#> all_indirect_paths(fit = fit)
#> Path(s):
#> path
#> 1 m11 -> m12 -> y
#> 2 x -> m11 -> m12
#> 3 x -> m11 -> m12 -> y
#> 4 x -> m11 -> y
#> 5 x -> m12 -> y
#> 6 x -> m2 -> y
#> 7 c1 -> m11 -> m12
#> 8 c1 -> m11 -> m12 -> y
#> 9 c1 -> m11 -> y
#> 10 c1 -> m12 -> y
#> 11 c1 -> m2 -> y
#> 12 c2 -> m11 -> m12
#> 13 c2 -> m11 -> m12 -> y
#> 14 c2 -> m11 -> y
#> 15 c2 -> m12 -> y
#> 16 c2 -> m2 -> y
names(out1)
#> [1] "m11 -> m12 -> y" "x -> m11 -> m12" "x -> m11 -> m12 -> y"
#> [4] "x -> m11 -> y" "x -> m12 -> y" "x -> m2 -> y"
#> [7] "c1 -> m11 -> m12" "c1 -> m11 -> m12 -> y" "c1 -> m11 -> y"
#> [10] "c1 -> m12 -> y" "c1 -> m2 -> y" "c2 -> m11 -> m12"
#> [13] "c2 -> m11 -> m12 -> y" "c2 -> m11 -> y" "c2 -> m12 -> y"
#> [16] "c2 -> m2 -> y"
# Exclude c1 and c2 in the search
out2 <- all_indirect_paths(fit, exclude = c("c1", "c2"))
out2
#> Call:
#> all_indirect_paths(fit = fit, exclude = c("c1", "c2"))
#> Path(s):
#> path
#> 1 m11 -> m12 -> y
#> 2 x -> m11 -> m12
#> 3 x -> m11 -> m12 -> y
#> 4 x -> m11 -> y
#> 5 x -> m12 -> y
#> 6 x -> m2 -> y
names(out2)
#> [1] "m11 -> m12 -> y" "x -> m11 -> m12" "x -> m11 -> m12 -> y"
#> [4] "x -> m11 -> y" "x -> m12 -> y" "x -> m2 -> y"
# 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
#> Call:
#> all_indirect_paths(fit = fit, exclude = c("c1", "c2"), x = "x",
#> y = "y")
#> Path(s):
#> path
#> 1 x -> m11 -> m12 -> y
#> 2 x -> m11 -> y
#> 3 x -> m12 -> y
#> 4 x -> m2 -> y
names(out3)
#> [1] "x -> m11 -> m12 -> y" "x -> m11 -> y" "x -> m12 -> y"
#> [4] "x -> m2 -> y"