diff --git a/DESCRIPTION b/DESCRIPTION index 7bd16232..de86ddb5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: correlation Title: Methods for Correlation Analysis -Version: 0.8.8 +Version: 0.8.8.1 Authors@R: c(person(given = "Dominique", family = "Makowski", @@ -88,7 +88,7 @@ VignetteBuilder: knitr Encoding: UTF-8 Language: en-US -RoxygenNote: 7.3.2 +RoxygenNote: 7.3.3 Roxygen: list(markdown = TRUE) Config/testthat/edition: 3 Config/Needs/website: diff --git a/NEWS.md b/NEWS.md index c9759e1c..e583219f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,11 @@ +# correlation 0.8.9 + +## Bug Fixes + +- `cormatrix_to_excel()` now works correctly with openxlsx2 v1.16+. Fixed + conditional formatting rule ordering to accommodate openxlsx2's new waterfall + strategy (#361). + # correlation 0.8.8 - `correlation()` gains a `missing=` argument, similar to `stats::cor(use=)`, for controlling how missing data is handled. diff --git a/R/cormatrix_to_excel.R b/R/cormatrix_to_excel.R index a4448ef3..d383b5aa 100644 --- a/R/cormatrix_to_excel.R +++ b/R/cormatrix_to_excel.R @@ -41,13 +41,17 @@ #' \dontshow{ #' setwd(.old_wd) #' } -cormatrix_to_excel <- function(data, - filename, - overwrite = TRUE, - print.mat = TRUE, - ...) { +cormatrix_to_excel <- function( + data, + filename, + overwrite = TRUE, + print.mat = TRUE, + ... +) { if (missing(filename)) { - insight::format_error("Argument 'filename' required (as per CRAN policies).") + insight::format_error( + "Argument 'filename' required (as per CRAN policies)." + ) } insight::check_if_installed("openxlsx2") @@ -241,52 +245,61 @@ cormatrix_to_excel <- function(data, wb$add_worksheet("p_values")$add_data(x = p_val) # create conditional formatting for the stars (as well as colours as we have no) - # one star # Compute the cell range for styling dims_fmt <- openxlsx2::wb_dims(cols = all.columns, rows = all.columns) + + # gray diagonal (most specific, must be first to win for r = 1) wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 <= .2, r_values!B2 > 0, p_values!B2 < .05)", - style = "one_star_pink" + rule = "r_values!B2 = 1", + style = "gray_style" ) + + # three stars (p < .001) - most specific color ranges first wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 >= .2, p_values!B2 < .05)", - style = "one_star_peach" + rule = "AND(r_values!B2 >= .4, p_values!B2 < .001)", + style = "three_stars_red" ) wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 >= .4, p_values!B2 < .05)", - style = "one_star_red" + rule = "AND(r_values!B2 >= .2, p_values!B2 < .001)", + style = "three_stars_peach" ) wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 >= -.2, r_values!B2 < 0, p_values!B2 < .05)", - style = "one_star_lightblue" + rule = "AND(r_values!B2 <= .2, r_values!B2 > 0, p_values!B2 < .001)", + style = "three_stars_pink" ) wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 <= -.2, p_values!B2 < .05)", - style = "one_star_midblue" + rule = "AND(r_values!B2 <= -.4, p_values!B2 < .001)", + style = "three_stars_darkblue" ) wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 <= -.4, p_values!B2 < .05)", - style = "one_star_darkblue" + rule = "AND(r_values!B2 <= -.2, p_values!B2 < .001)", + style = "three_stars_midblue" + ) + wb$add_conditional_formatting( + "r_values", + dims = dims_fmt, + rule = "AND(r_values!B2 >= -.2, r_values!B2 < 0, p_values!B2 < .001)", + style = "three_stars_lightblue" ) - # two stars + # two stars (p < .01) - most specific color ranges first wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 <= .2, r_values!B2 > 0, p_values!B2 < .01)", - style = "two_stars_pink" + rule = "AND(r_values!B2 >= .4, p_values!B2 < .01)", + style = "two_stars_red" ) wb$add_conditional_formatting( "r_values", @@ -297,14 +310,14 @@ cormatrix_to_excel <- function(data, wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 >= .4, p_values!B2 < .01)", - style = "two_stars_red" + rule = "AND(r_values!B2 <= .2, r_values!B2 > 0, p_values!B2 < .01)", + style = "two_stars_pink" ) wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 >= -.02, r_values!B2 < 0, p_values!B2 < .01)", - style = "two_stars_lightblue" + rule = "AND(r_values!B2 <= -.4, p_values!B2 < .01)", + style = "two_stars_darkblue" ) wb$add_conditional_formatting( "r_values", @@ -315,55 +328,49 @@ cormatrix_to_excel <- function(data, wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 <= -.4, p_values!B2 < .01)", - style = "two_stars_darkblue" + rule = "AND(r_values!B2 >= -.2, r_values!B2 < 0, p_values!B2 < .01)", + style = "two_stars_lightblue" ) - # three stars + # one star (p < .05) - most specific color ranges first wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 <= .2, r_values!B2 > 0, p_values!B2 < .001)", - style = "three_stars_pink" + rule = "AND(r_values!B2 >= .4, p_values!B2 < .05)", + style = "one_star_red" ) wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 >= .2, p_values!B2 < .001)", - style = "three_stars_peach" + rule = "AND(r_values!B2 >= .2, p_values!B2 < .05)", + style = "one_star_peach" ) wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 >= .4, p_values!B2 < .001)", - style = "three_stars_red" + rule = "AND(r_values!B2 <= .2, r_values!B2 > 0, p_values!B2 < .05)", + style = "one_star_pink" ) wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 >= -.2, r_values!B2 < 0, p_values!B2 < .001)", - style = "three_stars_lightblue" + rule = "AND(r_values!B2 <= -.4, p_values!B2 < .05)", + style = "one_star_darkblue" ) wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 <= -.2, p_values!B2 < .001)", - style = "three_stars_midblue" + rule = "AND(r_values!B2 <= -.2, p_values!B2 < .05)", + style = "one_star_midblue" ) wb$add_conditional_formatting( "r_values", dims = dims_fmt, - rule = "AND(r_values!B2 <= -.4, p_values!B2 < .001)", - style = "three_stars_darkblue" + rule = "AND(r_values!B2 >= -.2, r_values!B2 < 0, p_values!B2 < .05)", + style = "one_star_lightblue" ) - # Other formatting - wb$add_conditional_formatting( - "r_values", - dims = dims_fmt, - rule = "AND(r_values!B2 = 1)", - style = "gray_style" - ) + # no stars (for non-significant correlations) wb$add_conditional_formatting( "r_values", dims = dims_fmt, @@ -371,18 +378,18 @@ cormatrix_to_excel <- function(data, style = "no_star" ) - # p-values + # p-values (most specific first) wb$add_conditional_formatting( "p_values", dims = dims_fmt, - rule = "< 10", - style = "p_style" + rule = "== 0", + style = "gray_style" ) wb$add_conditional_formatting( "p_values", dims = dims_fmt, - rule = "< .05", - style = "p_style1" + rule = "< .001", + style = "p_style3" ) wb$add_conditional_formatting( "p_values", @@ -393,14 +400,14 @@ cormatrix_to_excel <- function(data, wb$add_conditional_formatting( "p_values", dims = dims_fmt, - rule = "< .001", - style = "p_style3" + rule = "< .05", + style = "p_style1" ) wb$add_conditional_formatting( "p_values", dims = dims_fmt, - rule = "== 0", - style = "gray_style" + rule = "< 10", + style = "p_style" ) ## Freeze Panes @@ -409,7 +416,8 @@ cormatrix_to_excel <- function(data, # Save Excel cat(paste0( - "\n\n [Correlation matrix '", filename, + "\n\n [Correlation matrix '", + filename, ".xlsx' has been saved to working directory (or where specified).]" )) openxlsx2::wb_save(wb, file = paste0(filename, ".xlsx"), overwrite = TRUE) diff --git a/man/cor_smooth.Rd b/man/cor_smooth.Rd index b2806a48..ddb3fa32 100644 --- a/man/cor_smooth.Rd +++ b/man/cor_smooth.Rd @@ -32,7 +32,7 @@ is done, inferential statistics (\emph{p}-values, confidence intervals, etc.) ar removed, as they are no longer valid. } \examples{ -\dontshow{if (requireNamespace("psych", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (requireNamespace("psych", quietly = TRUE)) withAutoprint(\{ # examplesIf} set.seed(123) data <- as.matrix(mtcars) # Make missing data so pairwise correlation matrix is non-positive definite diff --git a/man/cormatrix_to_excel.Rd b/man/cormatrix_to_excel.Rd index ca5ae021..e140ffa6 100644 --- a/man/cormatrix_to_excel.Rd +++ b/man/cormatrix_to_excel.Rd @@ -33,7 +33,7 @@ correlation coefficients colour-coded based on effect size for small (.10), medium (.30), and large (.50) correlation sizes. } \examples{ -\dontshow{if (requireNamespace("openxlsx2", quietly = TRUE)) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (requireNamespace("openxlsx2", quietly = TRUE)) withAutoprint(\{ # examplesIf} \dontshow{ .old_wd <- setwd(tempdir()) } diff --git a/man/correlation.Rd b/man/correlation.Rd index 69dc1387..7852dbf9 100644 --- a/man/correlation.Rd +++ b/man/correlation.Rd @@ -272,7 +272,7 @@ Bayesian rank correlations (which have different priors). } } \examples{ -\dontshow{if (all(insight::check_if_installed(c("psych", "datawizard"), quietly = TRUE)) && getRversion() >= "4.1.0") (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (all(insight::check_if_installed(c("psych", "datawizard"), quietly = TRUE)) && getRversion() >= "4.1.0") withAutoprint(\{ # examplesIf} library(correlation) data(iris) diff --git a/man/visualisation_recipe.easycormatrix.Rd b/man/visualisation_recipe.easycormatrix.Rd index 453b1308..6ddf9ed4 100644 --- a/man/visualisation_recipe.easycormatrix.Rd +++ b/man/visualisation_recipe.easycormatrix.Rd @@ -61,7 +61,7 @@ the arguments described below. See examples \href{https://easystats.github.io/correlation/reference/visualisation_recipe.easycormatrix.html#ref-examples}{\strong{here}}. } \examples{ -\dontshow{if (require("see")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (require("see")) withAutoprint(\{ # examplesIf} \donttest{ rez <- cor_test(mtcars, "mpg", "wt") @@ -77,7 +77,7 @@ plot(rez, ) } \dontshow{\}) # examplesIf} -\dontshow{if (require("see")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (require("see")) withAutoprint(\{ # examplesIf} \donttest{ rez <- correlation(mtcars) @@ -106,7 +106,7 @@ layers <- visualisation_recipe(x, plot(layers) + theme_modern() } \dontshow{\}) # examplesIf} -\dontshow{if (require("see") && require("tidygraph") && require("ggraph")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} +\dontshow{if (require("see") && require("tidygraph") && require("ggraph")) withAutoprint(\{ # examplesIf} \donttest{ rez <- correlation(iris) diff --git a/tests/testthat/_snaps/renaming.md b/tests/testthat/_snaps/renaming.md index 2fe9f676..9423b06a 100644 --- a/tests/testthat/_snaps/renaming.md +++ b/tests/testthat/_snaps/renaming.md @@ -5,9 +5,9 @@ Output # Correlation Matrix (pearson-method) - Parameter1 | Parameter2 | r | 95% CI | t(9) | p - ---------------------------------------------------------------- - x1 | x2 | 1.00 | [1.00, 1.00] | Inf | < .001*** + Parameter1 | Parameter2 | r | 95% CI | t(9) | p + ------------------------------------------------------------- + x1 | x2 | 1 | [1.00, 1.00] | Inf | < .001*** p-value adjustment method: Holm (1979) Observations: 11 @@ -19,9 +19,9 @@ Output # Correlation Matrix (pearson-method) - Parameter1 | Parameter2 | r | 95% CI | t(9) | p - ---------------------------------------------------------------- - var1 | var2 | 1.00 | [1.00, 1.00] | Inf | < .001*** + Parameter1 | Parameter2 | r | 95% CI | t(9) | p + ------------------------------------------------------------- + var1 | var2 | 1 | [1.00, 1.00] | Inf | < .001*** p-value adjustment method: Holm (1979) Observations: 11 diff --git a/tests/testthat/test-cor_test_na_present.R b/tests/testthat/test-cor_test_na_present.R index 8449c265..1a17a3ac 100644 --- a/tests/testthat/test-cor_test_na_present.R +++ b/tests/testthat/test-cor_test_na_present.R @@ -11,7 +11,11 @@ test_that("cor_test kendall", { skip_if_not_or_load_if_installed("ggplot2") out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "kendall") - out2 <- suppressWarnings(stats::cor.test(ggplot2::msleep$brainwt, ggplot2::msleep$sleep_rem, method = "kendall")) + out2 <- suppressWarnings(stats::cor.test( + ggplot2::msleep$brainwt, + ggplot2::msleep$sleep_rem, + method = "kendall" + )) expect_equal(out$tau, out2$estimate[[1]], tolerance = 0.001) expect_equal(out$p, out2$p.value[[1]], tolerance = 0.001) @@ -35,26 +39,58 @@ test_that("cor_test tetrachoric", { data$sleep_rem_binary <- as.numeric(data$sleep_rem > 1.2) # With Factors / Binary - expect_error(cor_test(data, "brainwt_binary", "sleep_rem_binary", method = "tetrachoric")) + expect_warning(expect_error(cor_test( + data, + "brainwt_binary", + "sleep_rem_binary", + method = "tetrachoric" + ))) data$sleep_rem_ordinal <- as.factor(round(data$sleep_rem)) data$brainwt_ordinal <- as.factor(round(data$brainwt)) - out <- suppressWarnings(cor_test(data, "brainwt", "brainwt_ordinal", method = "polychoric")) + out <- suppressWarnings(cor_test( + data, + "brainwt", + "brainwt_ordinal", + method = "polychoric" + )) expect_equal(out$rho, 0.9999, tolerance = 0.01) # Biserial - expect_error(cor_test(data, "brainwt", "sleep_rem_binary", method = "pointbiserial")) - - expect_error(cor_test(data, "brainwt", "sleep_rem_binary", method = "biserial")) + expect_error(cor_test( + data, + "brainwt", + "sleep_rem_binary", + method = "pointbiserial" + )) + + expect_error(cor_test( + data, + "brainwt", + "sleep_rem_binary", + method = "biserial" + )) }) test_that("cor_test robust", { skip_if_not_or_load_if_installed("ggplot2") - out1 <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "pearson", ranktransform = TRUE) - out2 <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "spearman", ranktransform = FALSE) + out1 <- cor_test( + ggplot2::msleep, + "brainwt", + "sleep_rem", + method = "pearson", + ranktransform = TRUE + ) + out2 <- cor_test( + ggplot2::msleep, + "brainwt", + "sleep_rem", + method = "spearman", + ranktransform = FALSE + ) expect_equal(out1$r, out2$rho, tolerance = 0.01) }) @@ -75,7 +111,12 @@ test_that("cor_test percentage", { skip_if_not_or_load_if_installed("ggplot2") skip_if_not_or_load_if_installed("WRS2") - out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "percentage") + out <- cor_test( + ggplot2::msleep, + "brainwt", + "sleep_rem", + method = "percentage" + ) comparison <- WRS2::pbcor(ggplot2::msleep$brainwt, ggplot2::msleep$sleep_rem) expect_equal(out$r, as.numeric(comparison$cor), tolerance = 0.01) }) @@ -85,7 +126,12 @@ test_that("cor_test shepherd", { skip_if_not_or_load_if_installed("ggplot2") set.seed(333) - expect_error(cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "shepherd")) + expect_error(cor_test( + ggplot2::msleep, + "brainwt", + "sleep_rem", + method = "shepherd" + )) }) @@ -121,19 +167,33 @@ test_that("cor_test gaussian", { expect_equal(out$r, -0.3679795, tolerance = 0.01) skip_if_not_or_load_if_installed("BayesFactor") - out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", method = "gaussian", bayesian = TRUE) + out <- cor_test( + ggplot2::msleep, + "brainwt", + "sleep_rem", + method = "gaussian", + bayesian = TRUE + ) expect_equal(out$rho, -0.3269572, tolerance = 0.01) }) # Additional arguments ---------------------------------------------------- - test_that("cor_test one-sided p value", { skip_if_not_or_load_if_installed("ggplot2") - baseline <- cor.test(ggplot2::msleep$brainwt, ggplot2::msleep$sleep_rem, alternative = "greater") - - out <- cor_test(ggplot2::msleep, "brainwt", "sleep_rem", alternative = "greater") + baseline <- cor.test( + ggplot2::msleep$brainwt, + ggplot2::msleep$sleep_rem, + alternative = "greater" + ) + + out <- cor_test( + ggplot2::msleep, + "brainwt", + "sleep_rem", + alternative = "greater" + ) expect_equal(out$p, baseline$p.value, tolerance = 0.000001) })