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,
keep_nodes, 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
Suppose we have a model with four main variables,
x4, and three control variables,
cov3. The sample data,
pa_example_3covs, is in the package
This is the analysis in
We can plot the full model using
semPlot::semPaths and use
layout_matrix to control the positions of all seven variables (please refer to the vignette on
layou_matrix on how to do this):
Suppose we want to remove
cov3. We do this by calling
semPlot::semPlotModel directly, and modify it by
We can drop
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
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:
We can then use
semPlot::semPaths to plot this modified model:
Note that in the call to
semPlot::semPaths, the modified output of
pm_no_covs, is used instead of
fit, the output of
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.
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.