From 21084703bfe84563e39b84218e11847212aa9779 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 18 Jun 2025 23:44:57 +0200 Subject: [PATCH 01/14] Support for psych::omega models Fixes #136 --- DESCRIPTION | 2 +- NAMESPACE | 4 ++ NEWS.md | 2 +- R/display.R | 6 +++ R/methods_psych.R | 104 ++++++++++++++++++++++++++++++++++++++-------- R/print_md.R | 35 ++++++++++++++++ R/utils_pca_efa.R | 56 +++++++++++++++++-------- 7 files changed, 171 insertions(+), 38 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index f4971b5f4..73113bec6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: parameters Title: Processing of Model Parameters -Version: 0.26.0.4 +Version: 0.26.0.5 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/NAMESPACE b/NAMESPACE index ba98b0bf8..49e4652ac 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -126,6 +126,8 @@ S3method(display,parameters_brms_meta) S3method(display,parameters_efa) S3method(display,parameters_efa_summary) S3method(display,parameters_model) +S3method(display,parameters_omega) +S3method(display,parameters_omega_summary) S3method(display,parameters_pca) S3method(display,parameters_pca_summary) S3method(display,parameters_sem) @@ -621,6 +623,8 @@ S3method(print_md,parameters_brms_meta) S3method(print_md,parameters_efa) S3method(print_md,parameters_efa_summary) S3method(print_md,parameters_model) +S3method(print_md,parameters_omega) +S3method(print_md,parameters_omega_summary) S3method(print_md,parameters_p_function) S3method(print_md,parameters_pca) S3method(print_md,parameters_pca_summary) diff --git a/NEWS.md b/NEWS.md index 7ec62b0c6..ef8039f6d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -12,7 +12,7 @@ * New function `factor_scores()` to extract factor scores from EFA (`psych::fa()` or `factor_analysis()`). -* Added and/or improved print-methods for all functions around PCA and FA. +* Added and/or improved print-methods for all functions around PCA, FA and Omega. * Improved efficiency in `model_parameters()` for models from packages *brms* and *rstanarm*. diff --git a/R/display.R b/R/display.R index c35c80fd1..ef2cae5f8 100644 --- a/R/display.R +++ b/R/display.R @@ -232,6 +232,9 @@ display.parameters_efa_summary <- function(object, format = "markdown", digits = #' @export display.parameters_pca_summary <- display.parameters_efa_summary +#' @export +display.parameters_omega_summary <- display.parameters_efa_summary + #' @inheritParams model_parameters.principal #' @rdname display.parameters_model @@ -257,6 +260,9 @@ display.parameters_efa <- function(object, format = "markdown", digits = 2, sort #' @export display.parameters_pca <- display.parameters_efa +#' @export +display.parameters_omega <- display.parameters_efa + # Equivalence tests ------------------------ diff --git a/R/methods_psych.R b/R/methods_psych.R index b3bafaccc..53d26f991 100644 --- a/R/methods_psych.R +++ b/R/methods_psych.R @@ -224,29 +224,77 @@ model_parameters.fa.ci <- model_parameters.fa #' @export -model_parameters.omega <- function(model, verbose = TRUE, ...) { - # Table of omega coefficients - table_om <- model$omega.group - colnames(table_om) <- c("Omega_Total", "Omega_Hierarchical", "Omega_Group") - table_om$Composite <- row.names(table_om) - row.names(table_om) <- NULL - table_om <- table_om[c("Composite", names(table_om)[names(table_om) != "Composite"])] +model_parameters.omega <- function(model, + sort = FALSE, + threshold = NULL, + labels = NULL, + verbose = TRUE, + ...) { + # n + n <- model$stats$factors - # Get summary: Table of Variance - table_var <- as.data.frame(unclass(model$omega.group)) - table_var$Composite <- rownames(model$omega.group) - table_var$Total <- table_var$total * 100 - table_var$General <- table_var$general * 100 - table_var$Group <- table_var$group * 100 - table_var <- table_var[c("Composite", "Total", "General", "Group")] + # Get summary + data_summary <- .get_omega_variance_summary(model) + + # Get omega coefficients + omega_coefficients <- .get_omega_coefficients_summary(model) + + # Get loadings + loadings <- as.data.frame(unclass(model$schmid$sl)) + + # Format + loadings <- cbind(data.frame(Variable = row.names(loadings)), loadings) + row.names(loadings) <- NULL + + # Labels + if (!is.null(labels)) { + loadings$Label <- labels + loadings <- loadings[c("Variable", "Label", names(loadings)[!names(loadings) %in% c("Variable", "Label")])] + loading_cols <- 3:(n + 4) + } else { + loading_cols <- 2:(n + 3) + } + + # Add information + colnames(loadings)[colnames(loadings) == "com"] <- "Complexity" - out <- table_om - attr(out, "summary") <- table_var - class(out) <- c("parameters_omega", class(out)) - out + # Add attributes + attr(loadings, "summary") <- data_summary + attr(loadings, "omega_coefficients") <- omega_coefficients + attr(loadings, "model") <- model + attr(loadings, "rotation") <- model$rotation + attr(loadings, "scores") <- model$scores + attr(loadings, "additional_arguments") <- list(...) + attr(loadings, "n") <- n + attr(loadings, "threshold") <- threshold + attr(loadings, "sort") <- sort + attr(loadings, "loadings_columns") <- loading_cols + + # Sorting + if (isTRUE(sort)) { + loadings <- .sort_loadings(loadings) + } + + # Add some more attributes + attr(loadings, "loadings_long") <- .long_loadings(loadings, threshold = threshold, loadings_columns = loading_cols) + # here we match the original columns in the data set with the assigned components + # for each variable, so we know which column in the original data set belongs + # to which extracted component... + attr(loadings, "closest_component") <- .closest_component( + loadings, + loadings_columns = loading_cols, + variable_names = rownames(model$schmid$sl) + ) + + # add class-attribute for printing + class(loadings) <- c("parameters_omega", class(loadings)) + loadings } +# helper ------------------------------------------------ + + .get_fa_variance_summary <- function(model) { n <- model$factors variance <- as.data.frame(unclass(model$Vaccounted)) @@ -268,3 +316,23 @@ model_parameters.omega <- function(model, verbose = TRUE, ...) { data_summary } + + +.get_omega_variance_summary <- function(model) { + # Get summary: Table of Variance + table_var <- as.data.frame(unclass(model$omega.group)) + table_var$Composite <- rownames(model$omega.group) + table_var$Total <- table_var$total * 100 + table_var$General <- table_var$general * 100 + table_var$Group <- table_var$group * 100 + table_var[c("Composite", "Total", "General", "Group")] +} + +.get_omega_coefficients_summary <- function(model) { + # Table of omega coefficients + table_om <- model$omega.group + colnames(table_om) <- c("Omega_Total", "Omega_Hierarchical", "Omega_Group") + table_om$Composite <- row.names(table_om) + row.names(table_om) <- NULL + table_om[c("Composite", names(table_om)[names(table_om) != "Composite"])] +} diff --git a/R/print_md.R b/R/print_md.R index 959a495c7..335d9234c 100644 --- a/R/print_md.R +++ b/R/print_md.R @@ -273,6 +273,38 @@ print_md.parameters_efa_summary <- function(x, digits = 3, ...) { #' @export print_md.parameters_pca_summary <- print_md.parameters_efa_summary +#' @export +print_md.parameters_omega_summary <- function(x, ...) { + orig_x <- x + + # extract summary tables + omega_coefficients <- attributes(x)$omega_coefficients + variance_summary <- attributes(x)$summary + + # rename columns + if (!is.null(omega_coefficients)) { + names(omega_coefficients) <- c( + "Composite", "Omega (total)", "Omega (hierarchical)", "Omega (group)" + ) + caption1 <- "Omega Coefficients" + } + if (!is.null(variance_summary)) { + names(variance_summary) <- c( + "Composite", "Total (%)", "General Factor (%)", + "Group Factor (%)" + ) + caption2 <- "Variances" + } + + # list for export + out <- insight::compact_list(list(omega_coefficients, variance_summary)) + captions <- insight::compact_list(list(caption1, caption2)) + + cat(insight::export_table(out, caption = captions, format = "markdown", ...)) + invisible(orig_x) +} + + #' @export print_md.parameters_efa <- function(x, digits = 2, @@ -298,6 +330,9 @@ print_md.parameters_efa <- function(x, #' @export print_md.parameters_pca <- print_md.parameters_efa +#' @export +print_md.parameters_omega <- print_md.parameters_efa + # Equivalence test ---------------------------- diff --git a/R/utils_pca_efa.R b/R/utils_pca_efa.R index 46c9f2f2c..256d17b2e 100644 --- a/R/utils_pca_efa.R +++ b/R/utils_pca_efa.R @@ -129,9 +129,8 @@ summary.parameters_pca <- summary.parameters_efa #' @export summary.parameters_omega <- function(object, ...) { - table_var <- attributes(object)$summary - class(table_var) <- c("parameters_omega_summary", class(table_var)) - table_var + class(object) <- c("parameters_omega_summary", "data.frame") + object } @@ -284,28 +283,41 @@ print.parameters_efa <- function(x, invisible(x) } - #' @export print.parameters_pca <- print.parameters_efa - #' @export -print.parameters_omega <- function(x, ...) { - orig_x <- x - names(x) <- c("Composite", "Omega (total)", "Omega (hierarchical)", "Omega (group)") - cat(insight::export_table(x)) - invisible(orig_x) -} +print.parameters_omega <- print.parameters_efa #' @export print.parameters_omega_summary <- function(x, ...) { orig_x <- x - names(x) <- c( - "Composite", "Total Variance (%)", "Variance due to General Factor (%)", - "Variance due to Group Factor (%)" - ) - cat(insight::export_table(x)) + + # extract summary tables + omega_coefficients <- attributes(x)$omega_coefficients + variance_summary <- attributes(x)$summary + + # rename columns + if (!is.null(omega_coefficients)) { + names(omega_coefficients) <- c( + "Composite", "Omega (total)", "Omega (hierarchical)", "Omega (group)" + ) + caption1 <- c("# Omega Coefficients", "blue") + } + if (!is.null(variance_summary)) { + names(variance_summary) <- c( + "Composite", "Total (%)", "General Factor (%)", + "Group Factor (%)" + ) + caption2 <- c("# Variances", "blue") + } + + # list for export + out <- insight::compact_list(list(omega_coefficients, variance_summary)) + captions <- insight::compact_list(list(caption1, caption2)) + + cat(insight::export_table(out, caption = captions, format = "text", ...)) invisible(orig_x) } @@ -317,8 +329,10 @@ print.parameters_omega_summary <- function(x, ...) { # Method if (inherits(x, "parameters_pca")) { method <- "Principal Component Analysis" - } else { + } else if (inherits(x, "parameters_efa")) { method <- "Factor Analysis" + } else { + method <- "Omega" } # Rotation @@ -341,7 +355,13 @@ print.parameters_omega_summary <- function(x, ...) { } # table caption - if (is.null(rotation_name) || rotation_name == "none") { + if (method == "Omega") { + if (format %in% c("markdown", "html")) { + table_caption <- "Omega" + } else { + table_caption <- c("# Omega", "blue") + } + } else if (is.null(rotation_name) || rotation_name == "none") { if (format %in% c("markdown", "html")) { table_caption <- sprintf("Loadings from %s (no rotation)", method) } else { From ef5082d7e45499ed72fdad497679a732af5407b0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 19 Jun 2025 10:58:06 +0200 Subject: [PATCH 02/14] address comments --- R/methods_psych.R | 1 - R/print_md.R | 2 ++ R/utils_pca_efa.R | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/R/methods_psych.R b/R/methods_psych.R index 53d26f991..9c94181c7 100644 --- a/R/methods_psych.R +++ b/R/methods_psych.R @@ -228,7 +228,6 @@ model_parameters.omega <- function(model, sort = FALSE, threshold = NULL, labels = NULL, - verbose = TRUE, ...) { # n n <- model$stats$factors diff --git a/R/print_md.R b/R/print_md.R index 335d9234c..85a8c59d4 100644 --- a/R/print_md.R +++ b/R/print_md.R @@ -276,6 +276,8 @@ print_md.parameters_pca_summary <- print_md.parameters_efa_summary #' @export print_md.parameters_omega_summary <- function(x, ...) { orig_x <- x + caption1 <- NULL + caption2 <- NULL # extract summary tables omega_coefficients <- attributes(x)$omega_coefficients diff --git a/R/utils_pca_efa.R b/R/utils_pca_efa.R index 256d17b2e..4df2ff535 100644 --- a/R/utils_pca_efa.R +++ b/R/utils_pca_efa.R @@ -293,6 +293,8 @@ print.parameters_omega <- print.parameters_efa #' @export print.parameters_omega_summary <- function(x, ...) { orig_x <- x + caption1 <- NULL + caption2 <- NULL # extract summary tables omega_coefficients <- attributes(x)$omega_coefficients From dfd8c3e1d7881b82446a255c8e8ea029f07830c7 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 19 Jun 2025 11:05:06 +0200 Subject: [PATCH 03/14] extract scores, docs --- NAMESPACE | 2 + R/factor_scores.R | 15 +++- R/methods_psych.R | 111 +++++++++++++--------------- man/factor_scores.Rd | 7 +- man/model_parameters.principal.Rd | 118 ++++++++++++++---------------- 5 files changed, 126 insertions(+), 127 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 49e4652ac..4049d34a9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -152,7 +152,9 @@ S3method(equivalence_test,wbm) S3method(equivalence_test,zeroinfl) S3method(factor_analysis,data.frame) S3method(factor_scores,fa) +S3method(factor_scores,omega) S3method(factor_scores,parameters_efa) +S3method(factor_scores,parameters_omega) S3method(format,compare_parameters) S3method(format,equivalence_test_lm) S3method(format,p_calibrate) diff --git a/R/factor_scores.R b/R/factor_scores.R index 8a1648a42..f8592135f 100644 --- a/R/factor_scores.R +++ b/R/factor_scores.R @@ -1,9 +1,10 @@ -#' Extract factor scores from Factor Analysis (EFA) +#' Extract factor scores from Factor Analysis (EFA) or Omega #' #' `factor_scores()` extracts the factor scores from objects returned by -#' [`psych::fa()`] or [`factor_analysis()`]. +#' [`psych::fa()`], [`factor_analysis()`], or [`psych::omega()`] #' -#' @param x An object returned by [`psych::fa()`] or [`factor_analysis()`]. +#' @param x An object returned by [`psych::fa()`], [`factor_analysis()`], or +#' [`psych::omega()`]. #' @param ... Currently unused. #' #' @return A data frame with the factor scores. It simply extracts the `$scores` @@ -24,6 +25,11 @@ factor_scores.fa <- function(x, ...) { as.data.frame(x$scores) } +#' @export +factor_scores.omega <- function(x, ...) { + as.data.frame(x$scores) +} + #' @export factor_scores.parameters_efa <- function(x, ...) { model <- attributes(x)$model @@ -32,3 +38,6 @@ factor_scores.parameters_efa <- function(x, ...) { } as.data.frame(model$scores) } + +#' @export +factor_scores.parameters_omega <- factor_scores.parameters_efa diff --git a/R/methods_psych.R b/R/methods_psych.R index 9c94181c7..7fabd779a 100644 --- a/R/methods_psych.R +++ b/R/methods_psych.R @@ -46,81 +46,74 @@ #' PCA. The value should be > 0.6, and desirable values are > 0.8 #' (\cite{Tabachnick and Fidell, 2013}). #' -#' @examples -#' \donttest{ +#' @examplesIf all(insight::check_if_installed(c("psych", "lavaan"), quietly = TRUE)) #' library(parameters) -#' if (require("psych", quietly = TRUE)) { -#' # Principal Component Analysis (PCA) --------- -#' pca <- psych::principal(attitude) -#' model_parameters(pca) +#' \donttest{ +#' # Principal Component Analysis (PCA) --------- +#' pca <- psych::principal(attitude) +#' model_parameters(pca) #' -#' pca <- psych::principal(attitude, nfactors = 3, rotate = "none") -#' model_parameters(pca, sort = TRUE, threshold = 0.2) +#' pca <- psych::principal(attitude, nfactors = 3, rotate = "none") +#' model_parameters(pca, sort = TRUE, threshold = 0.2) #' -#' principal_components(attitude, n = 3, sort = TRUE, threshold = 0.2) +#' principal_components(attitude, n = 3, sort = TRUE, threshold = 0.2) #' #' -#' # Exploratory Factor Analysis (EFA) --------- -#' efa <- psych::fa(attitude, nfactors = 3) -#' model_parameters(efa, -#' threshold = "max", sort = TRUE, -#' labels = as.character(1:ncol(attitude)) -#' ) +#' # Exploratory Factor Analysis (EFA) --------- +#' efa <- psych::fa(attitude, nfactors = 3) +#' model_parameters(efa, +#' threshold = "max", sort = TRUE, +#' labels = as.character(1:ncol(attitude)) +#' ) #' #' -#' # Omega --------- -#' omega <- psych::omega(mtcars, nfactors = 3) -#' params <- model_parameters(omega) -#' params -#' summary(params) -#' } +#' # Omega --------- +#' omega <- psych::omega(mtcars, nfactors = 3, plot = FALSE) +#' params <- model_parameters(omega) +#' params +#' summary(params) #' } #' -#' # lavaan -#' -#' library(parameters) #' #' # lavaan ------------------------------------- -#' if (require("lavaan", quietly = TRUE)) { -#' # Confirmatory Factor Analysis (CFA) --------- +#' # Confirmatory Factor Analysis (CFA) --------- #' -#' structure <- " visual =~ x1 + x2 + x3 -#' textual =~ x4 + x5 + x6 -#' speed =~ x7 + x8 + x9 " -#' model <- lavaan::cfa(structure, data = HolzingerSwineford1939) -#' model_parameters(model) -#' model_parameters(model, standardize = TRUE) +#' structure <- " visual =~ x1 + x2 + x3 +#' textual =~ x4 + x5 + x6 +#' speed =~ x7 + x8 + x9 " +#' model <- lavaan::cfa(structure, data = HolzingerSwineford1939) +#' model_parameters(model) +#' model_parameters(model, standardize = TRUE) #' -#' # filter parameters -#' model_parameters( -#' model, -#' parameters = list( -#' To = "^(?!visual)", -#' From = "^(?!(x7|x8))" -#' ) +#' # filter parameters +#' model_parameters( +#' model, +#' parameters = list( +#' To = "^(?!visual)", +#' From = "^(?!(x7|x8))" #' ) +#' ) #' -#' # Structural Equation Model (SEM) ------------ +#' # Structural Equation Model (SEM) ------------ #' -#' structure <- " -#' # latent variable definitions -#' ind60 =~ x1 + x2 + x3 -#' dem60 =~ y1 + a*y2 + b*y3 + c*y4 -#' dem65 =~ y5 + a*y6 + b*y7 + c*y8 -#' # regressions -#' dem60 ~ ind60 -#' dem65 ~ ind60 + dem60 -#' # residual correlations -#' y1 ~~ y5 -#' y2 ~~ y4 + y6 -#' y3 ~~ y7 -#' y4 ~~ y8 -#' y6 ~~ y8 -#' " -#' model <- lavaan::sem(structure, data = PoliticalDemocracy) -#' model_parameters(model) -#' model_parameters(model, standardize = TRUE) -#' } +#' structure <- " +#' # latent variable definitions +#' ind60 =~ x1 + x2 + x3 +#' dem60 =~ y1 + a*y2 + b*y3 + c*y4 +#' dem65 =~ y5 + a*y6 + b*y7 + c*y8 +#' # regressions +#' dem60 ~ ind60 +#' dem65 ~ ind60 + dem60 +#' # residual correlations +#' y1 ~~ y5 +#' y2 ~~ y4 + y6 +#' y3 ~~ y7 +#' y4 ~~ y8 +#' y6 ~~ y8 +#' " +#' model <- lavaan::sem(structure, data = PoliticalDemocracy) +#' model_parameters(model) +#' model_parameters(model, standardize = TRUE) #' #' @return A data frame of indices or loadings. #' @references diff --git a/man/factor_scores.Rd b/man/factor_scores.Rd index 8f8b2a5cf..9ea748634 100644 --- a/man/factor_scores.Rd +++ b/man/factor_scores.Rd @@ -2,12 +2,13 @@ % Please edit documentation in R/factor_scores.R \name{factor_scores} \alias{factor_scores} -\title{Extract factor scores from Factor Analysis (EFA)} +\title{Extract factor scores from Factor Analysis (EFA) or Omega} \usage{ factor_scores(x, ...) } \arguments{ -\item{x}{An object returned by \code{\link[psych:fa]{psych::fa()}} or \code{\link[=factor_analysis]{factor_analysis()}}.} +\item{x}{An object returned by \code{\link[psych:fa]{psych::fa()}}, \code{\link[=factor_analysis]{factor_analysis()}}, or +\code{\link[psych:omega]{psych::omega()}}.} \item{...}{Currently unused.} } @@ -17,7 +18,7 @@ element from the object and converts it into a data frame. } \description{ \code{factor_scores()} extracts the factor scores from objects returned by -\code{\link[psych:fa]{psych::fa()}} or \code{\link[=factor_analysis]{factor_analysis()}}. +\code{\link[psych:fa]{psych::fa()}}, \code{\link[=factor_analysis]{factor_analysis()}}, or \code{\link[psych:omega]{psych::omega()}} } \examples{ \dontshow{if (insight::check_if_installed("psych", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} diff --git a/man/model_parameters.principal.Rd b/man/model_parameters.principal.Rd index 6d206c4c0..ce2c69810 100644 --- a/man/model_parameters.principal.Rd +++ b/man/model_parameters.principal.Rd @@ -112,81 +112,75 @@ for \code{lavaan} models implemented in the \href{https://easystats.github.io/see/}{\strong{see}-package}. } \examples{ -\donttest{ +\dontshow{if (all(insight::check_if_installed(c("psych", "lavaan"), quietly = TRUE))) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} library(parameters) -if (require("psych", quietly = TRUE)) { - # Principal Component Analysis (PCA) --------- - pca <- psych::principal(attitude) - model_parameters(pca) +\donttest{ +# Principal Component Analysis (PCA) --------- +pca <- psych::principal(attitude) +model_parameters(pca) - pca <- psych::principal(attitude, nfactors = 3, rotate = "none") - model_parameters(pca, sort = TRUE, threshold = 0.2) +pca <- psych::principal(attitude, nfactors = 3, rotate = "none") +model_parameters(pca, sort = TRUE, threshold = 0.2) - principal_components(attitude, n = 3, sort = TRUE, threshold = 0.2) +principal_components(attitude, n = 3, sort = TRUE, threshold = 0.2) - # Exploratory Factor Analysis (EFA) --------- - efa <- psych::fa(attitude, nfactors = 3) - model_parameters(efa, - threshold = "max", sort = TRUE, - labels = as.character(1:ncol(attitude)) - ) +# Exploratory Factor Analysis (EFA) --------- +efa <- psych::fa(attitude, nfactors = 3) +model_parameters(efa, + threshold = "max", sort = TRUE, + labels = as.character(1:ncol(attitude)) +) - # Omega --------- - omega <- psych::omega(mtcars, nfactors = 3) - params <- model_parameters(omega) - params - summary(params) +# Omega --------- +omega <- psych::omega(mtcars, nfactors = 3, plot = FALSE) +params <- model_parameters(omega) +params +summary(params) } -} - -# lavaan -library(parameters) # lavaan ------------------------------------- -if (require("lavaan", quietly = TRUE)) { - # Confirmatory Factor Analysis (CFA) --------- - - structure <- " visual =~ x1 + x2 + x3 - textual =~ x4 + x5 + x6 - speed =~ x7 + x8 + x9 " - model <- lavaan::cfa(structure, data = HolzingerSwineford1939) - model_parameters(model) - model_parameters(model, standardize = TRUE) - - # filter parameters - model_parameters( - model, - parameters = list( - To = "^(?!visual)", - From = "^(?!(x7|x8))" - ) - ) +# Confirmatory Factor Analysis (CFA) --------- - # Structural Equation Model (SEM) ------------ - - structure <- " - # latent variable definitions - ind60 =~ x1 + x2 + x3 - dem60 =~ y1 + a*y2 + b*y3 + c*y4 - dem65 =~ y5 + a*y6 + b*y7 + c*y8 - # regressions - dem60 ~ ind60 - dem65 ~ ind60 + dem60 - # residual correlations - y1 ~~ y5 - y2 ~~ y4 + y6 - y3 ~~ y7 - y4 ~~ y8 - y6 ~~ y8 - " - model <- lavaan::sem(structure, data = PoliticalDemocracy) - model_parameters(model) - model_parameters(model, standardize = TRUE) -} +structure <- " visual =~ x1 + x2 + x3 + textual =~ x4 + x5 + x6 + speed =~ x7 + x8 + x9 " +model <- lavaan::cfa(structure, data = HolzingerSwineford1939) +model_parameters(model) +model_parameters(model, standardize = TRUE) + +# filter parameters +model_parameters( + model, + parameters = list( + To = "^(?!visual)", + From = "^(?!(x7|x8))" + ) +) +# Structural Equation Model (SEM) ------------ + +structure <- " + # latent variable definitions + ind60 =~ x1 + x2 + x3 + dem60 =~ y1 + a*y2 + b*y3 + c*y4 + dem65 =~ y5 + a*y6 + b*y7 + c*y8 + # regressions + dem60 ~ ind60 + dem65 ~ ind60 + dem60 + # residual correlations + y1 ~~ y5 + y2 ~~ y4 + y6 + y3 ~~ y7 + y4 ~~ y8 + y6 ~~ y8 +" +model <- lavaan::sem(structure, data = PoliticalDemocracy) +model_parameters(model) +model_parameters(model, standardize = TRUE) +\dontshow{\}) # examplesIf} } \references{ \itemize{ From 42bc40c116a48f6be817d04a02f8e2431acd5dc9 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 19 Jun 2025 11:22:51 +0200 Subject: [PATCH 04/14] rotation --- R/methods_psych.R | 6 +++++- R/utils_pca_efa.R | 8 +------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/R/methods_psych.R b/R/methods_psych.R index 7fabd779a..2e3c166a9 100644 --- a/R/methods_psych.R +++ b/R/methods_psych.R @@ -249,12 +249,16 @@ model_parameters.omega <- function(model, # Add information colnames(loadings)[colnames(loadings) == "com"] <- "Complexity" + rotation <- model$Call$rotate + if (is.null(rotation)) { + rotation <- "oblimin" + } # Add attributes attr(loadings, "summary") <- data_summary attr(loadings, "omega_coefficients") <- omega_coefficients attr(loadings, "model") <- model - attr(loadings, "rotation") <- model$rotation + attr(loadings, "rotation") <- rotation attr(loadings, "scores") <- model$scores attr(loadings, "additional_arguments") <- list(...) attr(loadings, "n") <- n diff --git a/R/utils_pca_efa.R b/R/utils_pca_efa.R index 4df2ff535..bf3afa968 100644 --- a/R/utils_pca_efa.R +++ b/R/utils_pca_efa.R @@ -357,13 +357,7 @@ print.parameters_omega_summary <- function(x, ...) { } # table caption - if (method == "Omega") { - if (format %in% c("markdown", "html")) { - table_caption <- "Omega" - } else { - table_caption <- c("# Omega", "blue") - } - } else if (is.null(rotation_name) || rotation_name == "none") { + if (is.null(rotation_name) || rotation_name == "none") { if (format %in% c("markdown", "html")) { table_caption <- sprintf("Loadings from %s (no rotation)", method) } else { From 57b6eb4678c5b40730fcecb24a08c156045326eb Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 19 Jun 2025 11:35:04 +0200 Subject: [PATCH 05/14] update --- R/print_md.R | 32 +++---------------------------- R/utils_pca_efa.R | 48 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/R/print_md.R b/R/print_md.R index 85a8c59d4..f667ffbcc 100644 --- a/R/print_md.R +++ b/R/print_md.R @@ -275,35 +275,9 @@ print_md.parameters_pca_summary <- print_md.parameters_efa_summary #' @export print_md.parameters_omega_summary <- function(x, ...) { - orig_x <- x - caption1 <- NULL - caption2 <- NULL - - # extract summary tables - omega_coefficients <- attributes(x)$omega_coefficients - variance_summary <- attributes(x)$summary - - # rename columns - if (!is.null(omega_coefficients)) { - names(omega_coefficients) <- c( - "Composite", "Omega (total)", "Omega (hierarchical)", "Omega (group)" - ) - caption1 <- "Omega Coefficients" - } - if (!is.null(variance_summary)) { - names(variance_summary) <- c( - "Composite", "Total (%)", "General Factor (%)", - "Group Factor (%)" - ) - caption2 <- "Variances" - } - - # list for export - out <- insight::compact_list(list(omega_coefficients, variance_summary)) - captions <- insight::compact_list(list(caption1, caption2)) - - cat(insight::export_table(out, caption = captions, format = "markdown", ...)) - invisible(orig_x) + out <- .print_omega_summary(x, format = "markdown") + cat(insight::export_table(out$tables, caption = out$captions, format = "markdown", ...)) + invisible(x) } diff --git a/R/utils_pca_efa.R b/R/utils_pca_efa.R index bf3afa968..0e5ac3238 100644 --- a/R/utils_pca_efa.R +++ b/R/utils_pca_efa.R @@ -292,9 +292,33 @@ print.parameters_omega <- print.parameters_efa #' @export print.parameters_omega_summary <- function(x, ...) { - orig_x <- x + out <- .print_omega_summary(x) + cat(insight::export_table(out$tables, caption = out$captions, format = "text", ...)) + invisible(x) +} + + +# print-helper ---------------------- + + +.print_omega_summary <- function(x, format = "text") { caption1 <- NULL caption2 <- NULL + caption3 <- NULL + + # extract model + model <- attributes(x)$model + if (!is.null(model)) { + stats <- data.frame( + Statistic = c("Alpha", "G.6", "Omega (hierachical)", "Omega (asymptotic H)", "Omega (total)"), + Coefficient = c(model$alpha, model$G6, model$omega_h, model$omega.lim, model$omega.tot) + ) + if (format == "text") { + caption1 <- c("# Omega Statistics", "blue") + } else { + caption1 <- "Omega Statistics" + } + } # extract summary tables omega_coefficients <- attributes(x)$omega_coefficients @@ -305,28 +329,32 @@ print.parameters_omega_summary <- function(x, ...) { names(omega_coefficients) <- c( "Composite", "Omega (total)", "Omega (hierarchical)", "Omega (group)" ) - caption1 <- c("# Omega Coefficients", "blue") + if (format == "text") { + caption2 <- c("# Omega Coefficients", "blue") + } else { + caption2 <- "Omega Coefficients" + } } if (!is.null(variance_summary)) { names(variance_summary) <- c( "Composite", "Total (%)", "General Factor (%)", "Group Factor (%)" ) - caption2 <- c("# Variances", "blue") + if (format == "text") { + caption3 <- c("# Variances", "blue") + } else { + caption3 <- "Variances" + } } # list for export - out <- insight::compact_list(list(omega_coefficients, variance_summary)) - captions <- insight::compact_list(list(caption1, caption2)) + out <- insight::compact_list(list(stats, omega_coefficients, variance_summary)) + captions <- insight::compact_list(list(caption2, caption3)) - cat(insight::export_table(out, caption = captions, format = "text", ...)) - invisible(orig_x) + list(tables = out, captions = captions) } -# print-helper ---------------------- - - .print_parameters_cfa_efa <- function(x, threshold, sort, format, digits, labels, ...) { # Method if (inherits(x, "parameters_pca")) { From 0c281b1263a45f23957dc6c856811a4028c27e39 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 19 Jun 2025 12:26:23 +0200 Subject: [PATCH 06/14] fix --- R/utils_pca_efa.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/utils_pca_efa.R b/R/utils_pca_efa.R index 0e5ac3238..8607c238e 100644 --- a/R/utils_pca_efa.R +++ b/R/utils_pca_efa.R @@ -349,7 +349,7 @@ print.parameters_omega_summary <- function(x, ...) { # list for export out <- insight::compact_list(list(stats, omega_coefficients, variance_summary)) - captions <- insight::compact_list(list(caption2, caption3)) + captions <- insight::compact_list(list(caption1, caption2, caption3)) list(tables = out, captions = captions) } From a8f584ade05b0eafe09cf3433fad6ab42390dbd8 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 19 Jun 2025 12:31:19 +0200 Subject: [PATCH 07/14] tests --- tests/testthat/_snaps/factor_analysis.md | 84 ++++++++++++++++++++++++ tests/testthat/test-factor_analysis.R | 15 +++++ 2 files changed, 99 insertions(+) diff --git a/tests/testthat/_snaps/factor_analysis.md b/tests/testthat/_snaps/factor_analysis.md index 694d35d64..9b4487b2c 100644 --- a/tests/testthat/_snaps/factor_analysis.md +++ b/tests/testthat/_snaps/factor_analysis.md @@ -42,3 +42,87 @@ |MR1 | 1.000 | -0.366 | |MR2 | -0.366 | 1.000 | +# omega + + Code + print(out) + Output + # Rotated loadings from Omega (oblimin-rotation) + + Variable | g | F1* | F2* | F3* | h2 | u2 | p2 | Complexity + ---------------------------------------------------------------------------------------- + mpg- | 0.58 | -0.67 | 0.09 | 0.29 | 0.88 | 0.12 | 0.38 | 2.40 + cyl | 0.70 | -0.61 | 0.28 | 0.07 | 0.96 | 0.04 | 0.52 | 2.33 + disp | 0.59 | -0.71 | 0.18 | 0.11 | 0.89 | 0.11 | 0.39 | 2.13 + hp | 0.77 | -0.31 | 0.23 | 0.36 | 0.87 | 0.13 | 0.68 | 2.00 + drat- | 0.27 | -0.79 | 0.06 | -0.07 | 0.71 | 0.29 | 0.10 | 1.26 + wt | 0.43 | -0.79 | -0.04 | 0.31 | 0.91 | 0.09 | 0.20 | 1.87 + qsec- | 0.81 | 0.19 | 0.50 | 0.06 | 0.95 | 0.05 | 0.70 | 1.81 + vs- | 0.74 | -0.27 | 0.38 | 0.05 | 0.77 | 0.23 | 0.71 | 1.81 + am- | 8.38e-03 | -0.89 | -0.15 | -9.51e-03 | 0.81 | 0.19 | 8.63e-05 | 1.06 + gear | 0.03 | 0.87 | 9.01e-03 | 0.32 | 0.87 | 0.13 | 9.03e-04 | 1.27 + carb | 0.68 | 0.06 | 0.10 | 0.63 | 0.87 | 0.13 | 0.53 | 2.06 + +--- + + Code + print_md(out) + Output + + + Table: Rotated loadings from Omega (oblimin-rotation) + + |Variable | g| F1* | F2*| F3*| h2 | u2 | p2| Complexity| + |:--------|--------:|:-----|--------:|---------:|:----|:----|--------:|----------:| + |mpg- | 0.58|-0.67 | 0.09| 0.29|0.88 |0.12 | 0.38| 2.40| + |cyl | 0.70|-0.61 | 0.28| 0.07|0.96 |0.04 | 0.52| 2.33| + |disp | 0.59|-0.71 | 0.18| 0.11|0.89 |0.11 | 0.39| 2.13| + |hp | 0.77|-0.31 | 0.23| 0.36|0.87 |0.13 | 0.68| 2.00| + |drat- | 0.27|-0.79 | 0.06| -0.07|0.71 |0.29 | 0.10| 1.26| + |wt | 0.43|-0.79 | -0.04| 0.31|0.91 |0.09 | 0.20| 1.87| + |qsec- | 0.81| 0.19 | 0.50| 0.06|0.95 |0.05 | 0.70| 1.81| + |vs- | 0.74|-0.27 | 0.38| 0.05|0.77 |0.23 | 0.71| 1.81| + |am- | 8.38e-03|-0.89 | -0.15| -9.51e-03|0.81 |0.19 | 8.63e-05| 1.06| + |gear | 0.03| 0.87 | 9.01e-03| 0.32|0.87 |0.13 | 9.03e-04| 1.27| + |carb | 0.68| 0.06 | 0.10| 0.63|0.87 |0.13 | 0.53| 2.06| + +--- + + Code + print(summary(out)) + Output + # Omega Statistics + + Statistic | Coefficient + ---------------------------------- + Alpha | 0.88 + G.6 | 0.97 + Omega (hierachical) | 0.57 + Omega (asymptotic H) | 0.58 + Omega (total) | 0.97 + + # Omega Coefficients + + Composite | Omega (total) | Omega (hierarchical) | Omega (group) + ---------------------------------------------------------------- + g | 0.97 | 0.57 | 0.26 + F1* | 0.90 | 0.31 | 0.59 + F2* | 0.91 | 0.69 | 0.22 + F3* | 0.87 | 0.60 | 0.28 + + # Variances + + Composite | Total (%) | General Factor (%) | Group Factor (%) + ------------------------------------------------------------- + g | 97.28 | 56.64 | 26.42 + F1* | 90.12 | 31.07 | 59.05 + F2* | 91.37 | 69.32 | 22.04 + F3* | 87.36 | 59.65 | 27.71 + +--- + + Code + print_md(summary(out)) + Output + Table: Omega Statistics |Statistic | Coefficient| |:--------------------|-----------:| |Alpha | 0.88| |G.6 | 0.97| |Omega (hierachical) | 0.57| |Omega (asymptotic H) | 0.58| |Omega (total) | 0.97| Table: Omega Coefficients |Composite | Omega (total)| Omega (hierarchical)| Omega (group)| |:---------|-------------:|--------------------:|-------------:| |g | 0.97| 0.57| 0.26| |F1* | 0.90| 0.31| 0.59| |F2* | 0.91| 0.69| 0.22| |F3* | 0.87| 0.60| 0.28| Table: Variances |Composite | Total (%)| General Factor (%)| Group Factor (%)| |:---------|---------:|------------------:|----------------:| |g | 97.28| 56.64| 26.42| |F1* | 90.12| 31.07| 59.05| |F2* | 91.37| 69.32| 22.04| |F3* | 87.36| 59.65| 27.71| + diff --git a/tests/testthat/test-factor_analysis.R b/tests/testthat/test-factor_analysis.R index 774dc5e17..ddf9f6610 100644 --- a/tests/testthat/test-factor_analysis.R +++ b/tests/testthat/test-factor_analysis.R @@ -50,3 +50,18 @@ test_that("factor_analysis", { fc <- factor_scores(out) expect_identical(dim(fc), c(32L, 2L)) }) + + +test_that("omega", { + skip_on_cran() + skip_if_not_installed("GPArotation") + skip_if_not_installed("psych") + + model <- psych::omega(mtcars, nfactors = 3, plot = FALSE) + out <- model_parameters(model) + expect_snapshot(print(out)) + expect_snapshot(print_md(out)) + + expect_snapshot(print(summary(out))) + expect_snapshot(print_md(summary(out))) +}) From f9bc3b33431ace7f46d1a88025b218123af7a8c9 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 19 Jun 2025 12:44:04 +0200 Subject: [PATCH 08/14] fix --- R/print_md.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/print_md.R b/R/print_md.R index f667ffbcc..b52e51237 100644 --- a/R/print_md.R +++ b/R/print_md.R @@ -276,7 +276,7 @@ print_md.parameters_pca_summary <- print_md.parameters_efa_summary #' @export print_md.parameters_omega_summary <- function(x, ...) { out <- .print_omega_summary(x, format = "markdown") - cat(insight::export_table(out$tables, caption = out$captions, format = "markdown", ...)) + insight::export_table(out$tables, caption = out$captions, format = "markdown", ...) invisible(x) } From ee60c3b563ec6313c0055544294777f552722d19 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 19 Jun 2025 13:37:00 +0200 Subject: [PATCH 09/14] fix --- R/print_md.R | 1 - R/utils_pca_efa.R | 20 +++++++++----------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/R/print_md.R b/R/print_md.R index b52e51237..b7d67eebc 100644 --- a/R/print_md.R +++ b/R/print_md.R @@ -277,7 +277,6 @@ print_md.parameters_pca_summary <- print_md.parameters_efa_summary print_md.parameters_omega_summary <- function(x, ...) { out <- .print_omega_summary(x, format = "markdown") insight::export_table(out$tables, caption = out$captions, format = "markdown", ...) - invisible(x) } diff --git a/R/utils_pca_efa.R b/R/utils_pca_efa.R index 8607c238e..b21778d88 100644 --- a/R/utils_pca_efa.R +++ b/R/utils_pca_efa.R @@ -269,17 +269,15 @@ print.parameters_efa <- function(x, if (is.null(threshold)) { threshold <- attributes(x)$threshold } - cat( - .print_parameters_cfa_efa( - x, - threshold = threshold, - sort = sort, - format = "text", - digits = digits, - labels = labels, - ... - ) - ) + cat(.print_parameters_cfa_efa( + x, + threshold = threshold, + sort = sort, + format = "text", + digits = digits, + labels = labels, + ... + )) invisible(x) } From e6dea846e4019a4cb01122159e12623aa3fd87fa Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 19 Jun 2025 13:45:18 +0200 Subject: [PATCH 10/14] docs, update snaps --- R/methods_psych.R | 6 ++++- man/model_parameters.principal.Rd | 5 +++- tests/testthat/_snaps/factor_analysis.md | 30 +++++++++++++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/R/methods_psych.R b/R/methods_psych.R index 2e3c166a9..8c51d9469 100644 --- a/R/methods_psych.R +++ b/R/methods_psych.R @@ -1,6 +1,8 @@ #' Parameters from PCA, FA, CFA, SEM #' -#' Format structural models from the **psych** or **FactoMineR** packages. +#' Format structural models from the **psych** or **FactoMineR** packages. There +#' is a `summary()` method for the returned output from `model_parameters()`, to +#' show further information. See 'Examples'. #' #' @param standardize Return standardized parameters (standardized coefficients). #' Can be `TRUE` (or `"all"` or `"std.all"`) for standardized @@ -52,6 +54,7 @@ #' # Principal Component Analysis (PCA) --------- #' pca <- psych::principal(attitude) #' model_parameters(pca) +#' summary(model_parameters(pca)) #' #' pca <- psych::principal(attitude, nfactors = 3, rotate = "none") #' model_parameters(pca, sort = TRUE, threshold = 0.2) @@ -324,6 +327,7 @@ model_parameters.omega <- function(model, table_var[c("Composite", "Total", "General", "Group")] } + .get_omega_coefficients_summary <- function(model) { # Table of omega coefficients table_om <- model$omega.group diff --git a/man/model_parameters.principal.Rd b/man/model_parameters.principal.Rd index ce2c69810..73dff06e3 100644 --- a/man/model_parameters.principal.Rd +++ b/man/model_parameters.principal.Rd @@ -82,7 +82,9 @@ loadings data. Usually, the question related to the item.} A data frame of indices or loadings. } \description{ -Format structural models from the \strong{psych} or \strong{FactoMineR} packages. +Format structural models from the \strong{psych} or \strong{FactoMineR} packages. There +is a \code{summary()} method for the returned output from \code{model_parameters()}, to +show further information. See 'Examples'. } \details{ For the structural models obtained with \strong{psych}, the following indices @@ -118,6 +120,7 @@ library(parameters) # Principal Component Analysis (PCA) --------- pca <- psych::principal(attitude) model_parameters(pca) +summary(model_parameters(pca)) pca <- psych::principal(attitude, nfactors = 3, rotate = "none") model_parameters(pca, sort = TRUE, threshold = 0.2) diff --git a/tests/testthat/_snaps/factor_analysis.md b/tests/testthat/_snaps/factor_analysis.md index 9b4487b2c..d92caa631 100644 --- a/tests/testthat/_snaps/factor_analysis.md +++ b/tests/testthat/_snaps/factor_analysis.md @@ -124,5 +124,33 @@ Code print_md(summary(out)) Output - Table: Omega Statistics |Statistic | Coefficient| |:--------------------|-----------:| |Alpha | 0.88| |G.6 | 0.97| |Omega (hierachical) | 0.57| |Omega (asymptotic H) | 0.58| |Omega (total) | 0.97| Table: Omega Coefficients |Composite | Omega (total)| Omega (hierarchical)| Omega (group)| |:---------|-------------:|--------------------:|-------------:| |g | 0.97| 0.57| 0.26| |F1* | 0.90| 0.31| 0.59| |F2* | 0.91| 0.69| 0.22| |F3* | 0.87| 0.60| 0.28| Table: Variances |Composite | Total (%)| General Factor (%)| Group Factor (%)| |:---------|---------:|------------------:|----------------:| |g | 97.28| 56.64| 26.42| |F1* | 90.12| 31.07| 59.05| |F2* | 91.37| 69.32| 22.04| |F3* | 87.36| 59.65| 27.71| + + + Table: Omega Statistics + + |Statistic | Coefficient| + |:--------------------|-----------:| + |Alpha | 0.88| + |G.6 | 0.97| + |Omega (hierachical) | 0.57| + |Omega (asymptotic H) | 0.58| + |Omega (total) | 0.97| + + Table: Omega Coefficients + + |Composite | Omega (total)| Omega (hierarchical)| Omega (group)| + |:---------|-------------:|--------------------:|-------------:| + |g | 0.97| 0.57| 0.26| + |F1* | 0.90| 0.31| 0.59| + |F2* | 0.91| 0.69| 0.22| + |F3* | 0.87| 0.60| 0.28| + + Table: Variances + + |Composite | Total (%)| General Factor (%)| Group Factor (%)| + |:---------|---------:|------------------:|----------------:| + |g | 97.28| 56.64| 26.42| + |F1* | 90.12| 31.07| 59.05| + |F2* | 91.37| 69.32| 22.04| + |F3* | 87.36| 59.65| 27.71| From dc2a7c8c4de2a6f6a52fb03b5c65c8ec168f1d4a Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 19 Jun 2025 13:55:06 +0200 Subject: [PATCH 11/14] typo --- R/n_clusters_easystats.R | 2 +- R/utils_pca_efa.R | 2 +- tests/testthat/_snaps/factor_analysis.md | 53 ++++++++++++------------ 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/R/n_clusters_easystats.R b/R/n_clusters_easystats.R index 88af68244..3c389a0ff 100644 --- a/R/n_clusters_easystats.R +++ b/R/n_clusters_easystats.R @@ -289,7 +289,7 @@ print.n_clusters_dbscan <- function(x, ...) { #' @export print.n_clusters_hclust <- function(x, ...) { - insight::print_color(paste0("The bootstrap analysis of hierachical clustering highlighted ", attributes(x)$n, " significant clusters."), "green") # nolint + insight::print_color(paste0("The bootstrap analysis of hierarchical clustering highlighted ", attributes(x)$n, " significant clusters."), "green") # nolint invisible(x) } diff --git a/R/utils_pca_efa.R b/R/utils_pca_efa.R index b21778d88..ebd111a7b 100644 --- a/R/utils_pca_efa.R +++ b/R/utils_pca_efa.R @@ -308,7 +308,7 @@ print.parameters_omega_summary <- function(x, ...) { model <- attributes(x)$model if (!is.null(model)) { stats <- data.frame( - Statistic = c("Alpha", "G.6", "Omega (hierachical)", "Omega (asymptotic H)", "Omega (total)"), + Statistic = c("Alpha", "G.6", "Omega (hierarchical)", "Omega (asymptotic H)", "Omega (total)"), Coefficient = c(model$alpha, model$G6, model$omega_h, model$omega.lim, model$omega.tot) ) if (format == "text") { diff --git a/tests/testthat/_snaps/factor_analysis.md b/tests/testthat/_snaps/factor_analysis.md index d92caa631..5043ddd47 100644 --- a/tests/testthat/_snaps/factor_analysis.md +++ b/tests/testthat/_snaps/factor_analysis.md @@ -4,16 +4,16 @@ print(summary(out)) Output # (Explained) Variance of Components - + Parameter | MR1 | MR2 ----------------------------------------------- Eigenvalues | 4.947 | 1.062 Variance Explained | 0.638 | 0.220 Variance Explained (Cumulative) | 0.638 | 0.858 Variance Explained (Proportion) | 0.744 | 0.256 - + # Factor Correlations - + Factor | MR1 | MR2 ------------------------ MR1 | 1.000 | -0.366 @@ -24,19 +24,19 @@ Code print_md(summary(out)) Output - - + + Table: (Explained) Variance of Components - + |Parameter | MR1 | MR2 | |:-------------------------------|:-----:|:-----:| |Eigenvalues | 4.947 | 1.062 | |Variance Explained | 0.638 | 0.220 | |Variance Explained (Cumulative) | 0.638 | 0.858 | |Variance Explained (Proportion) | 0.744 | 0.256 | - + Table: Factor Correlations - + |Factor | MR1 | MR2 | |:------|:------:|:------:| |MR1 | 1.000 | -0.366 | @@ -48,7 +48,7 @@ print(out) Output # Rotated loadings from Omega (oblimin-rotation) - + Variable | g | F1* | F2* | F3* | h2 | u2 | p2 | Complexity ---------------------------------------------------------------------------------------- mpg- | 0.58 | -0.67 | 0.09 | 0.29 | 0.88 | 0.12 | 0.38 | 2.40 @@ -68,10 +68,10 @@ Code print_md(out) Output - - + + Table: Rotated loadings from Omega (oblimin-rotation) - + |Variable | g| F1* | F2*| F3*| h2 | u2 | p2| Complexity| |:--------|--------:|:-----|--------:|---------:|:----|:----|--------:|----------:| |mpg- | 0.58|-0.67 | 0.09| 0.29|0.88 |0.12 | 0.38| 2.40| @@ -92,26 +92,26 @@ print(summary(out)) Output # Omega Statistics - + Statistic | Coefficient ---------------------------------- Alpha | 0.88 G.6 | 0.97 - Omega (hierachical) | 0.57 + Omega (hierarchical) | 0.57 Omega (asymptotic H) | 0.58 Omega (total) | 0.97 - + # Omega Coefficients - + Composite | Omega (total) | Omega (hierarchical) | Omega (group) ---------------------------------------------------------------- g | 0.97 | 0.57 | 0.26 F1* | 0.90 | 0.31 | 0.59 F2* | 0.91 | 0.69 | 0.22 F3* | 0.87 | 0.60 | 0.28 - + # Variances - + Composite | Total (%) | General Factor (%) | Group Factor (%) ------------------------------------------------------------- g | 97.28 | 56.64 | 26.42 @@ -124,33 +124,32 @@ Code print_md(summary(out)) Output - - + + Table: Omega Statistics - + |Statistic | Coefficient| |:--------------------|-----------:| |Alpha | 0.88| |G.6 | 0.97| - |Omega (hierachical) | 0.57| + |Omega (hierarchical) | 0.57| |Omega (asymptotic H) | 0.58| |Omega (total) | 0.97| - + Table: Omega Coefficients - + |Composite | Omega (total)| Omega (hierarchical)| Omega (group)| |:---------|-------------:|--------------------:|-------------:| |g | 0.97| 0.57| 0.26| |F1* | 0.90| 0.31| 0.59| |F2* | 0.91| 0.69| 0.22| |F3* | 0.87| 0.60| 0.28| - + Table: Variances - + |Composite | Total (%)| General Factor (%)| Group Factor (%)| |:---------|---------:|------------------:|----------------:| |g | 97.28| 56.64| 26.42| |F1* | 90.12| 31.07| 59.05| |F2* | 91.37| 69.32| 22.04| |F3* | 87.36| 59.65| 27.71| - From 9d1a163024e666b551886c478d0b63b3ab9b08d1 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 19 Jun 2025 13:55:56 +0200 Subject: [PATCH 12/14] fix --- R/methods_psych.R | 1 + man/model_parameters.principal.Rd | 1 + 2 files changed, 2 insertions(+) diff --git a/R/methods_psych.R b/R/methods_psych.R index 8c51d9469..96abf7eba 100644 --- a/R/methods_psych.R +++ b/R/methods_psych.R @@ -81,6 +81,7 @@ #' # lavaan ------------------------------------- #' # Confirmatory Factor Analysis (CFA) --------- #' +#' data(HolzingerSwineford1939, package = "lavaan") #' structure <- " visual =~ x1 + x2 + x3 #' textual =~ x4 + x5 + x6 #' speed =~ x7 + x8 + x9 " diff --git a/man/model_parameters.principal.Rd b/man/model_parameters.principal.Rd index 73dff06e3..d3adc0d22 100644 --- a/man/model_parameters.principal.Rd +++ b/man/model_parameters.principal.Rd @@ -147,6 +147,7 @@ summary(params) # lavaan ------------------------------------- # Confirmatory Factor Analysis (CFA) --------- +data(HolzingerSwineford1939, package = "lavaan") structure <- " visual =~ x1 + x2 + x3 textual =~ x4 + x5 + x6 speed =~ x7 + x8 + x9 " From 7101f461ec008bb80d8240d6158f42b3f263ff31 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 19 Jun 2025 13:57:15 +0200 Subject: [PATCH 13/14] docs --- R/methods_psych.R | 3 +++ man/model_parameters.principal.Rd | 3 +++ 2 files changed, 6 insertions(+) diff --git a/R/methods_psych.R b/R/methods_psych.R index 96abf7eba..19e6759f4 100644 --- a/R/methods_psych.R +++ b/R/methods_psych.R @@ -52,6 +52,7 @@ #' library(parameters) #' \donttest{ #' # Principal Component Analysis (PCA) --------- +#' data(attitude) #' pca <- psych::principal(attitude) #' model_parameters(pca) #' summary(model_parameters(pca)) @@ -71,6 +72,7 @@ #' #' #' # Omega --------- +#' data(mtcars) #' omega <- psych::omega(mtcars, nfactors = 3, plot = FALSE) #' params <- model_parameters(omega) #' params @@ -100,6 +102,7 @@ #' #' # Structural Equation Model (SEM) ------------ #' +#' data(PoliticalDemocracy, package = "lavaan") #' structure <- " #' # latent variable definitions #' ind60 =~ x1 + x2 + x3 diff --git a/man/model_parameters.principal.Rd b/man/model_parameters.principal.Rd index d3adc0d22..e99be2b56 100644 --- a/man/model_parameters.principal.Rd +++ b/man/model_parameters.principal.Rd @@ -118,6 +118,7 @@ for \code{lavaan} models implemented in the library(parameters) \donttest{ # Principal Component Analysis (PCA) --------- +data(attitude) pca <- psych::principal(attitude) model_parameters(pca) summary(model_parameters(pca)) @@ -137,6 +138,7 @@ model_parameters(efa, # Omega --------- +data(mtcars) omega <- psych::omega(mtcars, nfactors = 3, plot = FALSE) params <- model_parameters(omega) params @@ -166,6 +168,7 @@ model_parameters( # Structural Equation Model (SEM) ------------ +data(PoliticalDemocracy, package = "lavaan") structure <- " # latent variable definitions ind60 =~ x1 + x2 + x3 From c28be40bcca7204ef8dc22404bcb862467306f77 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 19 Jun 2025 14:07:02 +0200 Subject: [PATCH 14/14] fix snap --- tests/testthat/_snaps/factor_analysis.md | 49 ++++++++++++------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/tests/testthat/_snaps/factor_analysis.md b/tests/testthat/_snaps/factor_analysis.md index 5043ddd47..9aba14588 100644 --- a/tests/testthat/_snaps/factor_analysis.md +++ b/tests/testthat/_snaps/factor_analysis.md @@ -4,16 +4,16 @@ print(summary(out)) Output # (Explained) Variance of Components - + Parameter | MR1 | MR2 ----------------------------------------------- Eigenvalues | 4.947 | 1.062 Variance Explained | 0.638 | 0.220 Variance Explained (Cumulative) | 0.638 | 0.858 Variance Explained (Proportion) | 0.744 | 0.256 - + # Factor Correlations - + Factor | MR1 | MR2 ------------------------ MR1 | 1.000 | -0.366 @@ -24,19 +24,19 @@ Code print_md(summary(out)) Output - - + + Table: (Explained) Variance of Components - + |Parameter | MR1 | MR2 | |:-------------------------------|:-----:|:-----:| |Eigenvalues | 4.947 | 1.062 | |Variance Explained | 0.638 | 0.220 | |Variance Explained (Cumulative) | 0.638 | 0.858 | |Variance Explained (Proportion) | 0.744 | 0.256 | - + Table: Factor Correlations - + |Factor | MR1 | MR2 | |:------|:------:|:------:| |MR1 | 1.000 | -0.366 | @@ -48,7 +48,7 @@ print(out) Output # Rotated loadings from Omega (oblimin-rotation) - + Variable | g | F1* | F2* | F3* | h2 | u2 | p2 | Complexity ---------------------------------------------------------------------------------------- mpg- | 0.58 | -0.67 | 0.09 | 0.29 | 0.88 | 0.12 | 0.38 | 2.40 @@ -68,10 +68,10 @@ Code print_md(out) Output - - + + Table: Rotated loadings from Omega (oblimin-rotation) - + |Variable | g| F1* | F2*| F3*| h2 | u2 | p2| Complexity| |:--------|--------:|:-----|--------:|---------:|:----|:----|--------:|----------:| |mpg- | 0.58|-0.67 | 0.09| 0.29|0.88 |0.12 | 0.38| 2.40| @@ -92,7 +92,7 @@ print(summary(out)) Output # Omega Statistics - + Statistic | Coefficient ---------------------------------- Alpha | 0.88 @@ -100,18 +100,18 @@ Omega (hierarchical) | 0.57 Omega (asymptotic H) | 0.58 Omega (total) | 0.97 - + # Omega Coefficients - + Composite | Omega (total) | Omega (hierarchical) | Omega (group) ---------------------------------------------------------------- g | 0.97 | 0.57 | 0.26 F1* | 0.90 | 0.31 | 0.59 F2* | 0.91 | 0.69 | 0.22 F3* | 0.87 | 0.60 | 0.28 - + # Variances - + Composite | Total (%) | General Factor (%) | Group Factor (%) ------------------------------------------------------------- g | 97.28 | 56.64 | 26.42 @@ -124,10 +124,10 @@ Code print_md(summary(out)) Output - - + + Table: Omega Statistics - + |Statistic | Coefficient| |:--------------------|-----------:| |Alpha | 0.88| @@ -135,21 +135,22 @@ |Omega (hierarchical) | 0.57| |Omega (asymptotic H) | 0.58| |Omega (total) | 0.97| - + Table: Omega Coefficients - + |Composite | Omega (total)| Omega (hierarchical)| Omega (group)| |:---------|-------------:|--------------------:|-------------:| |g | 0.97| 0.57| 0.26| |F1* | 0.90| 0.31| 0.59| |F2* | 0.91| 0.69| 0.22| |F3* | 0.87| 0.60| 0.28| - + Table: Variances - + |Composite | Total (%)| General Factor (%)| Group Factor (%)| |:---------|---------:|------------------:|----------------:| |g | 97.28| 56.64| 26.42| |F1* | 90.12| 31.07| 59.05| |F2* | 91.37| 69.32| 22.04| |F3* | 87.36| 59.65| 27.71| +