auto_layout_mediation | R Documentation |
Set the layout of variables in a mediation model in the typical left-to-right style automatically.
auto_layout_mediation(
object,
x = NULL,
y = NULL,
exclude = NULL,
v_pos = c("middle", "lower", "upper"),
v_preference = c("upper", "lower"),
output = c("matrix", "xy"),
update_plot = TRUE
)
object |
It can be the output of
|
x |
The variables that will be
treated as (pure) |
y |
The variables that will be
treated as (pure) |
exclude |
The variables to be
omitted from the plot, typically the
covariates ("control variables") in a
model. If |
v_pos |
How the mediators are to
be positioned vertically in the
first pass. If
|
v_preference |
The preference in
shifting the mediators upward
( |
output |
The format of the
output, used if |
update_plot |
Logical. Used
if |
Typically, a path model with some
x
variables, some y
variables,
and some mediators are drawn from
left to right. This function tries
to generate the layout matrix
automatically, meeting the following
requirements:
The predictor(s), x
variables(x),
is/are placed to the left.
The outcome variable(s), y
variable(s), is/are placed to the
right.
The mediator(s) are positioned
between x
variable(s) and y
variable(s) such that all paths
point to the right. That is,
no vertical path.
The vertical position(s) of the mediator(s) will be adjusted such that no path passes through a mediator. That is, all paths are visible and not blocked by any mediator.
If object
is a lavaan
-class
object, or if update_plot
is FALSE
,
it returns
a two-dimension layout matrix of the
position of the nodes, or a
two-column matrix of the x-y positions
of the nodes, depending on the
argument output
.
If object
is a qgraph
object
and update_plot
is TRUE
, it
returns a qgraph
object with the
the modified layout.
set_sem_layout()
. The
output of auto_layout_mediation()
can be used by set_sem_layout()
.
library(lavaan)
library(semPlot)
# Create a dummy dataset
mod_pa <-
"
m11 ~ c1 + x1
m21 ~ c2 + m11
m2 ~ m11 + c3
m22 ~ m11 + c3
y ~ m2 + m21 + m22 + x1
"
fit <- lavaan::sem(
mod_pa,
do.fit = FALSE
)
dat <- simulateData(
parameterTable(fit),
sample.nobs = 500,
seed = 1234
)
fit <- lavaan::sem(
mod_pa,
dat
)
# Set the layout
m <- auto_layout_mediation(
fit,
exclude = c("c1", "c2", "c3")
)
pm <- semPlotModel(fit) |> drop_nodes(c("c1", "c2", "c3"))
semPaths(
pm,
whatLabels = "est",
layout = m
)
# v_pos = "lower"
m <- auto_layout_mediation(
fit,
exclude = c("c1", "c2", "c3"),
v_pos = "lower"
)
pm <- semPlotModel(fit) |> drop_nodes(c("c1", "c2", "c3"))
p0 <- semPaths(
pm,
whatLabels = "est",
layout = m
)
# v_pos = "upper"
m <- auto_layout_mediation(
fit,
exclude = c("c1", "c2", "c3"),
v_pos = "upper"
)
pm <- semPlotModel(fit) |> drop_nodes(c("c1", "c2", "c3"))
p0 <- semPaths(
pm,
whatLabels = "est",
layout = m
)
# Can modify a qgraph
pm <- semPlotModel(fit) |> drop_nodes(c("c1", "c2", "c3"))
p <- semPaths(
pm,
whatLabels = "est"
)
p2 <- auto_layout_mediation(p)
plot(p2)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.