-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from brianmsm/compare-model-fit-initial
compare-model-fit-initial
- Loading branch information
Showing
9 changed files
with
232 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
S3method(model_fit,lavaan) | ||
S3method(print,compare_model_fit) | ||
S3method(print,model_fit) | ||
export(compare_model_fit) | ||
export(model_fit) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#' Compare Model Fit Indices Across Multiple Models | ||
#' | ||
#' @description `compare_model_fit()` compares the fit indices of two or more | ||
#' models. It extracts the fit indices using `model_fit` and combines them into | ||
#' a single data frame for easy comparison. | ||
#' | ||
#' @param ... Two or more model objects to be compared. | ||
#' @param type A character string specifying the type of fit indices to extract. | ||
#' Options are `"standard"`, `"scaled"`, and `"robust"`. Defaults to `NULL`, | ||
#' which automatically selects `"scaled"` if a robust estimator is used, otherwise `"standard"`. | ||
#' @param metrics A character vector specifying which fit indices to extract. | ||
#' Defaults to `"essential"`, or a custom vector of indices. | ||
#' @param verbose Logical. If `TRUE`, prints messages about the indices being adjusted. | ||
#' @return A data frame containing the fit indices for each model, with an additional column identifying the models. | ||
#' @export | ||
#' @examples | ||
#' library(lavaan) | ||
#' model1 <- 'visual =~ x1 + x2 + x3' | ||
#' model2 <- 'visual =~ x1 + x2 + x3 + x4' | ||
#' fit1 <- cfa(model1, data = HolzingerSwineford1939, estimator = "MLR") | ||
#' fit2 <- cfa(model2, data = HolzingerSwineford1939, estimator = "MLR") | ||
#' compare_model_fit(fit1, fit2) | ||
|
||
compare_model_fit <- function(..., type = NULL, metrics = "essential", verbose = TRUE) { | ||
# Capture all the fit objects as a list | ||
fits <- list(...) | ||
|
||
# Ensure at least two models are provided for comparison | ||
if (length(fits) < 2) { | ||
cli::cli_alert_danger("At least two model fits must be provided for comparison.") | ||
stop("At least two model fits must be provided for comparison.") | ||
} | ||
|
||
# Apply model_fit to each model in the list | ||
fit_measures <- lapply(fits, model_fit, type = type, metrics = metrics, verbose = verbose) | ||
|
||
# Combine the dataframes vertically | ||
combined_measures <- do.call(rbind, fit_measures) | ||
|
||
# Add a column to identify each model | ||
model_names <- sapply(substitute(list(...))[-1L], deparse) | ||
combined_measures$model <- rep(model_names, times = sapply(fit_measures, nrow)) | ||
|
||
# Reorder columns so that "model" is the first column | ||
combined_measures <- combined_measures[, c("model", setdiff(names(combined_measures), "model"))] | ||
|
||
# Assign the custom class for print method | ||
class(combined_measures) <- c("compare_model_fit", class(combined_measures)) | ||
|
||
return(combined_measures) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' Print Method for compare_model_fit Objects | ||
#' | ||
#' @description This function provides a custom print method for objects of class | ||
#' `compare_model_fit`, which are generated by the `compare_model_fit()` function. | ||
#' It formats and displays the comparison of model fit indices in a clear and | ||
#' readable table. | ||
#' | ||
#' @param x An object of class `compare_model_fit`, created by the `compare_model_fit()` function. | ||
#' @param digits Integer. Number of digits to use for displaying numeric values. Defaults to 3. | ||
#' @param p_digits Integer. Number of digits to use for displaying p-values. Defaults to 3. | ||
#' @param format Character. The format in which to print the table. Options are `"text"`, `"markdown"`, or `"html"`. Defaults to `"text"`. | ||
#' @param ... Additional arguments passed to `insight::export_table()`. | ||
#' | ||
#' @return Invisibly returns the `compare_model_fit` object. The main purpose of this function is to print the formatted comparison table. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' library(psymetrics) | ||
#' library(lavaan) | ||
#' | ||
#' model1 <- 'visual =~ x1 + x2 + x3' | ||
#' model2 <- 'visual =~ x1 + x2 + x3 + x4' | ||
#' fit1 <- cfa(model1, data = HolzingerSwineford1939, estimator = "MLR") | ||
#' fit2 <- cfa(model2, data = HolzingerSwineford1939, estimator = "MLR") | ||
#' comparison <- compare_model_fit(fit1, fit2) | ||
#' print(comparison, digits = 2) | ||
|
||
|
||
print.compare_model_fit <- function(x, digits = 3, p_digits = 3, format = "text", ...) { | ||
# Format the comparison table | ||
formatted_table <- insight::format_table(x, digits = digits, p_digits = p_digits) | ||
|
||
# Print the formatted table | ||
cat( | ||
insight::export_table( | ||
x = formatted_table, | ||
digits = digits, | ||
format = format, | ||
caption = c("# Model Fit Comparison:", "blue"), | ||
... | ||
) | ||
) | ||
|
||
# Return the object invisibly | ||
invisible(x) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.