From 87a86803a5bed331ee7f77866cbfab69acfd3850 Mon Sep 17 00:00:00 2001 From: Dominique Makowski Date: Thu, 30 Nov 2023 15:48:32 +0000 Subject: [PATCH] Add Variance_Cumulative (explained variance) to summary of n_factors() --- R/n_factors.R | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/R/n_factors.R b/R/n_factors.R index ca70ee042..5d14b3855 100644 --- a/R/n_factors.R +++ b/R/n_factors.R @@ -340,6 +340,14 @@ n_factors <- function(x, n_Methods = as.numeric(by(out, as.factor(out$n_Factors), function(out) n <- nrow(out))) ) + # Add cumulative percentage of variance explained + fa <- factor_analysis(x, cor=cor, n = max(by_factors$n_Factors)) # Get it from our fa:: wrapper (TODO: that's probably not the most efficient) + varex <- attributes(fa)$summary + # Extract number of factors from EFA output (usually MR1, ML1, etc.) + varex$n_Factors <- as.numeric(gsub("[^\\d]+", "", varex$Component, perl=TRUE)) + by_factors <- merge(by_factors, varex[, c("n_Factors", "Variance_Cumulative")], by = "n_Factors") + + attr(out, "summary") <- by_factors attr(out, "n") <- min(as.numeric(as.character( by_factors[by_factors$n_Methods == max(by_factors$n_Methods), "n_Factors"]