Skip to content

Commit af28e13

Browse files
authored
Merge pull request #350 from easystats/strengejacke/issue347
polychoric correlation method yields error
2 parents 1948386 + a3cd313 commit af28e13

File tree

5 files changed

+36
-2
lines changed

5 files changed

+36
-2
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: correlation
33
Title: Methods for Correlation Analysis
4-
Version: 0.8.7.1
4+
Version: 0.8.7.2
55
Authors@R:
66
c(person(given = "Dominique",
77
family = "Makowski",

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- `correlation()` gains a `missing=` argument, similar to `stats::cor(use=)`, for controlling how missing data is handled.
44

5+
- `correlation()` converts numeric input variables automatically into factors when `method = "polychoric"`.
6+
57
# correlation 0.8.7
68

79
- The `format()` method for objects of class `easycormatrix` gets a `zap_small`

R/cor_test.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ cor_test <- function(data,
148148
data[c(x, y)] <- datawizard::to_numeric(data[c(x, y)], dummy_factors = FALSE)
149149
}
150150

151+
# However, for poly, we need factors!
152+
if (method %in% c("poly", "polychoric") && all(vapply(data[c(x, y)], is.numeric, FUN.VALUE = TRUE))) {
153+
# convert all input to factors, but only if all input currently is numeric
154+
# we allow mix of numeric and factors
155+
data[c(x, y)] <- datawizard::to_factor(data[c(x, y)])
156+
}
157+
151158
# Partial
152159
if (!isFALSE(partial)) {
153160
# partial

R/correlation.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,15 @@ correlation <- function(data,
551551
include_factors <- TRUE
552552
}
553553

554-
if (method == "polychoric") multilevel <- TRUE
554+
# definitely need factors for polychoric
555+
if (method == "polychoric") {
556+
multilevel <- TRUE
557+
# convert all input to factors, but only if all input currently is numeric
558+
# we allow mix of numeric and factors
559+
if (all(vapply(data, is.numeric, FUN.VALUE = TRUE))) {
560+
data <- datawizard::to_factor(data)
561+
}
562+
}
555563

556564
# Clean data and get combinations -------------
557565

tests/testthat/test-cor_test.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ test_that("cor_test tetrachoric", {
8888
out <- cor_test(data, "Sepal.Width", "Petal.Width_binary", method = "biserial")
8989
expect_equal(out$rho, -0.403, tolerance = 0.01)
9090
out_psych <- psych::biserial(data[["Sepal.Width"]], data[["Petal.Width_binary"]])[1]
91+
92+
set.seed(123)
93+
n <- 100
94+
k <- 5
95+
d <- data.frame(
96+
x1 = sample.int(k, n, TRUE),
97+
x2 = sample.int(k, n, TRUE),
98+
x3 = sample.int(k, n, TRUE),
99+
x4 = sample.int(k, n, TRUE)
100+
)
101+
expect_true(all(vapply(d, is.numeric, logical(1))))
102+
out <- correlation(d, method = "polychoric")
103+
expect_equal(
104+
out$rho,
105+
c(0.07729, -0.02453, -0.13999, 0.06508, -0.17158, 0.17863),
106+
tolerance = 1e-3
107+
)
91108
})
92109

93110

0 commit comments

Comments
 (0)