Skip to content

Commit f106402

Browse files
committed
remove reverse_item
1 parent 59f46e3 commit f106402

File tree

7 files changed

+2
-118
lines changed

7 files changed

+2
-118
lines changed

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/factor_analysis.R

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,10 @@ factor_analysis.data.frame <- function(x,
1414
sort = FALSE,
1515
threshold = NULL,
1616
standardize = FALSE,
17-
reverse_items = NULL,
1817
verbose = TRUE,
1918
...) {
2019
insight::check_if_installed("psych")
2120

22-
# should some items be reversed?
23-
if (!is.null(reverse_items)) {
24-
# only works for data frames, not matrices
25-
if (!is.data.frame(x)) {
26-
insight::format_error(
27-
"The `reverse_items` argument only works with data frames, not matrices."
28-
)
29-
}
30-
# numeric indices should be replaced by their column names
31-
if (is.numeric(reverse_items)) {
32-
reverse_items <- colnames(x)[reverse_items]
33-
}
34-
if (verbose) {
35-
insight::format_alert(paste("Reversing items:", toString(reverse_items)))
36-
}
37-
# reverse the items
38-
x <- datawizard::reverse_scale(x, reverse_items, verbose = verbose)
39-
}
40-
4121
# Standardize
4222
if (standardize) {
4323
x <- datawizard::standardize(x, ...)
@@ -60,7 +40,6 @@ factor_analysis.data.frame <- function(x,
6040
)
6141

6242
attr(out, "dataset") <- x
63-
attr(out, "reverse_items") <- reverse_items
6443
out
6544
}
6645

R/principal_components.R

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@
5353
#' computes a correlation matrix and uses that r-matrix for the factor analysis
5454
#' by default - therefore, standardization of the raw variables is unnecessary,
5555
#' and even undesirable when using `cor = "poly"`).
56-
#' @param reverse_items Character vector of variable names or numeric indices
57-
#' indicating their column positions in `x` that should be reversed before
58-
#' computing the PCA or FA. This is useful when the items are not coded in the
59-
#' same direction. If `NULL` (default), no items are reversed. **Note:** The
60-
#' data frame that is stored as attribute in the returned object is the modified
61-
#' version of `x`, with the reversed items.
6256
#' @param object An object of class `parameters_pca`, `parameters_efa` or
6357
#' `psych_efa`.
6458
#' @param newdata An optional data frame in which to look for variables with
@@ -295,7 +289,6 @@ principal_components.data.frame <- function(x,
295289
sort = FALSE,
296290
threshold = NULL,
297291
standardize = TRUE,
298-
reverse_items = NULL,
299292
verbose = TRUE,
300293
...) {
301294
# save name of data set
@@ -307,20 +300,6 @@ principal_components.data.frame <- function(x,
307300
# remove missing
308301
x <- stats::na.omit(x)
309302

310-
# should some items be reversed?
311-
if (!is.null(reverse_items)) {
312-
# numeric indices should be replaced by their column names
313-
if (is.numeric(reverse_items)) {
314-
reverse_items <- colnames(x)[reverse_items]
315-
}
316-
if (verbose) {
317-
insight::format_alert(paste("Reversing items:", toString(reverse_items)))
318-
}
319-
# reverse the items
320-
x <- datawizard::reverse_scale(x, reverse_items, verbose = verbose)
321-
original_data <- datawizard::reverse_scale(original_data, reverse_items, verbose = FALSE)
322-
}
323-
324303
# Select numeric only
325304
x <- x[vapply(x, is.numeric, TRUE)]
326305

@@ -345,7 +324,6 @@ principal_components.data.frame <- function(x,
345324

346325
attr(pca_loadings, "data") <- data_name
347326
attr(pca_loadings, "dataset") <- original_data
348-
attr(pca_loadings, "reverse_items") <- reverse_items
349327

350328
return(pca_loadings)
351329
}
@@ -469,7 +447,6 @@ principal_components.data.frame <- function(x,
469447
)
470448
attr(pca_loadings, "data") <- data_name
471449
attr(pca_loadings, "dataset") <- original_data
472-
attr(pca_loadings, "reverse_items") <- reverse_items
473450

474451
# add class-attribute for printing
475452
class(pca_loadings) <- unique(c("parameters_pca", "see_parameters_pca", class(pca_loadings)))

R/utils_pca_efa.R

Lines changed: 1 addition & 25 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
#' @param verbose Logical, whether to print messages about reversing items.
2117
#'
2218
#' @details
@@ -52,30 +48,10 @@
5248
#' (mtcars$hp + mtcars$qsec) / 2
5349
#'
5450
#' @export
55-
get_scores <- function(x, n_items = NULL, reverse_items = NULL, verbose = TRUE) {
51+
get_scores <- function(x, n_items = NULL) {
5652
subscales <- closest_component(x)
5753
dataset <- attributes(x)$dataset
5854

59-
# should some items be reversed?
60-
if (!is.null(reverse_items)) {
61-
# check if the object has an attribute "reverse_items" - if so, warn that
62-
# we don't want to "double reverse" items
63-
if ("reverse_items" %in% names(attributes(x))) {
64-
insight::format_warning(
65-
"It seem that items have already been reversed in this data set. Make sure that you do not reverse them again."
66-
)
67-
}
68-
# numeric indices should be replaced by their column names
69-
if (is.numeric(reverse_items)) {
70-
reverse_items <- colnames(dataset)[reverse_items]
71-
}
72-
if (verbose) {
73-
insight::format_alert(paste("Reversing items:", toString(reverse_items)))
74-
}
75-
# reverse the items
76-
dataset <- datawizard::reverse_scale(dataset, reverse_items, verbose = verbose)
77-
}
78-
7955
out <- lapply(sort(unique(subscales)), function(.subscale) {
8056
columns <- names(subscales)[subscales == .subscale]
8157
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.

man/principal_components.Rd

Lines changed: 0 additions & 9 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: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,4 @@ test_that("get_scores", {
77
scores <- get_scores(pca)
88
expect_equal(head(scores$Component_1), c(38.704, 38.755, 28.194, 58.339, 78.658, 51.064), tolerance = 1e-2)
99
expect_equal(head(scores$Component_2), c(63.23, 63.51, 55.805, 64.72, 96.01, 62.61), tolerance = 1e-2)
10-
11-
scores2 <- get_scores(pca, reverse_items = c("cyl", "drat"))
12-
expect_equal(head(scores2$Component_1), c(38.682, 38.733, 28.992, 58.645, 78.136, 51.498), tolerance = 1e-2)
13-
14-
expect_warning(
15-
get_scores(pca, reverse_items = c("cyl", "abc")),
16-
regex = "Following variable(s)",
17-
fixed = TRUE
18-
)
19-
20-
expect_message(
21-
get_scores(pca, reverse_items = c("cyl", "drat")),
22-
regex = "Reversing items: cyl, drat",
23-
fixed = TRUE
24-
)
25-
26-
expect_silent(get_scores(pca, reverse_items = c("cyl", "abc"), verbose = FALSE))
27-
expect_silent(get_scores(pca, reverse_items = c("cyl", "drat"), verbose = FALSE))
28-
29-
pca <- principal_components(
30-
mtcars[, 1:7],
31-
n = 2,
32-
rotation = "varimax",
33-
reverse_items = c("cyl", "drat")
34-
)
35-
expect_warning(
36-
get_scores(pca, reverse_items = c("cyl", "drat")),
37-
regex = "It seems that",
38-
fixed = TRUE
39-
)
4010
})

0 commit comments

Comments
 (0)