library(lavaan)
dat <- pa_dat
# The model
mod <-
"
m1 ~ a1 * iv1 + a2 * iv2
dv ~ b * m1
a1b := a1 * b
a2b := a2 * b
"
# Fit the model
fit <- lavaan::sem(mod, dat)
summary(fit)
# Fit the model n times. Each time with one case removed.
# For illustration, do this only for selected cases.
fit_rerun <- lavaan_rerun(fit, parallel = FALSE,
to_rerun = 1:10)
# Get all default influence stats
out <- influence_stat(fit_rerun)
head(out)
# Plot generalized Cook's distance. Label the 3 cases with the largest distances.
gcd_plot(out, largest_gcd = 3)
# Plot Mahalanobis distance. Label the 3 cases with the largest distances.
md_plot(out, largest_md = 3)
# Plot case influence on model chi-square against generalized Cook's distance.
# Label the 3 cases with the largest absolute influence.
# Label the 3 cases with the largest generalized Cook's distance.
gcd_gof_plot(out, fit_measure = "chisq", largest_gcd = 3,
largest_fit_measure = 3)
# Plot case influence on model chi-square against Mahalanobis distance.
# Size of bubble determined by generalized Cook's distance.
# Label the 3 cases with the largest absolute influence.
# Label the 3 cases with the largest Mahalanobis distance.
# Label the 3 cases with the largest generalized Cook's distance.
gcd_gof_md_plot(out, fit_measure = "chisq",
largest_gcd = 3,
largest_fit_measure = 3,
largest_md = 3,
circle_size = 10)
# Use the approximate method that does not require refitting the model.
# Fit the model
fit <- lavaan::sem(mod, dat)
summary(fit)
out <- influence_stat(fit)
head(out)
# Plot approximate generalized Cook's distance.
# Label the 3 cases with the largest values.
gcd_plot(out, largest_gcd = 3)
# Plot Mahalanobis distance.
# Label the 3 cases with the largest values.
md_plot(out, largest_md = 3)
# Plot approximate case influence on model chi-square against
# approximate generalized Cook's distance.
# Label the 3 cases with the largest absolute approximate case influence.
# Label the 3 cases with the largest approximate generalized Cook's distance.
gcd_gof_plot(out, fit_measure = "chisq", largest_gcd = 3,
largest_fit_measure = 3)
# Plot approximate case influence on model chi-square against Mahalanobis distance.
# The size of a bubble determined by approximate generalized Cook's distance.
# Label the 3 cases with the largest absolute approximate case influence.
# Label the 3 cases with the largest Mahalanobis distance.
# Label the 3 cases with the largest approximate generalized Cook's distance.
gcd_gof_md_plot(out, fit_measure = "chisq",
largest_gcd = 3,
largest_fit_measure = 3,
largest_md = 3,
circle_size = 10)
# Customize elements in the plot.
# For example, change the color and shape of the points.
gcd_gof_plot(out, fit_measure = "chisq", largest_gcd = 3,
largest_fit_measure = 3,
point_aes = list(shape = 3, color = "red"))
Run the code above in your browser using DataLab