|
102 | 102 | model_performance.lavaan <- function(model, metrics = "all", verbose = TRUE, ...) { |
103 | 103 | insight::check_if_installed("lavaan") |
104 | 104 |
|
105 | | - # Check if convergeed |
106 | | - if(insight::is_converged(model)) { |
| 105 | + # All possible metrics that this function can return. |
| 106 | + all_metric_names <- c( |
| 107 | + "Chi2", "Chi2_df", "p_Chi2", "Baseline", "Baseline_df", "p_Baseline", "GFI", |
| 108 | + "AGFI", "NFI", "NNFI", "CFI", "RMSEA", "RMSEA_CI_low", "RMSEA_CI_high", |
| 109 | + "p_RMSEA", "RMR", "SRMR", "RFI", "PNFI", "IFI", "RNI", "Loglikelihood", |
| 110 | + "AIC", "BIC", "BIC_adjusted" |
| 111 | + ) |
| 112 | + |
| 113 | + # extract valid column names based on metrics |
| 114 | + if (is.character(metrics) && !all(metrics == "all")) { |
| 115 | + out_names <- intersect(metrics, all_metric_names) |
| 116 | + } else { |
| 117 | + out_names <- all_metric_names |
| 118 | + } |
| 119 | + |
| 120 | + # Check if converged |
| 121 | + if (insight::is_converged(model)) { |
107 | 122 | if (isTRUE(verbose)) { |
108 | 123 | measures <- as.data.frame(t(as.data.frame(lavaan::fitmeasures(model, ...)))) |
109 | 124 | } else { |
@@ -139,34 +154,19 @@ model_performance.lavaan <- function(model, metrics = "all", verbose = TRUE, ... |
139 | 154 | BIC = measures$bic, |
140 | 155 | BIC_adjusted = measures$bic2 |
141 | 156 | ) |
142 | | - |
143 | | - if (all(metrics == "all")) { |
144 | | - metrics <- names(out) |
145 | | - } |
146 | | - out <- out[, metrics, drop = FALSE] |
147 | 157 | } else { |
148 | | - insight::format_warning("This lavaan model did not converge, no performance indices can be computed. Returning `NA`.") |
149 | | - |
150 | | - # All possible metrics that this function can return. |
151 | | - all_metric_names <- c( |
152 | | - "Chi2", "Chi2_df", "p_Chi2", "Baseline", "Baseline_df", "p_Baseline", "GFI", |
153 | | - "AGFI", "NFI", "NNFI", "CFI", "RMSEA", "RMSEA_CI_low", "RMSEA_CI_high", |
154 | | - "p_RMSEA", "RMR", "SRMR", "RFI", "PNFI", "IFI", "RNI", "Loglikelihood", |
155 | | - "AIC", "BIC", "BIC_adjusted" |
156 | | - ) |
157 | | - |
158 | | - if (is.character(metrics) && !all(metrics == "all")) { |
159 | | - out_names <- intersect(metrics, all_metric_names) |
160 | | - } else { |
161 | | - out_names <- all_metric_names |
| 158 | + if (verbose) { |
| 159 | + insight::format_warning("This lavaan model did not converge, no performance indices can be computed. Returning `NA`.") |
162 | 160 | } |
163 | | - |
164 | 161 | out <- as.data.frame(stats::setNames( |
165 | 162 | rep(list(NA), length(out_names)), |
166 | 163 | out_names |
167 | 164 | )) |
168 | 165 | } |
169 | 166 |
|
| 167 | + # select only the requested metrics |
| 168 | + out <- out[out_names] |
| 169 | + |
170 | 170 | class(out) <- c("performance_lavaan", "performance_model", class(out)) |
171 | 171 | out |
172 | 172 | } |
|
0 commit comments