Package 'lavaanPlot'

Title: Path Diagrams for 'Lavaan' Models via 'DiagrammeR'
Description: Plots path diagrams from models in 'lavaan' using the plotting functionality from the 'DiagrammeR' package. 'DiagrammeR' provides nice path diagrams via 'Graphviz', and these functions make it easy to generate these diagrams from a 'lavaan' path model without having to write the DOT language graph specification.
Authors: Alex Lishinski
Maintainer: Alex Lishinski <[email protected]>
License: GPL (>= 2)
Version: 0.8.1
Built: 2024-11-03 04:20:59 UTC
Source: https://github.com/alishinski/lavaanplot

Help Index


Builds the Diagrammer function call.

Description

Builds the Diagrammer function call.

Usage

buildCall(
  model = model,
  name = name,
  labels = labels,
  graph_options = list(overlap = "true", fontsize = "10"),
  node_options = list(shape = "box"),
  edge_options = list(color = "black"),
  ...
)

Arguments

model

A model fit object of class lavaan.

name

A string of the name of the plot.

labels

An optional named list of variable labels fit object of class lavaan.

graph_options

A named list of graph options for Diagrammer syntax.

node_options

A named list of node options for Diagrammer syntax.

edge_options

A named list of edge options for Diagrammer syntax.

...

additional arguments to be passed to buildPaths

Value

A string specifying the path diagram for model


Adds variable labels to the Diagrammer plot function call.

Description

Adds variable labels to the Diagrammer plot function call.

Usage

buildLabels(label_list)

Arguments

label_list

A named list of variable labels.


Extracts the paths from the lavaan model.

Description

Extracts the paths from the lavaan model.

Usage

buildPaths(
  fit,
  coefs = FALSE,
  sig = 1,
  stand = FALSE,
  covs = FALSE,
  stars = NULL,
  digits = 2
)

Arguments

fit

A model fit object of class lavaan.

coefs

whether or not to include significant path coefficient values in diagram

sig

significance level for determining what significant paths are

stand

Should the coefficients being used be standardized coefficients

covs

Should model covariances be included in the diagram

stars

a character vector indicating which parameters should include significance stars be included for regression paths, latent paths, or covariances. Include which of the 3 you want ("regress", "latent", "covs"), default is none.

digits

A number indicating the desired number of digits for the coefficient values in the plot


Uses the diagrammeR functions to turn the ndf and edf into dot

Description

Uses the diagrammeR functions to turn the ndf and edf into dot

Usage

convert_graph(ndf, edf, graph_options)

Arguments

ndf

A node data frame created by create_nodes

edf

An edge data frame created by create_edges

graph_options

a named list of graphviz graph attributes

Value

DOT specification of model


Creates edge data frame and adds formatting

Description

Creates edge data frame and adds formatting

Usage

create_edges(
  coefs,
  ndf,
  edge_options,
  coef_labels = FALSE,
  stand = FALSE,
  stars = NULL,
  sig = 1
)

Arguments

coefs

a coefficient table from lavaan model created by extract_coefs

ndf

A node data frame created by create_nodes

edge_options

a named list of graphviz edge attributes, or a data frame of edge options created by formatting, or a list of such data frames containing 1 set of edge options and one set of custom options

coef_labels

whether to label edges with coefficient values

stand

Should the coefficients being used be standardized coefficients

stars

a character vector indicating which parameters should include significance stars be included for regression paths, latent paths, or covariances. Include which of the 3 you want ("regress", "latent", "covs"), default is none.

sig

significance level for determining what significant paths are

Value

an edge data frame


Creates the grViz dot language code for plotting

Description

Creates the grViz dot language code for plotting

Usage

create_grviz(
  model,
  labels = labels,
  include = include,
  graph_options = graph_options,
  node_options = node_options,
  edge_options = edge_options,
  stand = stand,
  ...
)

Arguments

model

A model fit object of class lavaan.

labels

An optional named list of variable labels.

include

which parameters to include in the plot. Default is all regression and latent relationships. "covs" will also include covariances, while "all will also include error variances.

graph_options

a named list of graphviz graph attributes

node_options

a named list of graphviz node attributes

edge_options

a named list of graphviz edge attributes

stand

Should the coefficients being used be standardized coefficients

...

Additional arguments to be passed to create_edges

Value

A string specifying the path diagram for model


Creates node data frame and adds formatting

Description

Creates node data frame and adds formatting

Usage

create_nodes(coefs, labels = NULL, node_options)

Arguments

coefs

