Skip to content

Commit f2f51de

Browse files
committed
fix(factanal): restore na.rm/quantile_prob/retained from spec's final compute_parallel_analysis() (#37332)
Independent spec-compliance verification pass found 3 elements present in the issue's own "完成版" (finished) code for compute_parallel_analysis() that were dropped during implementation: - na.rm = TRUE on the random_threshold stats::quantile() call - quantile_prob echoed back as a field on the returned list - a retained boolean column on the returned table (seq_along(actual_eigen) <= recommended_n) None change existing consumers (parallel_screeplot/factor_count only read table$actual_eigenvalue/random_eigenvalue_threshold by name), so this is a pure additive fix restoring exact spec fidelity. Updated the one pinned colnames()/field assertion in test_factanal.R accordingly.
1 parent 3ff1f59 commit f2f51de

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

R/factanal.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ compute_parallel_analysis <- function(x, n_iter = 100, quantile_prob = 0.95,
299299
return(NULL) # no usable null distribution; the caller reports the parallel analysis as unavailable
300300
}
301301
random_eigen_mat <- do.call(cbind, random_eigen_list)
302-
random_threshold <- apply(random_eigen_mat, 1, stats::quantile, probs = quantile_prob)
302+
random_threshold <- apply(random_eigen_mat, 1, stats::quantile, probs = quantile_prob, na.rm = TRUE)
303303
recommended_n <- compute_parallel_recommended_n(actual_eigen, random_threshold)
304304

305305
list(
@@ -308,11 +308,13 @@ compute_parallel_analysis <- function(x, n_iter = 100, quantile_prob = 0.95,
308308
requested_n_iter = n_iter,
309309
successful_n_iter = length(random_eigen_list),
310310
failed_n_iter = n_iter - length(random_eigen_list),
311+
quantile_prob = quantile_prob,
311312
recommended_n = recommended_n,
312313
table = tibble::tibble(
313314
factor_number = seq_along(actual_eigen),
314315
actual_eigenvalue = actual_eigen,
315-
random_eigenvalue_threshold = random_threshold
316+
random_eigenvalue_threshold = random_threshold,
317+
retained = seq_along(actual_eigen) <= recommended_n
316318
)
317319
)
318320
}

tests/testthat/test_factanal.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,12 @@ test_that("factor analysis report judgment helpers (issue #37018)", {
338338
set.seed(1)
339339
pa <- compute_parallel_analysis(mtcars[, c("mpg","cyl","disp","hp","drat","wt","qsec")], n_iter = 20)
340340
expect_true(is.numeric(pa$recommended_n))
341-
expect_equal(colnames(pa$table), c("factor_number", "actual_eigenvalue", "random_eigenvalue_threshold"))
341+
expect_equal(colnames(pa$table), c("factor_number", "actual_eigenvalue", "random_eigenvalue_threshold", "retained"))
342342
# method defaults to "factor_model" (issue tam#37332).
343343
expect_equal(pa$method, "factor_model")
344344
expect_equal(pa$factor_extraction_method, "minres")
345+
expect_equal(pa$quantile_prob, 0.95)
346+
expect_equal(pa$table$retained, seq_len(nrow(pa$table)) <= pa$recommended_n)
345347
set.seed(99)
346348
before <- .Random.seed
347349
compute_parallel_analysis(mtcars[, 1:3], n_iter = 2)

0 commit comments

Comments
 (0)