The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.
In psychological research, it is common for a path analytic model to have several control variables. They need to be included in the analysis. However, for generating the plot, it is acceptable to omit them as long as results regarding these control variables are reported in other forms (e.g., a table of all parameter estimates), and keep only the main variables in the plot.
Two helper functions, drop_nodes
and
keep_nodes
, from the package semptools (CRAN page), were
developed for this purpose. When an SEM output object, such as the
output from lavaan
, is passed to
semPlot::semPaths
, it is first processed by
semPlot::semPlotModel
. In other words,
semPlot::semPaths
itself does not interpret the SEM output.
It actually use the output of semPlot::semPlotModel
to
generate the plot. These helper functions modify the output of
semPlot::semPlotModel
to be used by
semPlot::semPaths
.
Suppose we have a model with four main variables, x1
,
x2
, x3
, and x4
, and three control
variables, cov1
, cov2
, and cov3
.
The sample data, pa_example_3covs
, is in the package
semptools
.
This is the analysis in lavaan
:
library(lavaan)
#> This is lavaan 0.6-16
#> lavaan is FREE software! Please report any bugs.
<-
mod 'x3 ~ x1 + x2 + cov1 + cov2 + cov3
x4 ~ x1 + x3 + cov1 + cov2 + cov3
'
<- lavaan::sem(mod, pa_example_3covs) fit
We can plot the full model using semPlot::semPaths
and
use layout
and layout_matrix
to control the
positions of all seven variables (please refer to the vignette on
layou_matrix
on how to do this):
library(semPlot)
library(semptools)
<- layout_matrix(x1 = c(1, 1),
m x2 = c(3, 1),
x3 = c(2, 2),
x4 = c(2, 3),
cov1 = c(4, 1),
cov2 = c(5, 1),
cov3 = c(6, 1))
<- semPaths(fit, whatLabels = "est",
p_pa sizeMan = 10,
edge.label.cex = .5,
style = "ram",
nCharNodes = 0, nCharEdges = 0,
layout = m)
Suppose we want to remove cov1
, cov2
, and
cov3
. We do this by calling
semPlot::semPlotModel
directly, and modify it by
keep_nodes
or drop_nodes
.
We can drop cov1
, cov2
, and
cov3
by drop_nodes
:
<- semptools::drop_nodes(
pm_no_covs object = semPlotModel(fit),
nodes = c("cov1", "cov2", "cov3"))
The first argument, object
, should be the output of
semPlot::semPlotModel
. In the example,
semPlotModel(fit)
is used to call
semPlot::semPlotModel
to process fit
and then
pass the results immediately to drop_nodes
.
The second argument, nodes
, is a character vector with
the names of the variables to be dropped.
With just two arguments, the argument names can be omitted:
<- semptools::drop_nodes(
pm_no_covs semPlotModel(fit),
c("cov1", "cov2", "cov3"))
We can then use semPlot::semPaths
to plot this modified
model:
<- layout_matrix(x1 = c(1, 1),
m_no_covs x2 = c(3, 1),
x3 = c(2, 2),
x4 = c(2, 3))
<- semPaths(pm_no_covs, whatLabels = "est",
pa_no_covs sizeMan = 10,
edge.label.cex = .5,
style = "ram",
nCharNodes = 0, nCharEdges = 0,
layout = m_no_covs)
Note that in the call to semPlot::semPaths
, the modified
output of semPlot::semPlotModel
, pm_no_covs
,
is used instead of fit
, the output of
lavaan
.
Alternatively, we can also specify the variables to keep
using keep_nodes
. It is used in a similar way, except that
the variables specified in nodes
will be kept, and all
variables not specified will be removed.
<- semptools::keep_nodes(
pm_only_xs semPlotModel(fit),
c("x1", "x2", "x3", "x4"))
<- semPaths(pm_only_xs, whatLabels = "est",
pa_only_xs sizeMan = 10,
edge.label.cex = .5,
style = "ram",
nCharNodes = 0, nCharEdges = 0,
layout = m_no_covs)
This plot is identical to the previous plot.
The plot generated by semPlot::semPaths
with selected
nodes dropped or kept can then be passed to other semptools
functions for further processing.
Which function to use depends on which one is easier to specify. For
example, if there are a lot of contorl variables but only a few main
variables, then it is more efficient to use keep_nodes
and
specify the few main variables. If the number of control variables is
substantially less than the number of main variables, then it is more
efficient to use drop_nodes
and specify only the few
control variables to drop.
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.