lavaan.printer"

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Introduction

This article illustrates how to use the two main functions of lavaan.printer:

These are the packages used in this article:

library(lavaan)
library(lavaan.printer)

For What Scenarios?

These two functions are not for end-users. They are for package developers, and are intended to be used internally by print-methods or similar functions for the objects in other packages which generate customized parameter tables.

Scenarios

Create a List of Data frames

In the simplest case, parameterEstimates_table_list() can be used to generate the usual parameter estimates results of lavaan, but as a list of data frames, each corresponding to a section in the output (e.g., regression coefficients, factor loadings). Arguments for to lavaan::parameterEstimates() can be used when calling parameterEstimates_table_list():

# Adapted from the example of cfa()
model_cfa <- "visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6"
fit <- cfa(model_cfa,
           data = HolzingerSwineford1939,
           group = "school")
est <- parameterEstimates_table_list(fit,
                                     rsquare = TRUE)

Because it is intended to be used inside a print function, it does not have a print method itself. Call print_parameterEstimates_table_list() instead:

print_parameterEstimates_table_list(est)

The output looks similar to the lavaan output, except for some minor changes in column names (e.g., CI.Lo and CI.Up instead of ci.lower and ci.upper) to shorten the width of the print, to make room for additional columns developer may want to add. This is intentional: mimicking the lavaan style to minimize the need for the users to learn anything new in reading the output.

These options are available in print_parameterEstimates_table_list():

This is an example of these arguments:

print_parameterEstimates_table_list(est,
                                    nd = 2,
                                    by_group = FALSE,
                                    drop_cols = "Z",
                                    na_str = "--")

Insert a Column Computed From Another Column

Suppose we would like to insert a column of symbols (e.g., "*" and "**") into the parameter estimates table based on the pvalue column, to denote whether a parameter is significant,

This is the function:

add_sig <- function(object,
                    breaks = c(1, .05, .01, .001, -Inf),
                    labels = c("***", "** ", "*  ", "  ")) {
    tmp <- object[, "pvalue", drop = TRUE]
    if (!is.null(tmp)) {
        tmp[is.na(tmp)] <- 1
        tmp2 <- cut(tmp,
                    breaks = breaks,
                    labels = labels)
        i <- match("pvalue", colnames(object))
        out <- data.frame(object[, 1:i],
                          Sig = tmp2,
                          object[, seq(i + 1, ncol(object))])
      }
    out
  }

This can be done by the argument est_funs:

model_cfa <- "visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6"
fit <- cfa(model_cfa,
           data = HolzingerSwineford1939[1:100, ])
est <- parameterEstimates_table_list(fit,
                                     est_funs = list(add_sig))
print_parameterEstimates_table_list(est)

Note that est_funs must be a list, even if only one function is supplied.

Add a Header or Footer Section

Header or footer functions can be used to include a header or footer section. The first argument will be one of the following object:

The following is a simple function to print the missing data handling method, stored in the output of lavaan::parameterEstimates() when printed with output = "text", header = "TRUE". The attribute section_title is used to set the title for this section. By default, print() will be used to print a section.

lavaan_missing <- function(x) {
    out0 <- attr(x, "missing")
    out1 <- data.frame(Option = "Missing",
                       Setting = out0)
    attr(out1, "section_title") <- "Additional Information:"
    out1
  }

This is a function to add some footnotes. The output is a character vector, which should be printed by cat(). This can be done by setting the attribute print_fun to "cat". If the print function is cat(), the section is printed with sep = "\n" by default.

footnotes <- function(x) {
    out0 <- c("- This is footnote 1.",
              paste("- This is footnote 2, a very very very very very",
                    "very very very very very very very very very very",
                    "very very long one. Wrapped by default"))
    attr(out0, "section_title") <- "Footnote:"
    attr(out0, "print_fun") <- "cat"
    out0
  }

These functions can then be used in header_funs and footer_funs:

model_cfa <- "visual  =~ x1 + x2 + x3"
fit <- cfa(model_cfa,
           data = HolzingerSwineford1939)
est <- parameterEstimates_table_list(fit,
                                     header_funs = list(lavaan_missing),
                                     footer_funs = list(footnotes))
print_parameterEstimates_table_list(est)

Like est_funs, the value of header_funs (and footer_funs) must be list.

Further Information

There are other ways to customize the output when calling parameterEstimates_table_list(). For example:

Please refer to the help page of parameterEstimates_table_list() for details.

Issues

If you have any suggestions and found any bugs, please feel feel to open a GitHub issue. Thanks.



Try the lavaan.printer package in your browser

Any scripts or data that you put into this service are public.

lavaan.printer documentation built on Sept. 30, 2024, 5:08 p.m.