Skip to content

Commit 6d9da1b

Browse files
authored
get_scores() support for factor scores from Factor_analysis() (#1121)
* `get_scores()` support for factor scores from `Factor_analysis()` Fixes #1120 * docs
1 parent 0b785e2 commit 6d9da1b

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

R/utils_pca_efa.R

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
#' Get Scores from Principal Component Analysis (PCA)
1+
#' Get Scores from Principal Component or Factor Analysis (PCA/FA)
22
#'
33
#' `get_scores()` takes `n_items` amount of items that load the most
44
#' (either by loading cutoff or number) on a component, and then computes their
5-
#' average.
5+
#' average. This results in a sum score for each component from the PCA/FA,
6+
#' which is on the same scale as the original, single items that were used to
7+
#' compute the PCA/FA.
68
#'
7-
#' @param x An object returned by [principal_components()].
9+
#' @param x An object returned by [principal_components()] or [factor_analysis()].
810
#' @param n_items Number of required (i.e. non-missing) items to build the sum
911
#' score for an observation. If an observation has more missing values than
1012
#' `n_items` in all items of a (sub) scale, `NA` is returned for that
@@ -15,15 +17,18 @@
1517
#' @details
1618
#' `get_scores()` takes the results from [`principal_components()`] or
1719
#' [`factor_analysis()`] and extracts the variables for each component found by
18-
#' the PCA. Then, for each of these "subscales", row means are calculated (which
19-
#' equals adding up the single items and dividing by the number of items). This
20-
#' results in a sum score for each component from the PCA, which is on the same
21-
#' scale as the original, single items that were used to compute the PCA.
20+
#' the PCA/FA. Then, for each of these "subscales", row means are calculated
21+
#' (which equals adding up the single items and dividing by the number of
22+
#' items). This results in a sum score for each component from the PCA/FA, which
23+
#' is on the same scale as the original, single items that were used to compute
24+
#' the PCA/FA.
2225
#'
2326
#' @return A data frame with subscales, which are average sum scores for all
24-
#' items from each component.
27+
#' items from each component or factor.
2528
#'
26-
#' @seealso [`principal_components()`]
29+
#' @seealso Functions to carry out a PCA ([`principal_components()`]) or
30+
#' a FA ([`factor_analysis()`]). [`factor_scores()`] extracts factor scores
31+
#' from an FA object.
2732
#'
2833
#' @examplesIf insight::check_if_installed("psych", quietly = TRUE)
2934
#' pca <- principal_components(mtcars[, 1:7], n = 2, rotation = "varimax")
@@ -579,11 +584,11 @@ sort.parameters_pca <- sort.parameters_efa
579584

580585
#' @rdname principal_components
581586
#' @export
582-
closest_component <- function(pca_results) {
583-
if ("closest_component" %in% names(attributes(pca_results))) {
584-
attributes(pca_results)$closest_component
587+
closest_component <- function(x) {
588+
if ("closest_component" %in% names(attributes(x))) {
589+
attributes(x)$closest_component
585590
} else {
586-
.closest_component(pca_results)
591+
.closest_component(x)
587592
}
588593
}
589594

man/get_scores.Rd

Lines changed: 14 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/principal_components.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)