Use Case IDs

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

Instead of using row numbers (case.idx in the lavaan object), lavaan_rerun() from the package semfindr supports user supplied case IDs. This can make the output more readable.

library(semfindr)
dat <- pa_dat
# Add case id
dat <- cbind(id = paste0("case", seq_len(nrow(dat))), dat)
head(dat)

Suppose that the data set has a column of case IDs. A model is fitted to this data set using lavaan::sem():

mod <-
"
m1 ~ iv1 + iv2
dv ~ m1
"
library(lavaan)
fit <- sem(mod, dat)

Rerun n Times

We refit the model 100 times, each time with one case removed. Although the id column is not stored in lavaan, it can be supplied through the argument case_id:

if (file.exists("user_id_fit_rerun.RDS")) {
    fit_rerun <- readRDS("user_id_fit_rerun.RDS")
  } else {
    fit_rerun <- lavaan_rerun(fit, case_id = dat$id)
    saveRDS(fit_rerun, "user_id_fit_rerun.RDS")
  }
fit_rerun <- lavaan_rerun(fit, case_id = dat$id)

The list of reruns now uses id as the names:

head(fit_rerun$rerun[1:3])

As shown below, most diagnostic functions will use user supplied case IDs in their displays, making it easier to locate them in the original data set.

Diagnostic Functions

Standardized Changes in Estimates

fit_est_change <- est_change(fit_rerun)
fit_est_change
fit_est_change_paths_only <- est_change(fit_rerun,
                                parameters = c("m1 ~ iv1",
                                               "m1 ~ iv2",
                                               "dv ~ m1"))
fit_est_change_paths_only

Raw Changes in Estimates

fit_est_change_raw <- est_change_raw(fit_rerun)
fit_est_change_raw

Mahalanobis Distance

fit_md <- mahalanobis_rerun(fit_rerun)
fit_md

Changes in Fit Measures

fit_mc <- fit_measures_change(fit_rerun,
            fit_measures = c("chisq", "cfi", "tli", "rmsea"))
fit_mc

All-In-One-Function

fit_influence <- influence_stat(fit_rerun)
fit_influence

Diagnostic Plots

Generalized Cook's Distance

gcd_plot(fit_influence, largest_gcd = 3)

Mahalanobis Distance

md_plot(fit_influence,
        largest_md = 3)

Fit Measure vs. Generalized Cook's Distance

gcd_gof_plot(fit_influence,
             fit_measure = "rmsea",
             largest_gcd = 3,
             largest_fit_measure = 3)

Bubble Plot

gcd_gof_md_plot(fit_influence,
                fit_measure = "rmsea",
                largest_gcd = 3,
                largest_fit_measure = 3,
                largest_md = 3,
                circle_size = 15)


Try the semfindr package in your browser

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

semfindr documentation built on April 3, 2025, 5:58 p.m.