Skip to content

Commit

Permalink
Merge pull request #336 from easystats/zap_small
Browse files Browse the repository at this point in the history
Correlation matrices now zap small by default
  • Loading branch information
strengejacke authored Jan 4, 2025
2 parents ac9ef1e + 4bdced8 commit 94f3d0a
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 103 deletions.
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.6.1
Version: 0.8.6.2
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# correlation 0.8.7

- The `format()` method for objects of class `easycormatrix` gets a `zap_small`
argument, to round very small numbers.

- `cor_sort()` can now deal with non-square matrices.

# correlation 0.8.6

- Fix CRAN check issues.
Expand Down
5 changes: 4 additions & 1 deletion R/methods_format.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ format.easycormatrix <- function(x,
stars = NULL,
include_significance = NULL,
format = NULL,
zap_small = NULL,
bf_exact = TRUE,
...) {
# If it's a real matrix
Expand All @@ -56,11 +57,13 @@ format.easycormatrix <- function(x,
stars <- .retrieve_arg_from_attr(attri, stars, default = TRUE)
include_significance <- .retrieve_arg_from_attr(attri, include_significance, default = FALSE)
p_digits <- .retrieve_arg_from_attr(attri, p_digits, default = "apa")
zap_small <- .retrieve_arg_from_attr(attri, zap_small, default = TRUE)


# Round and format values
nums <- sapply(as.data.frame(x), is.numeric)
x[, nums] <- sapply(as.data.frame(x)[, nums], insight::format_value, digits = digits)
x[, nums] <- sapply(as.data.frame(x)[, nums], insight::format_value,
digits = digits, zap_small = zap_small, ...)


# Deduct if stars only
Expand Down
96 changes: 48 additions & 48 deletions tests/testthat/_snaps/as_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@
p
---
Parameter | carb | gear | am | vs | qsec | wt | drat | hp | disp | cyl
-----------------------------------------------------------------------------------------------------------------------
mpg | 0.02 | 0.10 | 8.27e-03 | 1.09e-03 | 0.22 | 6.86e-09 | 5.86e-04 | 8.05e-06 | 4.78e-08 | 3.18e-08
cyl | 0.04 | 0.08 | 0.04 | 9.03e-07 | 0.01 | 5.60e-06 | 2.97e-04 | 1.74e-07 | 9.92e-11 |
disp | 0.30 | 0.02 | 0.01 | 2.04e-04 | 0.20 | 6.60e-10 | 2.04e-04 | 3.36e-06 | |
hp | 3.44e-05 | 1.00 | 1.00 | 1.24e-04 | 2.13e-04 | 1.29e-03 | 0.17 | | |
drat | 1.00 | 2.97e-04 | 1.94e-04 | 0.19 | 1.00 | 1.94e-04 | | | |
wt | 0.20 | 0.01 | 3.83e-04 | 0.02 | 1.00 | | | | |
qsec | 1.36e-03 | 1.00 | 1.00 | 4.43e-05 | | | | | |
vs | 0.02 | 1.00 | 1.00 | | | | | | |
am | 1.00 | 2.80e-06 | | | | | | | |
gear | 1.00 | | | | | | | | |
Parameter | carb | gear | am | vs | qsec | wt | drat | hp | disp | cyl
-------------------------------------------------------------------------------
mpg | 0.02 | 0.10 | 0.01 | 0.00 | 0.22 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00
cyl | 0.04 | 0.08 | 0.04 | 0.00 | 0.01 | 0.00 | 0.00 | 0.00 | 0.00 |
disp | 0.30 | 0.02 | 0.01 | 0.00 | 0.20 | 0.00 | 0.00 | 0.00 | |
hp | 0.00 | 1.00 | 1.00 | 0.00 | 0.00 | 0.00 | 0.17 | | |
drat | 1.00 | 0.00 | 0.00 | 0.19 | 1.00 | 0.00 | | | |
wt | 0.20 | 0.01 | 0.00 | 0.02 | 1.00 | | | | |
qsec | 0.00 | 1.00 | 1.00 | 0.00 | | | | | |
vs | 0.02 | 1.00 | 1.00 | | | | | | |
am | 1.00 | 0.00 | | | | | | | |
gear | 1.00 | | | | | | | | |

Expand Down Expand Up @@ -85,13 +85,13 @@
p
---
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
---------------------------------------------------------------------------
carni | sleep_total | 0.37 | 0.73 | 0.00 | 1.00 | 3.19e-04
carni | sleep_rem | 0.20 | 1.00 | 3.19e-04 | 1.00 |
carni | sleep_cycle | 1.00 | 1.00 | 1.00 | |
carni | awake | 0.37 | 0.73 | | |
carni | brainwt | 0.09 | | | |
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
------------------------------------------------------------------------
carni | sleep_total | 0.37 | 0.73 | 0.00 | 1.00 | 0.00
carni | sleep_rem | 0.20 | 1.00 | 0.00 | 1.00 |
carni | sleep_cycle | 1.00 | 1.00 | 1.00 | |
carni | awake | 0.37 | 0.73 | | |
carni | brainwt | 0.09 | | | |
Expand Down Expand Up @@ -123,13 +123,13 @@
p
---
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
------------------------------------------------------------------------------
herbi | sleep_total | 3.42e-06 | 8.71e-06 | 0.00 | 0.35 | 5.00e-09
herbi | sleep_rem | 4.65e-04 | 4.43e-03 | 5.00e-09 | 0.35 |
herbi | sleep_cycle | 0.03 | 0.04 | 0.35 | |
herbi | awake | 3.42e-06 | 8.71e-06 | | |
herbi | brainwt | 5.17e-13 | | | |
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
------------------------------------------------------------------------
herbi | sleep_total | 0.00 | 0.00 | 0.00 | 0.35 | 0.00
herbi | sleep_rem | 0.00 | 0.00 | 0.00 | 0.35 |
herbi | sleep_cycle | 0.03 | 0.04 | 0.35 | |
herbi | awake | 0.00 | 0.00 | | |
herbi | brainwt | 0.00 | | | |
Expand Down Expand Up @@ -161,13 +161,13 @@
p
---
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
-------------------------------------------------------------------------------
insecti | sleep_total | 1.00 | 1.00 | 1.46e-22 | 1.00 | 1.00
insecti | sleep_rem | 1.00 | 1.00 | 1.00 | 0.00 |
insecti | sleep_cycle | 1.00 | 1.00 | 1.00 | |
insecti | awake | 1.00 | 1.00 | | |
insecti | brainwt | 5.56e-23 | | | |
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
--------------------------------------------------------------------------
insecti | sleep_total | 1.00 | 1.00 | 0.00 | 1.00 | 1.00
insecti | sleep_rem | 1.00 | 1.00 | 1.00 | 0.00 |
insecti | sleep_cycle | 1.00 | 1.00 | 1.00 | |
insecti | awake | 1.00 | 1.00 | | |
insecti | brainwt | 0.00 | | | |
Expand Down Expand Up @@ -199,13 +199,13 @@
p
---
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
---------------------------------------------------------------------------
omni | sleep_total | 1.00 | 1.00 | 0.00 | 1.00 | 1.00
omni | sleep_rem | 1.00 | 1.00 | 1.00 | 1.00 |
omni | sleep_cycle | 0.04 | 7.73e-04 | 1.00 | |
omni | awake | 1.00 | 1.00 | | |
omni | brainwt | 7.64e-06 | | | |
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
------------------------------------------------------------------------
omni | sleep_total | 1.00 | 1.00 | 0.00 | 1.00 | 1.00
omni | sleep_rem | 1.00 | 1.00 | 1.00 | 1.00 |
omni | sleep_cycle | 0.04 | 0.00 | 1.00 | |
omni | awake | 1.00 | 1.00 | | |
omni | brainwt | 0.00 | | | |
Expand Down Expand Up @@ -237,10 +237,10 @@
p
---
Group | Parameter | hp
----------------------------
0 | cyl | 2.11e-06
0 | wt | 1.11e-05
Group | Parameter | hp
------------------------
0 | cyl | 0.00
0 | wt | 0.00
Expand All @@ -266,10 +266,10 @@
p
---
Group | Parameter | hp
----------------------------
1 | cyl | 9.58e-04
1 | wt | 1.04e-03
Group | Parameter | hp
------------------------
1 | cyl | 0.00
1 | wt | 0.00
Expand Down
49 changes: 30 additions & 19 deletions tests/testthat/_snaps/display_print_dataframe.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
# display and print method works - markdown

Code
print(correlation(iris), format = "markdown")
display(correlation(iris))
Output
Table: Correlation Matrix (pearson-method) |Parameter1 | Parameter2| r | 95% CI | t(148)| p | |:------------|------------:|:-----|:--------------|------:|:---------| |Sepal.Length | Sepal.Width|-0.12 |[-0.27, 0.04] | -1.44|0.152 | |Sepal.Length | Petal.Length| 0.87 |[ 0.83, 0.91] | 21.65|< .001*** | |Sepal.Length | Petal.Width| 0.82 |[ 0.76, 0.86] | 17.30|< .001*** | |Sepal.Width | Petal.Length|-0.43 |[-0.55, -0.29] | -5.77|< .001*** | |Sepal.Width | Petal.Width|-0.37 |[-0.50, -0.22] | -4.79|< .001*** | |Petal.Length | Petal.Width| 0.96 |[ 0.95, 0.97] | 43.39|< .001*** | p-value adjustment method: Holm (1979) Observations: 150
Table: Correlation Matrix (pearson-method)
|Parameter1 | Parameter2 | r | 95% CI | t(148) | p |
|:------------|:------------:|:-----:|:--------------:|:------:|:---------:|
|Sepal.Length | Sepal.Width | -0.12 | (-0.27, 0.04) | -1.44 | 0.152 |
|Sepal.Length | Petal.Length | 0.87 | (0.83, 0.91) | 21.65 | < .001*** |
|Sepal.Length | Petal.Width | 0.82 | (0.76, 0.86) | 17.30 | < .001*** |
|Sepal.Width | Petal.Length | -0.43 | (-0.55, -0.29) | -5.77 | < .001*** |
|Sepal.Width | Petal.Width | -0.37 | (-0.50, -0.22) | -4.79 | < .001*** |
|Petal.Length | Petal.Width | 0.96 | (0.95, 0.97) | 43.39 | < .001*** |
p-value adjustment method: Holm (1979)
Observations: 150

---

Code
display(correlation(iris))
print_md(correlation(iris))
Output
[1] "Table: Correlation Matrix (pearson-method)"
[2] ""
[3] "|Parameter1 | Parameter2 | r | 95% CI | t(148) | p |"
[4] "|:------------|:------------:|:-----:|:--------------:|:------:|:---------:|"
[5] "|Sepal.Length | Sepal.Width | -0.12 | (-0.27, 0.04) | -1.44 | 0.152 |"
[6] "|Sepal.Length | Petal.Length | 0.87 | (0.83, 0.91) | 21.65 | < .001*** |"
[7] "|Sepal.Length | Petal.Width | 0.82 | (0.76, 0.86) | 17.30 | < .001*** |"
[8] "|Sepal.Width | Petal.Length | -0.43 | (-0.55, -0.29) | -5.77 | < .001*** |"
[9] "|Sepal.Width | Petal.Width | -0.37 | (-0.50, -0.22) | -4.79 | < .001*** |"
[10] "|Petal.Length | Petal.Width | 0.96 | (0.95, 0.97) | 43.39 | < .001*** |"
[11] "p-value adjustment method: Holm (1979)"
[12] "Observations: 150"
attr(,"format")
[1] "pipe"
attr(,"class")
[1] "knitr_kable" "character"
Table: Correlation Matrix (pearson-method)
|Parameter1 | Parameter2 | r | 95% CI | t(148) | p |
|:------------|:------------:|:-----:|:--------------:|:------:|:---------:|
|Sepal.Length | Sepal.Width | -0.12 | (-0.27, 0.04) | -1.44 | 0.152 |
|Sepal.Length | Petal.Length | 0.87 | (0.83, 0.91) | 21.65 | < .001*** |
|Sepal.Length | Petal.Width | 0.82 | (0.76, 0.86) | 17.30 | < .001*** |
|Sepal.Width | Petal.Length | -0.43 | (-0.55, -0.29) | -5.77 | < .001*** |
|Sepal.Width | Petal.Width | -0.37 | (-0.50, -0.22) | -4.79 | < .001*** |
|Petal.Length | Petal.Width | 0.96 | (0.95, 0.97) | 43.39 | < .001*** |
p-value adjustment method: Holm (1979)
Observations: 150

37 changes: 18 additions & 19 deletions tests/testthat/_snaps/display_print_matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,31 @@
Code
display(summary(correlation(iris)))
Output
[1] "Table: Correlation Matrix (pearson-method)"
[2] ""
[3] "|Parameter | Petal.Width | Petal.Length | Sepal.Width |"
[4] "|:------------|:-----------:|:------------:|:-----------:|"
[5] "|Sepal.Length | 0.82*** | 0.87*** | -0.12 |"
[6] "|Sepal.Width | -0.37*** | -0.43*** | |"
[7] "|Petal.Length | 0.96*** | | |"
[8] "p-value adjustment method: Holm (1979)"
attr(,"format")
[1] "pipe"
attr(,"class")
[1] "knitr_kable" "character"
Table: Correlation Matrix (pearson-method)
|Parameter | Petal.Width | Petal.Length | Sepal.Width |
|:------------|:-----------:|:------------:|:-----------:|
|Sepal.Length | 0.82*** | 0.87*** | -0.12 |
|Sepal.Width | -0.37*** | -0.43*** | |
|Petal.Length | 0.96*** | | |
p-value adjustment method: Holm (1979)

---

Code
print(summary(correlation(iris)))
print_md(summary(correlation(iris)))
Output
# Correlation Matrix (pearson-method)
Parameter | Petal.Width | Petal.Length | Sepal.Width
-------------------------------------------------------
Sepal.Length | 0.82*** | 0.87*** | -0.12
Sepal.Width | -0.37*** | -0.43*** |
Petal.Length | 0.96*** | |
Table: Correlation Matrix (pearson-method)
|Parameter | Petal.Width | Petal.Length | Sepal.Width |
|:------------|:-----------:|:------------:|:-----------:|
|Sepal.Length | 0.82*** | 0.87*** | -0.12 |
|Sepal.Width | -0.37*** | -0.43*** | |
|Petal.Length | 0.96*** | | |
p-value adjustment method: Holm (1979)

# as.matrix works
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/_snaps/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
Output
# Correlation Matrix (pearson-method)
Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
-----------------------------------------------------------------------
sleep_total | 0.04 | 0.05 | 3.63e-225 | 0.05 | 3.78e-11
sleep_rem | 0.05 | 0.13 | 3.78e-11 | 0.12 |
sleep_cycle | 0.05 | 2.66e-08 | 0.05 | |
awake | 0.04 | 0.05 | | |
brainwt | 1.28e-24 | | | |
Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
----------------------------------------------------------------
sleep_total | 0.04 | 0.05 | 0.00 | 0.05 | 0.00
sleep_rem | 0.05 | 0.13 | 0.00 | 0.12 |
sleep_cycle | 0.05 | 0.00 | 0.05 | |
awake | 0.04 | 0.05 | | |
brainwt | 0.00 | | | |
p-value adjustment method: Holm (1979)

28 changes: 28 additions & 0 deletions tests/testthat/_snaps/windows/correlation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# correlation output with zap_small

Code
summary(r)
Output
# Correlation Matrix (pearson-method)
Parameter | z | y
-----------------------
x | 0.02 | 0.01
y | 0.00 |
p-value adjustment method: Holm (1979)

---

Code
summary(r, zap_small = FALSE)
Output
# Correlation Matrix (pearson-method)
Parameter | z | y
--------------------------------
x | 0.02 | 6.02e-03
y | -3.07e-03 |
p-value adjustment method: Holm (1979)

13 changes: 13 additions & 0 deletions tests/testthat/test-correlation.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,16 @@ test_that("as.data.frame for correlation output", {
set.seed(123)
expect_snapshot(as.data.frame(correlation(ggplot2::msleep)))
})

test_that("correlation output with zap_small", {
set.seed(123)
d <- data.frame(
x = rnorm(10000),
y = rnorm(10000),
z = rnorm(10000)
)
r <- correlation::correlation(d)

expect_snapshot(summary(r), variant = "windows")
expect_snapshot(summary(r, zap_small = FALSE), variant = "windows")
})
6 changes: 2 additions & 4 deletions tests/testthat/test-display_print_dataframe.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
test_that("display and print method works - markdown", {
skip_on_cran()
skip_if(getRversion() < "4.0.0")
skip_if_not_or_load_if_installed("gt")

expect_snapshot(print(correlation(iris), format = "markdown"))
skip_if_not_or_load_if_installed("knitr")
expect_snapshot(display(correlation(iris)))
expect_snapshot(print_md(correlation(iris)))
})

# display and print method works - HTML -----------------------------
Expand Down
Loading

0 comments on commit 94f3d0a

Please sign in to comment.