all_indirect_paths: Enumerate All Indirect Effects in a Model

View source: R/all_indirect_paths.R

all_indirect_pathsR Documentation

Enumerate All Indirect Effects in a Model

Description

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

Usage

all_indirect_paths(
  fit = NULL,
  exclude = NULL,
  x = NULL,
  y = NULL,
  group = NULL
)

all_paths_to_df(all_paths)

Arguments

fit

A fit object. It can be the output of lavaan::lavaan() or its wrapper such as lavaan::sem(), or a list of the output of lm() or the output of lm2list(). If it is a single model fitted by lm(), it will be automatically converted to a list by lm2list().

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. If NULL, 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. If NULL, the default, then all variables that are the outcome variables in at least one regression equation will be included in the search.

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. If not specified by the model has more than one group, than paths that appears in at least one group will be included in the output.

all_paths

An all_paths-class object. For example, the output of all_indirect_paths().

Details

It makes use of igraph::all_simple_paths() to identify paths in a model.

Multigroup Models

Since Version 0.1.14.2, support for multigroup models has been added for models fitted by lavaan. If a model has more than one group and group is not specified, than paths in all groups will be returned. If group is specified, than only paths in the selected group will be returned.

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

Functions

  • all_indirect_paths(): Enumerate all indirect paths.

  • all_paths_to_df(): Convert the output of all_indirect_paths() to a data frame with three columns: x, y, and m.

Author(s)

Shu Fai Cheung https://orcid.org/0000-0002-9871-9448

See Also

indirect_effect(), lm2list(). many_indirect_effects()

Examples

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
out1 <- all_indirect_paths(fit)
out1
names(out1)

# Exclude c1 and c2 in the search
out2 <- all_indirect_paths(fit, exclude = c("c1", "c2"))
out2
names(out2)

# 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
names(out3)

# Multigroup models

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)

all_indirect_paths(fit,
                   x = "x1",
                   y = "y1")
all_indirect_paths(fit,
                   x = "x1",
                   y = "y1",
                   group = 1)
all_indirect_paths(fit,
                   x = "x1",
                   y = "y1",
                   group = "Group B")


manymome documentation built on Oct. 4, 2024, 5:10 p.m.