Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCA with n = 1 #904

Merged
merged 22 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: parameters
Title: Processing of Model Parameters
Version: 0.21.2
Version: 0.21.2.1
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# parameters 0.21.3

## Changes

* `principal_components()` and `factor_analysis()` now also work when argument
`n = 1`.

# parameters 0.21.2

## Changes
Expand Down
27 changes: 11 additions & 16 deletions R/principal_components.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
#' Details section).
#'
#' @param x A data frame or a statistical model.
#' @param n Number of components to extract. If `n="all"`, then `n` is
#' set as the number of variables minus 1 (`ncol(x)-1`). If
#' `n="auto"` (default) or `n=NULL`, the number of components is
#' selected through [`n_factors()`] resp. [`n_components()`].
#' In [`reduce_parameters()`], can also be `"max"`, in which case
#' it will select all the components that are maximally pseudo-loaded (i.e.,
#' correlated) by at least one variable.
#' @param n Number of components to extract. If `n="all"`, then `n` is set as
#' the number of variables minus 1 (`ncol(x)-1`). If `n="auto"` (default) or
#' `n=NULL`, the number of components is selected through [`n_factors()`] resp.
#' [`n_components()`]. Else, if `n` is a number, `n` components are extracted.
#' If `n` exceeds number of variables in the data, it is automatically set to
#' the maximum number (i.e. `ncol(x)`). In [`reduce_parameters()`], can also
#' be `"max"`, in which case it will select all the components that are
#' maximally pseudo-loaded (i.e., correlated) by at least one variable.
#' @param rotation If not `"none"`, the PCA / FA will be computed using the
#' **psych** package. Possible options include `"varimax"`,
#' `"quartimax"`, `"promax"`, `"oblimin"`, `"simplimax"`,
Expand Down Expand Up @@ -129,7 +130,7 @@
#' magnitude. In other words, the eigenvalues explain the variance of the
#' data along the new feature axes.
#'
#' @examplesIf require("nFactors", quietly = TRUE) && require("sparsepca", quietly = TRUE) && require("psych", quietly = TRUE)

Check warning on line 133 in R/principal_components.R

View workflow job for this annotation

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

file=R/principal_components.R,line=133,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 126 characters.
#' library(parameters)
#'
#' \donttest{
Expand Down Expand Up @@ -263,7 +264,7 @@
# Catch and compute Rotated PCA
if (rotation != "none") {
if (sparse) {
insight::format_error("Sparse PCA is currently incompatible with rotation. Use either `sparse=TRUE` or `rotation`.")

Check warning on line 267 in R/principal_components.R

View workflow job for this annotation

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

file=R/principal_components.R,line=267,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.
}

loadings <- .pca_rotate(
Expand Down Expand Up @@ -418,15 +419,9 @@
} else if (n == "all") {
n <- ncol(x) - 1
} else if (n >= ncol(x)) {
n <- ncol(x) - 1
}

## TODO: the next if-statement was removed by Dom, but this breaks
## performance code. Need to check, so we for now add this back

# sanity check - we need at least two factors
if (n < 2 && ncol(x) >= 2) {
n <- 2
n <- ncol(x)
} else if (n < 1) {
n <- 1
}
n
}
Expand All @@ -442,7 +437,7 @@
original_data = NULL,
...) {
if (!(rotation %in% c("varimax", "quartimax", "promax", "oblimin", "simplimax", "cluster", "none"))) {
insight::format_error("`rotation` must be one of \"varimax\", \"quartimax\", \"promax\", \"oblimin\", \"simplimax\", \"cluster\" or \"none\".")

Check warning on line 440 in R/principal_components.R

View workflow job for this annotation

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

file=R/principal_components.R,line=440,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 147 characters.
}

if (!inherits(x, c("prcomp", "data.frame"))) {
Expand Down
15 changes: 8 additions & 7 deletions man/principal_components.Rd

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

15 changes: 8 additions & 7 deletions man/reduce_parameters.Rd

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

21 changes: 12 additions & 9 deletions tests/testthat/test-pca.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ test_that("principal_components", {
tolerance = 0.01
)

expect_equal(
colnames(x),
c("Variable", "RC1", "RC2", "Complexity", "Uniqueness", "MSA")
)
expect_named(x, c("Variable", "RC1", "RC2", "Complexity", "Uniqueness", "MSA"))
})


test_that("principal_components, n", {
data(iris)
x <- parameters::principal_components(iris[1:4], n = 2)
expect_named(x, c("Variable", "PC1", "PC2", "Complexity"))

x <- parameters::principal_components(iris[1:4], n = 1)
expect_named(x, c("Variable", "PC1", "Complexity"))
})


Expand All @@ -43,17 +50,13 @@ test_that("principal_components", {
tolerance = 0.01
)

expect_equal(
colnames(x),
c("Variable", "PC1", "PC2", "Complexity")
)
expect_named(x, c("Variable", "PC1", "PC2", "Complexity"))
})


# predict ----------------------
# N.B tests will fail if `GPArotation` package is not installed

require("GPArotation", quietly = TRUE)
d <- na.omit(psych::bfi[, 1:25])
model <- psych::fa(d, nfactors = 5)
mp <- model_parameters(model, sort = TRUE, threshold = "max")
Expand Down
Loading