a coefficient table from lavaan model created by extract_coefs

labels

An optional list of labels

node_options

a named list of graphviz node attributes, or a data frame of node options created by formatting,

Value

an edge data frame


Embeds a plot into an rmarkdown pdf

Description

Embeds a plot into an rmarkdown pdf

Usage

embed_plot_pdf(plot, path, width = NULL, height = NULL)

Arguments

plot

plot object created by lavaanPlot

path

Filename to save the image

width

width of image in pixels, NULL for default

height

height of image, NULL for default

Value

no return value calls include_graphics to embed plot in pdf

Examples

library(lavaan)
model <- 'mpg ~ cyl + disp + hp
          qsec ~ disp + hp + wt'
fit <- sem(model, data = mtcars)
pl <- lavaanPlot(model = fit)
## Not run: 
embed_plot_pdf(pl, "plot2.pdf")

## End(Not run)

Creates a data frame of the parameter table from lavaan model

Description

Creates a data frame of the parameter table from lavaan model

Usage

extract_coefs(model, include = NULL, stand = FALSE)

Arguments

model

A fitted model of class lavaan

include

which parameters to include in the plot. Default is all regression and latent relationships. "covs" will also include covariances, while "all" will also include error variances.

stand

Should the coefficients being used be standardized coefficients

Value

a data frame with lavaan model parameters


Enables conditional formatting for different parts of the model

Description

Enables conditional formatting for different parts of the model

Usage

formatting(..., type, groups)

Arguments

...

lists of node or edge options for each of the groups

type

type of conditional formatting being used, node, edge, or custom; custom only works with edges

groups

character vector of the names of custom groups, with nodes and edges default values are set and you need to match the order: for nodes: c("latent", "obs"), for edges: c("regress", "latent", "covs"). For custom groups of edges, you must match names that you pre-multiply with coefficients in your model specification.

Value

a formatting data frame that can work with the create_nodes and create_edges functions


Extracts the paths from the lavaan model.

Description

Extracts the paths from the lavaan model.

Usage

getNodes(fit)

Arguments

fit

A model fit object of class lavaan.


Plots lavaan path model with DiagrammeR

Description

Plots lavaan path model with DiagrammeR

Usage

lavaanPlot(model, name = "plot", labels = NULL, ...)

Arguments

model

A model fit object of class lavaan.

name

A string of the name of the plot.

labels

An optional named list of variable labels.

...

Additional arguments to be called to buildCall and buildPaths

Value

A Diagrammer plot of the path diagram for model

Examples

library(lavaan)
model <- 'mpg ~ cyl + disp + hp
          qsec ~ disp + hp + wt'
fit <- sem(model, data = mtcars)
lavaanPlot(model = fit, node_options = list(shape = "box", fontname = "Helvetica"),
 edge_options = list(color = "grey"), coefs = FALSE)

Plots lavaan path model with DiagrammeR

Description

Plots lavaan path model with DiagrammeR

Usage

lavaanPlot2(
  model,
  labels = NULL,
  include = NULL,
  gr_viz = NULL,
  graph_options = NULL,
  node_options = NULL,
  edge_options = NULL,
  stand = FALSE,
  ...
)

Arguments

model

A model fit object of class lavaan.

labels

An optional named list of variable labels.

include

which parameters to include in the plot. Default is all regression and latent relationships. "covs" will also include covariances, while "all will also include error variances.

gr_viz

pass a gr_viz model generated from create_grviz to create plot from that directly

graph_options

a named list of graphviz graph attributes

node_options

a named list of graphviz node attributes

edge_options

a named list of graphviz edge attributes

stand

Should the coefficients being used be standardized coefficients

...

Additional arguments to be passed to create_grviz for creating edges

Value

A Diagrammer plot of the path diagram for model


Saves a plot as a png

Description

Saves a plot as a png

Usage

save_png(plot, path, width = NULL, height = NULL)

Arguments

plot

plot object created by lavaanPlot

path

filename to save the image

width

width of image in pixels, NULL for default

height

height of image, NULL for default

Value

no return value saves plot as png

Examples

library(lavaan)
model <- 'mpg ~ cyl + disp + hp
          qsec ~ disp + hp + wt'
fit <- sem(model, data = mtcars)
pl <- lavaanPlot(model = fit)
## Not run: 
save_png(pl, "plot.png")

## End(Not run)

Generates standard significance stars

Description

Generates standard significance stars

Generates standard significance stars

Usage

sig_stars(pvals)

sig_stars(pvals)

Arguments

pvals

a vector of p values