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 |
Builds the Diagrammer function call.
buildCall( model = model, name = name, labels = labels, graph_options = list(overlap = "true", fontsize = "10"), node_options = list(shape = "box"), edge_options = list(color = "black"), ... )
buildCall( model = model, name = name, labels = labels, graph_options = list(overlap = "true", fontsize = "10"), node_options = list(shape = "box"), edge_options = list(color = "black"), ... )
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 |
A string specifying the path diagram for model
Adds variable labels to the Diagrammer plot function call.
buildLabels(label_list)
buildLabels(label_list)
label_list |
A named list of variable labels. |
Extracts the paths from the lavaan model.
buildPaths( fit, coefs = FALSE, sig = 1, stand = FALSE, covs = FALSE, stars = NULL, digits = 2 )
buildPaths( fit, coefs = FALSE, sig = 1, stand = FALSE, covs = FALSE, stars = NULL, digits = 2 )
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
convert_graph(ndf, edf, graph_options)
convert_graph(ndf, edf, graph_options)
ndf |
A node data frame created by |
edf |
An edge data frame created by |
graph_options |
a named list of graphviz graph attributes |
DOT specification of model
Creates edge data frame and adds formatting
create_edges( coefs, ndf, edge_options, coef_labels = FALSE, stand = FALSE, stars = NULL, sig = 1 )
create_edges( coefs, ndf, edge_options, coef_labels = FALSE, stand = FALSE, stars = NULL, sig = 1 )
coefs |
a coefficient table from lavaan model created by |
ndf |
A node data frame created by |
edge_options |
a named list of graphviz edge attributes, or a data frame of edge options created by |
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 |
an edge data frame
Creates the grViz dot language code for plotting
create_grviz( model, labels = labels, include = include, graph_options = graph_options, node_options = node_options, edge_options = edge_options, stand = stand, ... )
create_grviz( model, labels = labels, include = include, graph_options = graph_options, node_options = node_options, edge_options = edge_options, stand = stand, ... )
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 |
A string specifying the path diagram for model
Creates node data frame and adds formatting
create_nodes(coefs, labels = NULL, node_options)
create_nodes(coefs, labels = NULL, node_options)
coefs |
a coefficient table from lavaan model created by |
labels |
An optional list of labels |
node_options |
a named list of graphviz node attributes, or a data frame of node options created by |
an edge data frame
Embeds a plot into an rmarkdown pdf
embed_plot_pdf(plot, path, width = NULL, height = NULL)
embed_plot_pdf(plot, path, width = NULL, height = NULL)
plot |
plot object created by |
path |
Filename to save the image |
width |
width of image in pixels, NULL for default |
height |
height of image, NULL for default |
no return value calls include_graphics
to embed plot in pdf
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)
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
extract_coefs(model, include = NULL, stand = FALSE)
extract_coefs(model, include = NULL, stand = FALSE)
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 |
a data frame with lavaan model parameters
Enables conditional formatting for different parts of the model
formatting(..., type, groups)
formatting(..., type, groups)
... |
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. |
a formatting data frame that can work with the create_nodes and create_edges functions
Extracts the paths from the lavaan model.
getNodes(fit)
getNodes(fit)
fit |
A model fit object of class lavaan. |
Plots lavaan path model with DiagrammeR
lavaanPlot(model, name = "plot", labels = NULL, ...)
lavaanPlot(model, name = "plot", labels = NULL, ...)
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 |
A Diagrammer plot of the path diagram for model
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)
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
lavaanPlot2( model, labels = NULL, include = NULL, gr_viz = NULL, graph_options = NULL, node_options = NULL, edge_options = NULL, stand = FALSE, ... )
lavaanPlot2( model, labels = NULL, include = NULL, gr_viz = NULL, graph_options = NULL, node_options = NULL, edge_options = NULL, stand = FALSE, ... )
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 |
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 |
A Diagrammer plot of the path diagram for model
Saves a plot as a png
save_png(plot, path, width = NULL, height = NULL)
save_png(plot, path, width = NULL, height = NULL)
plot |
plot object created by |
path |
filename to save the image |
width |
width of image in pixels, NULL for default |
height |
height of image, NULL for default |
no return value saves plot as png
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)
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
Generates standard significance stars
sig_stars(pvals) sig_stars(pvals)
sig_stars(pvals) sig_stars(pvals)
pvals |
a vector of p values |