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)
Run the code above in your browser using DataLab