Skip to content

Commit 94f3d0a

Browse files
authored
Merge pull request #336 from easystats/zap_small
Correlation matrices now zap small by default
2 parents ac9ef1e + 4bdced8 commit 94f3d0a

11 files changed

+162
-103
lines changed

DESCRIPTION

+1-1
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.6.1
4+
Version: 0.8.6.2
55
Authors@R:
66
c(person(given = "Dominique",
77
family = "Makowski",

NEWS.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# correlation 0.8.7
2+
3+
- The `format()` method for objects of class `easycormatrix` gets a `zap_small`
4+
argument, to round very small numbers.
5+
6+
- `cor_sort()` can now deal with non-square matrices.
7+
18
# correlation 0.8.6
29

310
- Fix CRAN check issues.

R/methods_format.R

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ format.easycormatrix <- function(x,
4040
stars = NULL,
4141
include_significance = NULL,
4242
format = NULL,
43+
zap_small = NULL,
4344
bf_exact = TRUE,
4445
...) {
4546
# If it's a real matrix
@@ -56,11 +57,13 @@ format.easycormatrix <- function(x,
5657
stars <- .retrieve_arg_from_attr(attri, stars, default = TRUE)
5758
include_significance <- .retrieve_arg_from_attr(attri, include_significance, default = FALSE)
5859
p_digits <- .retrieve_arg_from_attr(attri, p_digits, default = "apa")
60+
zap_small <- .retrieve_arg_from_attr(attri, zap_small, default = TRUE)
5961

6062

6163
# Round and format values
6264
nums <- sapply(as.data.frame(x), is.numeric)
63-
x[, nums] <- sapply(as.data.frame(x)[, nums], insight::format_value, digits = digits)
65+
x[, nums] <- sapply(as.data.frame(x)[, nums], insight::format_value,
66+
digits = digits, zap_small = zap_small, ...)
6467

6568

6669
# Deduct if stars only

tests/testthat/_snaps/as_list.md

+48-48
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@
3737
3838
p
3939
---
40-
Parameter | carb | gear | am | vs | qsec | wt | drat | hp | disp | cyl
41-
-----------------------------------------------------------------------------------------------------------------------
42-
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
43-
cyl | 0.04 | 0.08 | 0.04 | 9.03e-07 | 0.01 | 5.60e-06 | 2.97e-04 | 1.74e-07 | 9.92e-11 |
44-
disp | 0.30 | 0.02 | 0.01 | 2.04e-04 | 0.20 | 6.60e-10 | 2.04e-04 | 3.36e-06 | |
45-
hp | 3.44e-05 | 1.00 | 1.00 | 1.24e-04 | 2.13e-04 | 1.29e-03 | 0.17 | | |
46-
drat | 1.00 | 2.97e-04 | 1.94e-04 | 0.19 | 1.00 | 1.94e-04 | | | |
47-
wt | 0.20 | 0.01 | 3.83e-04 | 0.02 | 1.00 | | | | |
48-
qsec | 1.36e-03 | 1.00 | 1.00 | 4.43e-05 | | | | | |
49-
vs | 0.02 | 1.00 | 1.00 | | | | | | |
50-
am | 1.00 | 2.80e-06 | | | | | | | |
51-
gear | 1.00 | | | | | | | | |
40+
Parameter | carb | gear | am | vs | qsec | wt | drat | hp | disp | cyl
41+
-------------------------------------------------------------------------------
42+
mpg | 0.02 | 0.10 | 0.01 | 0.00 | 0.22 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00
43+
cyl | 0.04 | 0.08 | 0.04 | 0.00 | 0.01 | 0.00 | 0.00 | 0.00 | 0.00 |
44+
disp | 0.30 | 0.02 | 0.01 | 0.00 | 0.20 | 0.00 | 0.00 | 0.00 | |
45+
hp | 0.00 | 1.00 | 1.00 | 0.00 | 0.00 | 0.00 | 0.17 | | |
46+
drat | 1.00 | 0.00 | 0.00 | 0.19 | 1.00 | 0.00 | | | |
47+
wt | 0.20 | 0.01 | 0.00 | 0.02 | 1.00 | | | | |
48+
qsec | 0.00 | 1.00 | 1.00 | 0.00 | | | | | |
49+
vs | 0.02 | 1.00 | 1.00 | | | | | | |
50+
am | 1.00 | 0.00 | | | | | | | |
51+
gear | 1.00 | | | | | | | | |
5252
5353
5454

@@ -85,13 +85,13 @@
8585
8686
p
8787
---
88-
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
89-
---------------------------------------------------------------------------
90-
carni | sleep_total | 0.37 | 0.73 | 0.00 | 1.00 | 3.19e-04
91-
carni | sleep_rem | 0.20 | 1.00 | 3.19e-04 | 1.00 |
92-
carni | sleep_cycle | 1.00 | 1.00 | 1.00 | |
93-
carni | awake | 0.37 | 0.73 | | |
94-
carni | brainwt | 0.09 | | | |
88+
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
89+
------------------------------------------------------------------------
90+
carni | sleep_total | 0.37 | 0.73 | 0.00 | 1.00 | 0.00
91+
carni | sleep_rem | 0.20 | 1.00 | 0.00 | 1.00 |
92+
carni | sleep_cycle | 1.00 | 1.00 | 1.00 | |
93+
carni | awake | 0.37 | 0.73 | | |
94+
carni | brainwt | 0.09 | | | |
9595
9696
9797
@@ -123,13 +123,13 @@
123123
124124
p
125125
---
126-
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
127-
------------------------------------------------------------------------------
128-
herbi | sleep_total | 3.42e-06 | 8.71e-06 | 0.00 | 0.35 | 5.00e-09
129-
herbi | sleep_rem | 4.65e-04 | 4.43e-03 | 5.00e-09 | 0.35 |
130-
herbi | sleep_cycle | 0.03 | 0.04 | 0.35 | |
131-
herbi | awake | 3.42e-06 | 8.71e-06 | | |
132-
herbi | brainwt | 5.17e-13 | | | |
126+
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
127+
------------------------------------------------------------------------
128+
herbi | sleep_total | 0.00 | 0.00 | 0.00 | 0.35 | 0.00
129+
herbi | sleep_rem | 0.00 | 0.00 | 0.00 | 0.35 |
130+
herbi | sleep_cycle | 0.03 | 0.04 | 0.35 | |
131+
herbi | awake | 0.00 | 0.00 | | |
132+
herbi | brainwt | 0.00 | | | |
133133
134134
135135
@@ -161,13 +161,13 @@
161161
162162
p
163163
---
164-
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
165-
-------------------------------------------------------------------------------
166-
insecti | sleep_total | 1.00 | 1.00 | 1.46e-22 | 1.00 | 1.00
167-
insecti | sleep_rem | 1.00 | 1.00 | 1.00 | 0.00 |
168-
insecti | sleep_cycle | 1.00 | 1.00 | 1.00 | |
169-
insecti | awake | 1.00 | 1.00 | | |
170-
insecti | brainwt | 5.56e-23 | | | |
164+
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
165+
--------------------------------------------------------------------------
166+
insecti | sleep_total | 1.00 | 1.00 | 0.00 | 1.00 | 1.00
167+
insecti | sleep_rem | 1.00 | 1.00 | 1.00 | 0.00 |
168+
insecti | sleep_cycle | 1.00 | 1.00 | 1.00 | |
169+
insecti | awake | 1.00 | 1.00 | | |
170+
insecti | brainwt | 0.00 | | | |
171171
172172
173173
@@ -199,13 +199,13 @@
199199
200200
p
201201
---
202-
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
203-
---------------------------------------------------------------------------
204-
omni | sleep_total | 1.00 | 1.00 | 0.00 | 1.00 | 1.00
205-
omni | sleep_rem | 1.00 | 1.00 | 1.00 | 1.00 |
206-
omni | sleep_cycle | 0.04 | 7.73e-04 | 1.00 | |
207-
omni | awake | 1.00 | 1.00 | | |
208-
omni | brainwt | 7.64e-06 | | | |
202+
Group | Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
203+
------------------------------------------------------------------------
204+
omni | sleep_total | 1.00 | 1.00 | 0.00 | 1.00 | 1.00
205+
omni | sleep_rem | 1.00 | 1.00 | 1.00 | 1.00 |
206+
omni | sleep_cycle | 0.04 | 0.00 | 1.00 | |
207+
omni | awake | 1.00 | 1.00 | | |
208+
omni | brainwt | 0.00 | | | |
209209
210210
211211
@@ -237,10 +237,10 @@
237237
238238
p
239239
---
240-
Group | Parameter | hp
241-
----------------------------
242-
0 | cyl | 2.11e-06
243-
0 | wt | 1.11e-05
240+
Group | Parameter | hp
241+
------------------------
242+
0 | cyl | 0.00
243+
0 | wt | 0.00
244244
245245
246246
@@ -266,10 +266,10 @@
266266
267267
p
268268
---
269-
Group | Parameter | hp
270-
----------------------------
271-
1 | cyl | 9.58e-04
272-
1 | wt | 1.04e-03
269+
Group | Parameter | hp
270+
------------------------
271+
1 | cyl | 0.00
272+
1 | wt | 0.00
273273
274274
275275
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
11
# display and print method works - markdown
22

33
Code
4-
print(correlation(iris), format = "markdown")
4+
display(correlation(iris))
55
Output
6-
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
6+
7+
8+
Table: Correlation Matrix (pearson-method)
9+
10+
|Parameter1 | Parameter2 | r | 95% CI | t(148) | p |
11+
|:------------|:------------:|:-----:|:--------------:|:------:|:---------:|
12+
|Sepal.Length | Sepal.Width | -0.12 | (-0.27, 0.04) | -1.44 | 0.152 |
13+
|Sepal.Length | Petal.Length | 0.87 | (0.83, 0.91) | 21.65 | < .001*** |
14+
|Sepal.Length | Petal.Width | 0.82 | (0.76, 0.86) | 17.30 | < .001*** |
15+
|Sepal.Width | Petal.Length | -0.43 | (-0.55, -0.29) | -5.77 | < .001*** |
16+
|Sepal.Width | Petal.Width | -0.37 | (-0.50, -0.22) | -4.79 | < .001*** |
17+
|Petal.Length | Petal.Width | 0.96 | (0.95, 0.97) | 43.39 | < .001*** |
18+
p-value adjustment method: Holm (1979)
19+
Observations: 150
720

821
---
922

1023
Code
11-
display(correlation(iris))
24+
print_md(correlation(iris))
1225
Output
13-
[1] "Table: Correlation Matrix (pearson-method)"
14-
[2] ""
15-
[3] "|Parameter1 | Parameter2 | r | 95% CI | t(148) | p |"
16-
[4] "|:------------|:------------:|:-----:|:--------------:|:------:|:---------:|"
17-
[5] "|Sepal.Length | Sepal.Width | -0.12 | (-0.27, 0.04) | -1.44 | 0.152 |"
18-
[6] "|Sepal.Length | Petal.Length | 0.87 | (0.83, 0.91) | 21.65 | < .001*** |"
19-
[7] "|Sepal.Length | Petal.Width | 0.82 | (0.76, 0.86) | 17.30 | < .001*** |"
20-
[8] "|Sepal.Width | Petal.Length | -0.43 | (-0.55, -0.29) | -5.77 | < .001*** |"
21-
[9] "|Sepal.Width | Petal.Width | -0.37 | (-0.50, -0.22) | -4.79 | < .001*** |"
22-
[10] "|Petal.Length | Petal.Width | 0.96 | (0.95, 0.97) | 43.39 | < .001*** |"
23-
[11] "p-value adjustment method: Holm (1979)"
24-
[12] "Observations: 150"
25-
attr(,"format")
26-
[1] "pipe"
27-
attr(,"class")
28-
[1] "knitr_kable" "character"
26+
27+
28+
Table: Correlation Matrix (pearson-method)
29+
30+
|Parameter1 | Parameter2 | r | 95% CI | t(148) | p |
31+
|:------------|:------------:|:-----:|:--------------:|:------:|:---------:|
32+
|Sepal.Length | Sepal.Width | -0.12 | (-0.27, 0.04) | -1.44 | 0.152 |
33+
|Sepal.Length | Petal.Length | 0.87 | (0.83, 0.91) | 21.65 | < .001*** |
34+
|Sepal.Length | Petal.Width | 0.82 | (0.76, 0.86) | 17.30 | < .001*** |
35+
|Sepal.Width | Petal.Length | -0.43 | (-0.55, -0.29) | -5.77 | < .001*** |
36+
|Sepal.Width | Petal.Width | -0.37 | (-0.50, -0.22) | -4.79 | < .001*** |
37+
|Petal.Length | Petal.Width | 0.96 | (0.95, 0.97) | 43.39 | < .001*** |
38+
p-value adjustment method: Holm (1979)
39+
Observations: 150
2940

tests/testthat/_snaps/display_print_matrix.md

+18-19
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,31 @@
33
Code
44
display(summary(correlation(iris)))
55
Output
6-
[1] "Table: Correlation Matrix (pearson-method)"
7-
[2] ""
8-
[3] "|Parameter | Petal.Width | Petal.Length | Sepal.Width |"
9-
[4] "|:------------|:-----------:|:------------:|:-----------:|"
10-
[5] "|Sepal.Length | 0.82*** | 0.87*** | -0.12 |"
11-
[6] "|Sepal.Width | -0.37*** | -0.43*** | |"
12-
[7] "|Petal.Length | 0.96*** | | |"
13-
[8] "p-value adjustment method: Holm (1979)"
14-
attr(,"format")
15-
[1] "pipe"
16-
attr(,"class")
17-
[1] "knitr_kable" "character"
6+
7+
8+
Table: Correlation Matrix (pearson-method)
9+
10+
|Parameter | Petal.Width | Petal.Length | Sepal.Width |
11+
|:------------|:-----------:|:------------:|:-----------:|
12+
|Sepal.Length | 0.82*** | 0.87*** | -0.12 |
13+
|Sepal.Width | -0.37*** | -0.43*** | |
14+
|Petal.Length | 0.96*** | | |
15+
p-value adjustment method: Holm (1979)
1816

1917
---
2018

2119
Code
22-
print(summary(correlation(iris)))
20+
print_md(summary(correlation(iris)))
2321
Output
24-
# Correlation Matrix (pearson-method)
2522
26-
Parameter | Petal.Width | Petal.Length | Sepal.Width
27-
-------------------------------------------------------
28-
Sepal.Length | 0.82*** | 0.87*** | -0.12
29-
Sepal.Width | -0.37*** | -0.43*** |
30-
Petal.Length | 0.96*** | |
3123
24+
Table: Correlation Matrix (pearson-method)
25+
26+
|Parameter | Petal.Width | Petal.Length | Sepal.Width |
27+
|:------------|:-----------:|:------------:|:-----------:|
28+
|Sepal.Length | 0.82*** | 0.87*** | -0.12 |
29+
|Sepal.Width | -0.37*** | -0.43*** | |
30+
|Petal.Length | 0.96*** | | |
3231
p-value adjustment method: Holm (1979)
3332

3433
# as.matrix works

tests/testthat/_snaps/methods.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@
3939
Output
4040
# Correlation Matrix (pearson-method)
4141
42-
Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
43-
-----------------------------------------------------------------------
44-
sleep_total | 0.04 | 0.05 | 3.63e-225 | 0.05 | 3.78e-11
45-
sleep_rem | 0.05 | 0.13 | 3.78e-11 | 0.12 |
46-
sleep_cycle | 0.05 | 2.66e-08 | 0.05 | |
47-
awake | 0.04 | 0.05 | | |
48-
brainwt | 1.28e-24 | | | |
42+
Parameter | bodywt | brainwt | awake | sleep_cycle | sleep_rem
43+
----------------------------------------------------------------
44+
sleep_total | 0.04 | 0.05 | 0.00 | 0.05 | 0.00
45+
sleep_rem | 0.05 | 0.13 | 0.00 | 0.12 |
46+
sleep_cycle | 0.05 | 0.00 | 0.05 | |
47+
awake | 0.04 | 0.05 | | |
48+
brainwt | 0.00 | | | |
4949
5050
p-value adjustment method: Holm (1979)
5151

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# correlation output with zap_small
2+
3+
Code
4+
summary(r)
5+
Output
6+
# Correlation Matrix (pearson-method)
7+
8+
Parameter | z | y
9+
-----------------------
10+
x | 0.02 | 0.01
11+
y | 0.00 |
12+
13+
p-value adjustment method: Holm (1979)
14+
15+
---
16+
17+
Code
18+
summary(r, zap_small = FALSE)
19+
Output
20+
# Correlation Matrix (pearson-method)
21+
22+
Parameter | z | y
23+
--------------------------------
24+
x | 0.02 | 6.02e-03
25+
y | -3.07e-03 |
26+
27+
p-value adjustment method: Holm (1979)
28+

tests/testthat/test-correlation.R

+13
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,16 @@ test_that("as.data.frame for correlation output", {
198198
set.seed(123)
199199
expect_snapshot(as.data.frame(correlation(ggplot2::msleep)))
200200
})
201+
202+
test_that("correlation output with zap_small", {
203+
set.seed(123)
204+
d <- data.frame(
205+
x = rnorm(10000),
206+
y = rnorm(10000),
207+
z = rnorm(10000)
208+
)
209+
r <- correlation::correlation(d)
210+
211+
expect_snapshot(summary(r), variant = "windows")
212+
expect_snapshot(summary(r, zap_small = FALSE), variant = "windows")
213+
})

tests/testthat/test-display_print_dataframe.R

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
test_that("display and print method works - markdown", {
22
skip_on_cran()
3-
skip_if(getRversion() < "4.0.0")
4-
skip_if_not_or_load_if_installed("gt")
5-
6-
expect_snapshot(print(correlation(iris), format = "markdown"))
3+
skip_if_not_or_load_if_installed("knitr")
74
expect_snapshot(display(correlation(iris)))
5+
expect_snapshot(print_md(correlation(iris)))
86
})
97

108
# display and print method works - HTML -----------------------------

0 commit comments

Comments
 (0)