Skip to content

Commit

Permalink
fix?
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Dec 2, 2023
1 parent de8f464 commit c0837c3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 26 deletions.
4 changes: 2 additions & 2 deletions R/utils_pca_efa.R
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ predict.parameters_efa <- function(object,
}
} else {
# psych:::predict.fa(object, data)
out <- as.data.frame(stats::predict(attri$model, data = newdata))
out <- as.data.frame(stats::predict(attri$model, newdata = newdata))
}
} else if (inherits(attri$model, "spca")) {
# https://github.com/erichson/spca/issues/7
Expand All @@ -194,7 +194,7 @@ predict.parameters_efa <- function(object,
out <- as.matrix(newdata) %*% as.matrix(attri$model$loadings)
out <- stats::setNames(as.data.frame(out), paste0("Component", seq_len(ncol(out))))
} else {
out <- as.data.frame(stats::predict(attri$model, newdata = newdata, ...))
out <- as.data.frame(stats::predict(attri$model, newdata = attri$dataset, ...))
}

if (!is.null(names)) {
Expand Down
28 changes: 4 additions & 24 deletions tests/testthat/test-pca.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ test_that("principal_components", {

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

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


test_that("principal_components", {
x <- parameters::principal_components(mtcars[, 1:7])
x <- principal_components(mtcars[, 1:7])

expect_equal(
x$PC1,
Expand All @@ -55,27 +55,7 @@ test_that("principal_components", {
)

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

test_that("principal_components", {
x <- model_parameters(principal_components(mtcars[, 1:7], nfactors = 2))
expect_equal(
x$RC1,
c(
-0.836114674884308,
0.766808147590597,
0.85441780762136,
0.548502661888057,
-0.889046093964722,
0.931879020871552,
-0.030485507571411
),
tolerance = 0.01
)

expect_named(x, c("Variable", "RC1", "RC2", "Complexity", "Uniqueness"))
expect_identical(dim(suppressWarnings(predict(x))), c(32L, 2L))
expect_identical(dim(suppressWarnings(predict(x, newdata = mtcars[1:3, 1:7]))), c(3L, 2L))
expect_identical(dim(predict(x)), c(32L, 2L))
})


Expand Down

0 comments on commit c0837c3

Please sign in to comment.