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 21 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
10 changes: 2 additions & 8 deletions R/principal_components.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,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 132 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=132,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 +263,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 266 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=266,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 @@ -419,14 +419,8 @@
n <- ncol(x) - 1
} else if (n >= ncol(x)) {
n <- ncol(x) - 1
strengejacke marked this conversation as resolved.
Show resolved Hide resolved
}

## 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
} else if (n < 1) {
n <- 1
}
n
}
Expand All @@ -442,7 +436,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 439 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=439,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
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