Learn R Programming

semhelpinghands (version 0.1.14)

record_history: Record the Minimization History

Description

Record the minimization history when a model is fitted by lavaan::lavaan() or its wrappers (e.g., lavaan::sem() or lavaan::cfa()).

Usage

record_history(object)

# S3 method for fit_history plot(x, params, last_n = -1, orientation = c("horizontal", "vertical"), ...)

# S3 method for fit_history print(x, n_iterations = 10, digits = 3, ...)

Value

A fit_history-class object with a plot method (plot.fit_history()).

Arguments

object

A 'lavaan'-class object.

x

A fit_history class object, the output of record_history().

params

A character vector of the names of parameters to be plotted. Must be the names of one or more columns in x.

last_n

The lass n iterations to be plotted. Default is -1, plotting all iterations.

orientation

The orientation of the plot. Either "horizontal" (the default) or "vertical".

...

Optional arguments. To be passed to the print method of data frame.

n_iterations

The number of iterations to print. Default is 10, printing the first 10 iterations (or all iterations, if the number of iterations is less than 10).

digits

The number of digits to be displayed. Default is 3.

Functions

  • plot(fit_history): The plot method for the output of record_history().

  • print(fit_history): The print method for the output of record_history().

Details

It records the minimization history when a model is fitted by lavaan::lavaan() or its wrappers (e.g., lavaan::sem() or lavaan::cfa()). The recorded history can then be plotted or displayed, for visualizing how the estimates of free parameters is found.

It will refit the model by the update method of lavaan::lavaan, setting se = "none" and test = "standard" because they have no impact on the minimization process.

This and related functions are adapted from the package semunpack. The version in this package will be revised to be an advanced version intended for diagnostic purpose in real studies.

Examples

Run this code

# Adapted from the example for CFA in lavaan::cfa().
# Using only two of the factors
library(lavaan)
HS.model <-
'
visual  =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
'
fit <- cfa(HS.model, data = HolzingerSwineford1939)

# Refit the model with the history recorded
fit_h <- record_history(fit)
fit_h

# Plot the history for selected parameters
plot(fit_h, params = c("visual=~x2", "visual=~x3",
                       "visual~~textual"),
            last_n = 10)
plot(fit_h, params = c("visual=~x2", "visual=~x3",
                       "visual~~textual"),
            last_n = 10,
            orientation = "vertical")

Run the code above in your browser using DataLab