get_nodes: Extract nodes from a SEM model object

View source: R/plot-plot_sem.R

get_nodesR Documentation

Extract nodes from a SEM model object

Description

Attempts to extract nodes from a SEM model object, where nodes are defined as observed or latent variables.

Usage

get_nodes(x, label = paste2(name, est_sig, sep = "\n"), ...)

Arguments

x

A model object of class mplusObject or lavaan.

label

Either a character, indicating which column to use for node labels, or an expression. See Details. Defaults to paste(name, est_sig, sep = "\n", which gives the node name followed by the estimated value with significance asterisks.

...

Additional parameters passed to table_results. For example, users can pass the digits argument to control the number of digits in the node label, or pass the columns argument to retain auxiliary columns in the tidy_nodes data.frame for further processing (see Examples).

Details

The function get_nodes identifies all dependent and independent variables in the model as nodes. If a mean structure / intercepts are included in the model, the output of table_results for those means / intercepts is used to label the nodes.

Custom labels

One way to create custom node labels is by passing an expression to label, as in the default value of the argument. When an expression is passed to label, it is evaluated in the context of a data.frame containing the results of a call to table_results on the x argument, with an additional column labeled name, which contains the node names.

Another way to create custom labels is by requesting auxiliary variables using the columns argument (which is passed to table_results), and then using these columns to construct a new label. See examples.

Value

An object of class 'tidy_nodes'

Examples

# Standard use extracts node names and shape
# (rect for observed, oval for latent)
library(lavaan)
res <- sem("dist ~ speed", cars)
get_nodes(res)

# To label nodes with mean values, include meanstructure in the model
# Note that it is possible to pass the argument 'digits' to table_results
# through '...'
res <- sem("dist ~ speed", cars, meanstructure = TRUE)
get_nodes(res, digits = 3)

# Pass an expression to the 'label' argument for custom labels
get_nodes(res, label = paste0(name, " ", est_sig, "\n", confint))

# Pass the argument 'columns' to table_results through '...' to retain
# auxiliary columns for further processing
nod <- get_nodes(res, columns = c("est_sig", "confint"))
nod
nod <- within(nod, {label <- paste0(name, " ", est_sig, "\n", confint)})
nod

cjvanlissa/tidySEM documentation built on March 16, 2024, 6:35 a.m.