Skip to content

Commit 32bcede

Browse files
authored
Add test for reverse_items (#1122)
* Add test for `reverse_items` * add tests * fix * reverse_items for PCA and FA * save attr * tests * remove `reverse_item` * rd * rd
1 parent 256e474 commit 32bcede

File tree

6 files changed

+6
-29
lines changed

6 files changed

+6
-29
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: parameters
33
Title: Processing of Model Parameters
4-
Version: 0.26.0.10
4+
Version: 0.26.0.11
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

NEWS.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
* New function `factor_scores()` to extract factor scores from EFA (`psych::fa()`
2424
or `factor_analysis()`).
2525

26-
* `get_scores()` gets a `reverse_items` argument, to reverse the scale of
27-
items before calculating the sum scores of subscales. This is useful
28-
when the items are not coded in the same direction.
29-
3026
* Added and/or improved print-methods for all functions around PCA, FA and Omega.
3127

3228
* Improved efficiency in `model_parameters()` for models from packages *brms*

R/principal_components.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ principal_components.data.frame <- function(x,
322322
)
323323

324324
attr(pca_loadings, "data") <- data_name
325+
attr(pca_loadings, "dataset") <- original_data
326+
325327
return(pca_loadings)
326328
}
327329

R/utils_pca_efa.R

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
#' observation, else, the sum score of all (sub) items is calculated. If `NULL`,
1414
#' the value is chosen to match half of the number of columns in a data frame,
1515
#' i.e. no more than 50% missing values are allowed.
16-
#' @param reverse_items Character vector of variable names or numeric indices
17-
#' indicating their column positions in `x` that should be reversed before
18-
#' computing sum scores. This is useful when the items are not coded in the same
19-
#' direction. If `NULL` (default), no items are reversed.
2016
#'
2117
#' @details
2218
#' `get_scores()` takes the results from [`principal_components()`] or
@@ -51,23 +47,10 @@
5147
#' (mtcars$hp + mtcars$qsec) / 2
5248
#'
5349
#' @export
54-
get_scores <- function(x, n_items = NULL, reverse_items = NULL) {
50+
get_scores <- function(x, n_items = NULL) {
5551
subscales <- closest_component(x)
5652
dataset <- attributes(x)$dataset
5753

58-
# should some items be reversed?
59-
if (!is.null(reverse_items)) {
60-
# numeric indices should be replaced by their column names
61-
if (is.numeric(reverse_items)) {
62-
reverse_items <- colnames(dataset)[reverse_items]
63-
}
64-
if (verbose) {
65-
insight::format_message("Reversing items: ", toString(reverse_items))
66-
}
67-
# reverse the items
68-
dataset <- datawizard::reverse_scale(dataset, reverse_items, verbose = verbose)
69-
}
70-
7154
out <- lapply(sort(unique(subscales)), function(.subscale) {
7255
columns <- names(subscales)[subscales == .subscale]
7356
items <- dataset[columns]

man/get_scores.Rd

Lines changed: 1 addition & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-get_scores.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ skip_on_cran()
22

33
test_that("get_scores", {
44
skip_if_not_installed("psych")
5+
data(mtcars)
56
pca <- principal_components(mtcars[, 1:7], n = 2, rotation = "varimax")
67
scores <- get_scores(pca)
78
expect_equal(head(scores$Component_1), c(38.704, 38.755, 28.194, 58.339, 78.658, 51.064), tolerance = 1e-2)

0 commit comments

Comments
 (0)