Skip to content

Instantly share code, notes, and snippets.

@EoinTravers
Created August 23, 2021 11:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EoinTravers/0e4d8b2331347e4efbf824474f3eb10f to your computer and use it in GitHub Desktop.
Save EoinTravers/0e4d8b2331347e4efbf824474f3eb10f to your computer and use it in GitHub Desktop.
plot_measurement_invariance = function(model){
factor_loadings = parameterEstimates(model) %>%
rename(estimate = est, low = ci.lower, high = ci.upper) %>%
mutate(rhs = factor(rhs, levels = unique(rhs)),
lhs = factor(lhs, levels = unique(lhs)),
label = paste(lhs, op, rhs),
type = case_when(
op == '=~' ~ 'Loading',
op == '~~' ~ 'Covariance',
op == '~1' ~ 'Mean',
T ~ '???'
))
if(!'group' %in% names(factor_loadings)){
factor_loadings$group = 'Common'
}
ggplot(factor_loadings,
aes(fct_rev(label), estimate,
color = factor(group),
ymin = low, ymax = high)) +
facet_wrap(~type, scales = 'free') +
geom_point() +
geom_errorbar(width=.2) +
geom_hline(linetype = 'dashed', yintercept = 0) +
coord_flip() +
no_legend_bg() +
labs(x = 'Item', y = 'Weight', color = 'Group')
}
plot_measurement_invariance(model_full) + ggtitle('Full Invariance')
plot_measurement_invariance(model_strict) + ggtitle('Strict Invariance')
plot_measurement_invariance(model_strong) + ggtitle('Strong Invariance')
plot_measurement_invariance(model_weak) + ggtitle('Weak Invariance')
plot_measurement_invariance(model_configural) + ggtitle('Configural Invariance')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment