Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correlation matrices now zap small by default #336

Merged
merged 13 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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.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,5 +1,12 @@
# correlation 0.8.6

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

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

# correlation 0.8.6

- Fix CRAN check issues.

# correlation 0.8.5
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
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")
})
Loading