| 1 |
#' @templateVar class lavaan |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x A `lavaan` object, such as those returned from [lavaan::cfa()], |
| 5 |
#' and [lavaan::sem()]. |
| 6 |
#' |
| 7 |
#' @template param_confint |
| 8 |
#' |
| 9 |
#' @param ... Additional arguments passed to [lavaan::parameterEstimates()]. |
| 10 |
#' **Cautionary note**: Misspecified arguments may be silently ignored. |
| 11 |
#' |
| 12 |
#' @return A [tibble::tibble()] with one row for each estimated parameter and |
| 13 |
#' columns: |
| 14 |
#' |
| 15 |
#' \item{term}{The result of paste(lhs, op, rhs)} |
| 16 |
#' \item{op}{The operator in the model syntax (e.g. `~~` for covariances, or |
| 17 |
#' `~` for regression parameters)} |
| 18 |
#' \item{group}{The group (if specified) in the lavaan model} |
| 19 |
#' \item{estimate}{The parameter estimate (may be standardized)} |
| 20 |
#' \item{std.error}{} |
| 21 |
#' \item{statistic}{The z value returned by [lavaan::parameterEstimates()]} |
| 22 |
#' \item{p.value}{} |
| 23 |
#' \item{conf.low}{} |
| 24 |
#' \item{conf.high}{} |
| 25 |
#' \item{std.lv}{Standardized estimates based on the variances of the |
| 26 |
#' (continuous) latent variables only} |
| 27 |
#' \item{std.all}{Standardized estimates based on both the variances |
| 28 |
#' of both (continuous) observed and latent variables.} |
| 29 |
#' \item{std.nox}{Standardized estimates based on both the variances |
| 30 |
#' of both (continuous) observed and latent variables, but not the |
| 31 |
#' variances of exogenous covariates.} |
| 32 |
#' |
| 33 |
#' @examplesIf rlang::is_installed("lavaan") |
| 34 |
#' |
| 35 |
#' # load libraries for models and data |
| 36 |
#' library(lavaan) |
| 37 |
#' |
| 38 |
#' cfa.fit <- cfa("F =~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9", |
| 39 |
#' data = HolzingerSwineford1939, group = "school" |
| 40 |
#' ) |
| 41 |
#' |
| 42 |
#' tidy(cfa.fit) |
| 43 |
#' |
| 44 |
#' @export |
| 45 |
#' @aliases lavaan_tidiers sem_tidiers cfa_tidiers |
| 46 |
#' @family lavaan tidiers |
| 47 |
#' @seealso [tidy()], [lavaan::cfa()], [lavaan::sem()], |
| 48 |
#' [lavaan::parameterEstimates()] |
| 49 |
tidy.lavaan <- function(x, conf.int = FALSE, conf.level = 0.95, ...) { |
| 50 |
check_ellipses("exponentiate", "tidy", "lavaan", ...) |
| 51 |
|
| 52 |
lavaan::parameterEstimates( |
| 53 |
x, |
| 54 |
ci = conf.int, |
| 55 |
level = conf.level, |
| 56 |
standardized = TRUE, |
| 57 |
... |
| 58 |
) |> |
| 59 |
as_tibble() |> |
| 60 |
tibble::rownames_to_column() |> |
| 61 |
mutate(term = paste(lhs, op, rhs)) |> |
| 62 |
rename2( |
| 63 |
estimate = est, |
| 64 |
std.error = se, |
| 65 |
p.value = pvalue, |
| 66 |
statistic = z, |
| 67 |
conf.low = ci.lower, |
| 68 |
conf.high = ci.upper |
| 69 |
) |> |
| 70 |
select(term, op, dplyr::everything(), -rowname, -lhs, -rhs) |> |
| 71 |
as_tibble() |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
#' @templateVar class lavaan |
| 76 |
#' @template title_desc_glance |
| 77 |
#' |
| 78 |
#' @inheritParams tidy.lavaan |
| 79 |
#' @template param_unused_dots |
| 80 |
#' |
| 81 |
#' @return A one-row [tibble::tibble] with columns: |
| 82 |
#' |
| 83 |
#' \item{chisq}{Model chi squared} |
| 84 |
#' \item{npar}{Number of parameters in the model} |
| 85 |
#' \item{rmsea}{Root mean square error of approximation} |
| 86 |
#' \item{rmsea.conf.high}{95 percent upper bound on RMSEA} |
| 87 |
#' \item{srmr}{Standardised root mean residual} |
| 88 |
#' \item{agfi}{Adjusted goodness of fit} |
| 89 |
#' \item{cfi}{Comparative fit index} |
| 90 |
#' \item{tli}{Tucker Lewis index} |
| 91 |
#' \item{AIC}{Akaike information criterion} |
| 92 |
#' \item{BIC}{Bayesian information criterion} |
| 93 |
#' \item{ngroups}{Number of groups in model} |
| 94 |
#' \item{nobs}{Number of observations included} |
| 95 |
#' \item{norig}{Number of observation in the original dataset} |
| 96 |
#' \item{nexcluded}{Number of excluded observations} |
| 97 |
#' \item{converged}{Logical - Did the model converge} |
| 98 |
#' \item{estimator}{Estimator used} |
| 99 |
#' \item{missing_method}{Method for eliminating missing data} |
| 100 |
#' |
| 101 |
#' For further recommendations on reporting SEM and CFA models see |
| 102 |
#' Schreiber, J. B. (2017). Update to core reporting practices in |
| 103 |
#' structural equation modeling. Research in Social and Administrative |
| 104 |
#' Pharmacy, 13(3), 634-643. https://doi.org/10.1016/j.sapharm.2016.06.006 |
| 105 |
#' |
| 106 |
#' @examplesIf rlang::is_installed("lavaan") |
| 107 |
#' |
| 108 |
#' library(lavaan) |
| 109 |
#' |
| 110 |
#' # fit model |
| 111 |
#' cfa.fit <- cfa( |
| 112 |
#' "F =~ x1 + x2 + x3 + x4 + x5", |
| 113 |
#' data = HolzingerSwineford1939, group = "school" |
| 114 |
#' ) |
| 115 |
#' |
| 116 |
#' # summarize model fit with tidiers |
| 117 |
#' glance(cfa.fit) |
| 118 |
#' |
| 119 |
#' @export |
| 120 |
#' @family lavaan tidiers |
| 121 |
#' @seealso [glance()], [lavaan::cfa()], [lavaan::sem()], |
| 122 |
#' [lavaan::fitmeasures()] |
| 123 |
#' |
| 124 |
glance.lavaan <- function(x, ...) { |
| 125 |
res <- x |> |
| 126 |
lavaan::fitmeasures( |
| 127 |
fit.measures = c( |
| 128 |
"npar", |
| 129 |
"chisq", |
| 130 |
"rmsea", |
| 131 |
"rmsea.ci.upper", |
| 132 |
"srmr", |
| 133 |
"aic", |
| 134 |
"bic", |
| 135 |
"tli", |
| 136 |
"agfi", |
| 137 |
"cfi" |
| 138 |
) |
| 139 |
) |> |
| 140 |
tibble::enframe(name = "term") |> |
| 141 |
pivot_wider(names_from = term, values_from = value) |
| 142 |
res |> |
| 143 |
select(order(colnames(res))) |> |
| 144 |
map_df(as.numeric) |> |
| 145 |
bind_cols( |
| 146 |
tibble( |
| 147 |
converged = lavaan::lavInspect(x, "converged"), |
| 148 |
estimator = lavaan::lavInspect(x, "options")$estimator, |
| 149 |
ngroups = lavaan::lavInspect(x, "ngroups"), |
| 150 |
missing_method = lavaan::lavInspect(x, "options")$missing, |
| 151 |
nobs = sum(lavaan::lavInspect(x, "nobs")), |
| 152 |
norig = sum(lavaan::lavInspect(x, "norig")), |
| 153 |
nexcluded = norig - nobs |
| 154 |
) |
| 155 |
) |> |
| 156 |
rename(rmsea.conf.high = rmsea.ci.upper, AIC = aic, BIC = bic) |
| 157 |
} |