Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: correlation
Title: Methods for Correlation Analysis
Version: 0.8.7.1
Version: 0.8.7.2
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

- `correlation()` converts numeric input variables automatically into factors when `method = "polychoric"`.

# correlation 0.8.7

- The `format()` method for objects of class `easycormatrix` gets a `zap_small`
Expand Down
7 changes: 7 additions & 0 deletions R/cor_test.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Correlation test
#'
#' This function performs a correlation test between two variables.
#' You can easily visualize the result using [`plot()`][visualisation_recipe.easycormatrix()] (see examples [**here**](https://easystats.github.io/correlation/reference/visualisation_recipe.easycormatrix.html#ref-examples)).

Check warning on line 4 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/cor_test.R,line=4,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 224 characters.

Check warning on line 4 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_test.R,line=4,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 224 characters.
#'
#' @param data A data frame.
#' @param x,y Names of two variables present in the data.
Expand Down Expand Up @@ -118,7 +118,7 @@
#' }
#' }
#' @export
cor_test <- function(data,

Check warning on line 121 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/cor_test.R,line=121,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this expression from 55 to at most 40.

Check warning on line 121 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_test.R,line=121,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this expression from 55 to at most 40.
x,
y,
method = "pearson",
Expand All @@ -137,7 +137,7 @@
...) {
# valid matrix checks
if (!all(x %in% names(data)) || !all(y %in% names(data))) {
insight::format_error("The names you entered for x and y are not available in the dataset. Make sure there are no typos!")

Check warning on line 140 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/cor_test.R,line=140,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 126 characters.

Check warning on line 140 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_test.R,line=140,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 126 characters.
}

if (ci == "default") ci <- 0.95
Expand All @@ -148,6 +148,13 @@
data[c(x, y)] <- datawizard::to_numeric(data[c(x, y)], dummy_factors = FALSE)
}

# However, for poly, we need factors!
if (method %in% c("poly", "polychoric") && all(vapply(data[c(x, y)], is.numeric, FUN.VALUE = TRUE))) {
# convert all input to factors, but only if all input currently is numeric
# we allow mix of numeric and factors
data[c(x, y)] <- datawizard::to_factor(data[c(x, y)])
}

# Partial
if (!isFALSE(partial)) {
# partial
Expand Down Expand Up @@ -237,19 +244,19 @@

# Bayesian
} else if (method %in% c("tetra", "tetrachoric")) {
insight::format_error("Tetrachoric Bayesian correlations are not supported yet. Get in touch if you want to contribute.")

Check warning on line 247 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/cor_test.R,line=247,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.

Check warning on line 247 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_test.R,line=247,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.
} else if (method %in% c("poly", "polychoric")) {
insight::format_error("Polychoric Bayesian correlations are not supported yet. Get in touch if you want to contribute.")

Check warning on line 249 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/cor_test.R,line=249,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.

Check warning on line 249 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_test.R,line=249,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.
} else if (method %in% c("biserial", "pointbiserial", "point-biserial")) {
insight::format_error("Biserial Bayesian correlations are not supported yet. Get in touch if you want to contribute.")

Check warning on line 251 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/cor_test.R,line=251,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.

Check warning on line 251 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_test.R,line=251,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.
} else if (method == "biweight") {
insight::format_error("Biweight Bayesian correlations are not supported yet. Get in touch if you want to contribute.")

Check warning on line 253 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/cor_test.R,line=253,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.

Check warning on line 253 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_test.R,line=253,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.
} else if (method == "distance") {
insight::format_error("Bayesian distance correlations are not supported yet. Get in touch if you want to contribute.")

Check warning on line 255 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/cor_test.R,line=255,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.

Check warning on line 255 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_test.R,line=255,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.
} else if (method %in% c("percentage", "percentage_bend", "percentagebend", "pb")) {
insight::format_error("Bayesian Percentage Bend correlations are not supported yet. Get in touch if you want to contribute.")

Check warning on line 257 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/cor_test.R,line=257,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 129 characters.

Check warning on line 257 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_test.R,line=257,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 129 characters.
} else if (method %in% c("blomqvist", "median", "medial")) {
insight::format_error("Bayesian Blomqvist correlations are not supported yet. Check-out the BBcor package (https://github.com/donaldRwilliams/BBcor).")

Check warning on line 259 in R/cor_test.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/cor_test.R,line=259,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 155 characters.
} else if (method == "hoeffding") {
insight::format_error("Bayesian Hoeffding's correlations are not supported yet. Check-out the BBcor package (https://github.com/donaldRwilliams/BBcor).")
} else if (method == "gamma") {
Expand Down
10 changes: 9 additions & 1 deletion R/correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,15 @@ correlation <- function(data,
include_factors <- TRUE
}

if (method == "polychoric") multilevel <- TRUE
# definitely need factors for polychoric
if (method == "polychoric") {
multilevel <- TRUE
# convert all input to factors, but only if all input currently is numeric
# we allow mix of numeric and factors
if (all(vapply(data, is.numeric, FUN.VALUE = TRUE))) {
data <- datawizard::to_factor(data)
}
}

# Clean data and get combinations -------------

Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-cor_test.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ test_that("cor_test tetrachoric", {
out <- cor_test(data, "Sepal.Width", "Petal.Width_binary", method = "biserial")
expect_equal(out$rho, -0.403, tolerance = 0.01)
out_psych <- psych::biserial(data[["Sepal.Width"]], data[["Petal.Width_binary"]])[1]

set.seed(123)
n <- 100
k <- 5
d <- data.frame(
x1 = sample.int(k, n, TRUE),
x2 = sample.int(k, n, TRUE),
x3 = sample.int(k, n, TRUE),
x4 = sample.int(k, n, TRUE)
)
expect_true(all(vapply(d, is.numeric, logical(1))))
out <- correlation(d, method = "polychoric")
expect_equal(
out$rho,
c(0.07729, -0.02453, -0.13999, 0.06508, -0.17158, 0.17863),
tolerance = 1e-3
)
})


Expand Down
Loading