Skip to content

Commit

Permalink
PCA with n = 1 (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke authored Sep 17, 2023
1 parent 700067b commit 294bba8
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 40 deletions.
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 @@ -418,15 +419,9 @@ principal_components.data.frame <- function(x,
} 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 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

0 comments on commit 294bba8

Please sign in to comment.