prettylavaan: Pretty Printing of Lavaan Results

View source: R/prettylavaan.R

prettylavaanR Documentation

Pretty Printing of Lavaan Results

Description

Results from lavaan get messed up when summary() is called within a for loop within an R Markdown chunk with the option results = "asis". This function is an alternative to summary() that can print out a pretty output in such situations.

Usage

prettylavaan(
  fitobj,
  output_format = "asis",
  robust = FALSE,
  modindice.nrow = 10,
  param.type = c("=~", "~~", "~"),
  dp = 3,
  align = "c",
  multigroup = FALSE,
  show.modind = FALSE,
  ...
)

Arguments

fitobj

A lavaan fit object.

output_format

Output format, default is "asis" for use with results = "asis" chunks. Use "datatable" for an interactive parameter table. Or "kableExtra" to generate a list of kable tables that can be independently piped to kableExtra functions.

robust

Defaults to FALSE, set to TRUE to print out scaled and robust fit indicators.

modindice.nrow

Defaults to 10. Number of rows to display for modification indices.

param.type

Defaults to only show path coefficients (~), factor loadings (=~) and covariances (~~). Use (|) for thresholds if WLS estimators were used.

dp

Defaults to 3. Number of decimal points for all numeric values.

align

Defaults to "c", which stands for centered. Adjusts the alignment of text within table cells. Works the same way as kable().

multigroup

Defaults to FALSE. Set to TRUE if a multiple-group analysis was conducted. Keeps the grouping column in paramterEstimates().

show.modind

Defaults to FALSE. Set to TRUE to print modification indices.

...

Additional arguments passed onto kable() or datatable depending on output_format.

Examples

library(lavaan)
# the famous Holzinger and Swineford (1939) example
HS.model <-  "visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9"
fit <- cfa(HS.model, data = HolzingerSwineford1939)
prettylavaan(fit, output_format = "datatable")

# using robust estimators
robustfit <- cfa(HS.model, data = HolzingerSwineford1939, estimator = "MLM")
prettylavaan(robustfit, output_format = "datatable")

# request for robust fit indices
prettylavaan(robustfit, output_format = "datatable", robust = TRUE)

# multigroup
mfit <- cfa(HS.model, data = HolzingerSwineford1939, estimator = "MLM", group = "sex")
prettylavaan(mfit, robust = TRUE, multigroup = TRUE)

# for piping to kableExtra for further editing
# note: library(kableExtra) may mess up formatting of normal kable tables in the same Rmd document
# refer to https://github.com/haozhu233/kableExtra/issues/265
# format = "html" is required to work with kableExtra
library(kableExtra)
mylist <- prettylavaan(robustfit, output_format = "kableExtra", format = "html", robust = TRUE)
mylist$Param %>% kable_styling(font_size = 9)

Aaron0696/aaRon documentation built on July 27, 2023, 2:05 p.m.