This function is a wrapper around stats::cor.test(). It implements the Pearson's correlation test that tests the null hypothesis that two or more paired samples of values are unrelated. This function can be applied to two or more numeric variables in the provided data.

cor_test_multi(
  .data,
  ...,
  .pvalues = FALSE,
  .ci = FALSE,
  .as_matrix = TRUE,
  .omit_redundancies = FALSE,
  .method = "pearson"
)

Arguments

.data

A data frame.

...

Variables for which the correlation coefficient should be returned. If no variable name is provided, correlations will be returned for all numeric variables in .data.

.pvalues

logical If FALSE (default), p-values will be omitted from the output. If TRUE, p-values will be included in the output.

.ci

logical If FALSE (default), 95% confidence interval bounds will be omitted from the output. If TRUE, 95% confidence interval bounds will be included in the output.

.as_matrix

logical If TRUE (default), results will be return as matrix. If TRUE, results will be returned as tibble.

.omit_redundancies

logical If FALSE (default), all n^2 correlations will be include in the output. If TRUE, only unique correlations will be returned (x ~ y but not y ~ x) and correlation of a variable with itself will be omitted.

.method

A character string indicating which correlation coefficient is to be used: "pearson", "kendall", or "spearman". Default method is "pearson".

Value

By default a matrix with correlation coefficients. Output format and included statistics can be changed in the argument settings.

Examples

# Calculate the correlations between all numeric variables in the `faithfulfaces` data.
cor_test_multi(faithfulfaces)
#> $cor
#>             sex_dimorph attractive trustworthy   faithful
#> sex_dimorph   1.0000000  0.6042674  -0.0683907 -0.5988680
#> attractive    0.6042674  1.0000000   0.3643517 -0.3004394
#> trustworthy  -0.0683907  0.3643517   1.0000000  0.4851078
#> faithful     -0.5988680 -0.3004394   0.4851078  1.0000000
#> 
# Calculate the correlations between the 1st, 2nd and 4th variable.
cor_test_multi(faithfulfaces, c(1,2,4))
#> $cor
#>             sex_dimorph attractive trustworthy
#> sex_dimorph   1.0000000  0.6042674  -0.0683907
#> attractive    0.6042674  1.0000000   0.3643517
#> trustworthy  -0.0683907  0.3643517   1.0000000
#> 
# Calculate the correlations between `sex_dimorph`, `attractive`, and `trustworthy`.
cor_test_multi(faithfulfaces, sex_dimorph, attractive, trustworthy)
#> $cor
#>             sex_dimorph attractive trustworthy
#> sex_dimorph   1.0000000  0.6042674  -0.0683907
#> attractive    0.6042674  1.0000000   0.3643517
#> trustworthy  -0.0683907  0.3643517   1.0000000
#> 
# Calculate all correlations and return p-values and 95% confidence intervals.
cor_test_multi(faithfulfaces, .pvalues = TRUE, .ci = TRUE)
#> $cor
#>             sex_dimorph attractive trustworthy   faithful
#> sex_dimorph   1.0000000  0.6042674  -0.0683907 -0.5988680
#> attractive    0.6042674  1.0000000   0.3643517 -0.3004394
#> trustworthy  -0.0683907  0.3643517   1.0000000  0.4851078
#> faithful     -0.5988680 -0.3004394   0.4851078  1.0000000
#> 
#> $p_value
#>               sex_dimorph    attractive   trustworthy      faithful
#> sex_dimorph 2.225074e-308  2.679625e-18  3.755287e-01  6.356909e-18
#> attractive   2.679625e-18 2.225074e-308  1.037068e-06  6.870986e-05
#> trustworthy  3.755287e-01  1.037068e-06 2.225074e-308  2.026274e-11
#> faithful     6.356909e-18  6.870986e-05  2.026274e-11 2.225074e-308
#> 
#> $lower
#>             sex_dimorph attractive trustworthy   faithful
#> sex_dimorph   1.0000000  0.4991514  -0.2166746 -0.6874195
#> attractive    0.4991514  1.0000000   0.2262447 -0.4314436
#> trustworthy  -0.2166746  0.2262447   1.0000000  0.3609485
#> faithful     -0.6874195 -0.4314436   0.3609485  1.0000000
#> 
#> $upper
#>             sex_dimorph attractive trustworthy   faithful
#> sex_dimorph   1.0000000  0.6918567   0.0829777 -0.4927712
#> attractive    0.6918567  1.0000000   0.4880986 -0.1570259
#> trustworthy   0.0829777  0.4880986   1.0000000  0.5923698
#> faithful     -0.4927712 -0.1570259   0.5923698  1.0000000
#> 
# Calculate all correlations with p-values and 95% confidence intervals and 
# return results as table with only unique pairs of the off-diagonal correlations.
cor_test_multi(faithfulfaces, .pvalues = TRUE, .ci = TRUE, .as_matrix = FALSE, 
.omit_redundancies = TRUE)
#> # A tibble: 6 × 6
#>   x           y               cor  lower   upper  p_value
#>   <chr>       <chr>         <dbl>  <dbl>   <dbl>    <dbl>
#> 1 sex_dimorph attractive   0.604   0.499  0.692  2.68e-18
#> 2 sex_dimorph trustworthy -0.0684 -0.217  0.0830 3.76e- 1
#> 3 sex_dimorph faithful    -0.599  -0.687 -0.493  6.36e-18
#> 4 attractive  trustworthy  0.364   0.226  0.488  1.04e- 6
#> 5 attractive  faithful    -0.300  -0.431 -0.157  6.87e- 5
#> 6 trustworthy faithful     0.485   0.361  0.592  2.03e-11