Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions R/utils_pca_efa.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#' Get Scores from Principal Component Analysis (PCA)
#' Get Scores from Principal Component or Factor Analysis (PCA/FA)
#'
#' `get_scores()` takes `n_items` amount of items that load the most
#' (either by loading cutoff or number) on a component, and then computes their
#' average.
#' average. This results in a sum score for each component from the PCA/FA,
#' which is on the same scale as the original, single items that were used to
#' compute the PCA/FA.
#'
#' @param x An object returned by [principal_components()].
#' @param x An object returned by [principal_components()] or [factor_analysis()].
#' @param n_items Number of required (i.e. non-missing) items to build the sum
#' score for an observation. If an observation has more missing values than
#' `n_items` in all items of a (sub) scale, `NA` is returned for that
Expand All @@ -15,15 +17,16 @@
#' @details
#' `get_scores()` takes the results from [`principal_components()`] or
#' [`factor_analysis()`] and extracts the variables for each component found by
#' the PCA. Then, for each of these "subscales", row means are calculated (which
#' equals adding up the single items and dividing by the number of items). This
#' results in a sum score for each component from the PCA, which is on the same
#' scale as the original, single items that were used to compute the PCA.
#' the PCA/FA. Then, for each of these "subscales", row means are calculated
#' (which equals adding up the single items and dividing by the number of
#' items). This results in a sum score for each component from the PCA/FA, which
#' is on the same scale as the original, single items that were used to compute
#' the PCA/FA.
#'
#' @return A data frame with subscales, which are average sum scores for all
#' items from each component.
#' items from each component or factor.
#'
#' @seealso [`principal_components()`]
#' @seealso [`principal_components()`], [`factor_analysis()`]
#'
#' @examplesIf insight::check_if_installed("psych", quietly = TRUE)
#' pca <- principal_components(mtcars[, 1:7], n = 2, rotation = "varimax")
Expand Down Expand Up @@ -312,7 +315,7 @@
# extract model
model <- attributes(x)$model
if (!is.null(model)) {
stats <- data.frame(

Check warning on line 318 in R/utils_pca_efa.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/utils_pca_efa.R,line=318,col=14,[strings_as_factors_linter] Supply an explicit value for stringsAsFactors for this code to work before and after R version 4.0.
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)
)
Expand Down Expand Up @@ -579,11 +582,11 @@

#' @rdname principal_components
#' @export
closest_component <- function(pca_results) {
if ("closest_component" %in% names(attributes(pca_results))) {
attributes(pca_results)$closest_component
closest_component <- function(x) {
if ("closest_component" %in% names(attributes(x))) {
Comment on lines 584 to +588
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The argument name pca_results is not consistent with the function's purpose after this change. It now accepts more general input than just PCA results. Consider renaming it to x to match the argument name used in the function definition and to reflect the broader applicability to both PCA and FA results.

closest_component <- function(x) {
  if ("closest_component" %in% names(attributes(x))) {

attributes(x)$closest_component
} else {
.closest_component(pca_results)
.closest_component(x)
}
}

Expand Down
21 changes: 12 additions & 9 deletions man/get_scores.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/principal_components.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading