Skip to contents

This function computes pairwise correlations for all variables in a given dataset, supporting both frequentist and Bayesian approaches. It provides multiple correlation estimates, confidence intervals, Bayesian credible intervals, probability of direction (pd), and Bayes factors.

Usage

cor_test_all(
  dat,
  cor = TRUE,
  cor_EAP = FALSE,
  cor_MAP = FALSE,
  cor_MED = FALSE,
  pd = FALSE,
  bf = FALSE,
  ci = c("freq", "bayes_central", "bayes_hdi"),
  triangle = c("upper", "lower", "full"),
  alternative = c("two.sided", "less", "greater"),
  method = c("pearson", "kendall", "spearman"),
  exact = NULL,
  conf.level = 0.95,
  continuity = FALSE,
  rscale_est = "ultrawide",
  rscale_bf = "ultrawide",
  iterations = 10000,
  map_density_n = 512,
  ...
)

Arguments

dat

A data frame or matrix containing numeric variables for correlation analysis.

cor

Logical. If TRUE, computes frequentist correlation coefficients (default: TRUE).

cor_EAP

Logical. If TRUE, computes the expected a posteriori (EAP) estimate of the correlation coefficient (default: FALSE).

cor_MAP

Logical. If TRUE, computes the maximum a posteriori (MAP) estimate of the correlation coefficient (default: FALSE).

cor_MED

Logical. If TRUE, computes the median of the posterior distribution (MED) for the correlation coefficient (default: FALSE).

pd

Logical. If TRUE, computes the probability of direction (pd) based on posterior distributions (default: FALSE).

bf

Logical. If TRUE, computes Bayes factors for the presence of correlation versus the null hypothesis (default: FALSE).

ci

Character. Specifies the type of confidence or credible interval: "freq" (frequentist confidence interval), "bayes_central" (Bayesian central credible interval), or "bayes_hdi" (highest density interval based on the posterior distribution) (default: "freq").

triangle

Character. Specifies which part of the correlation matrix to return: "upper", "lower", or "full" (default: "upper").

alternative

Character. Specifies the alternative hypothesis for the frequentist test: "two.sided", "less", or "greater" (default: "two.sided").

method

Character. Specifies the correlation method for the frequentist test: "pearson", "kendall", or "spearman" (default: "pearson").

exact

Logical or NULL. If TRUE, computes exact p-values for small sample sizes when using Spearman or Kendall correlations.

conf.level

Numeric. The confidence level for frequentist intervals or credibility level for Bayesian intervals (default: 0.95).

continuity

Logical. If TRUE, applies a continuity correction for Kendall correlations (default: FALSE).

rscale_est

Numeric or character. Specifies the Cauchy prior scale for Bayesian estimation of the posterior distribution. Options: "ultrawide", "wide", "medium", or a positive real number (default: "ultrawide"). Passed to BayesFactor::correlationBF().

rscale_bf

Numeric or character. Specifies the Cauchy prior scale for Bayes factor calculation. Options: "ultrawide", "wide", "medium", or a positive real number (default: "ultrawide"). Passed to BayesFactor::correlationBF().

iterations

Integer. Number of MCMC samples for Bayesian estimation (default: 10000).

map_density_n

Integer. Number of bins for MAP density estimation (default: 512).

...

Additional arguments passed to cor.test().

Value

A list containing:

all

A data frame with all computed correlation statistics.

table_XX

A data frame corresponding to a table named "table_XX", where "XX" is derived from the output variables (e.g., "table_cor", "table_p", "table_BF10"). The content of the table depends on the provided inputs.

Examples

# Frequentist analysis
results <- cor_test_all(mtcars[, 1:5])
results$all  # View detailed results in a tidy format
#>    var_row var_col row col        cor      lower      upper         t df
#> 1      mpg     cyl   1   2 -0.8521620 -0.9257694 -0.7163171 -8.919699 30
#> 2      mpg    disp   1   3 -0.8475514 -0.9233594 -0.7081376 -8.747152 30
#> 3      mpg      hp   1   4 -0.7761684 -0.8852686 -0.5860994 -6.742389 30
#> 4      mpg    drat   1   5  0.6811719  0.4360484  0.8322010  5.096042 30
#> 5      cyl    disp   2   3  0.9020329  0.8072442  0.9514607 11.445474 30
#> 6      cyl      hp   2   4  0.8324475  0.6816016  0.9154223  8.228604 30
#> 7      cyl    drat   2   5 -0.6999381 -0.8429083 -0.4646481 -5.367824 30
#> 8     disp      hp   3   4  0.7909486  0.6106794  0.8932775  7.080122 30
#> 9     disp    drat   3   5 -0.7102139 -0.8487237 -0.4805193 -5.525681 30
#> 10      hp    drat   4   5 -0.4487591 -0.6895522 -0.1186280 -2.750459 30
#>               p n_pair n_na
#> 1  6.112687e-10     32    0
#> 2  9.380327e-10     32    0
#> 3  1.787835e-07     32    0
#> 4  1.776240e-05     32    0
#> 5  1.802838e-12     32    0
#> 6  3.477861e-09     32    0
#> 7  8.244636e-06     32    0
#> 8  7.142679e-08     32    0
#> 9  5.282022e-06     32    0
#> 10 9.988772e-03     32    0
results$table_cor  # View correlation matrix
#>      mpg       cyl       disp         hp       drat
#> mpg   NA -0.852162 -0.8475514 -0.7761684  0.6811719
#> cyl   NA        NA  0.9020329  0.8324475 -0.6999381
#> disp  NA        NA         NA  0.7909486 -0.7102139
#> hp    NA        NA         NA         NA -0.4487591
#> drat  NA        NA         NA         NA         NA

# Calculation of Bayesian statistics
results <- cor_test_all(mtcars[, 1:5], cor_MAP = TRUE, ci = "bayes_hdi",
                        bf = TRUE, pd = TRUE, rscale_est = "ultrawide",
                        rscale_bf = "ultrawide")
results$all  # View detailed results in a tidy format
#>    var_row var_col row col        cor    cor_MAP      lower      upper
#> 1      mpg     cyl   1   2 -0.8521620 -0.8551780 -0.9291335 -0.7104358
#> 2      mpg    disp   1   3 -0.8475514 -0.8469345 -0.9182262 -0.6980145
#> 3      mpg      hp   1   4 -0.7761684 -0.7759596 -0.8863940 -0.5718015
#> 4      mpg    drat   1   5  0.6811719  0.6599987  0.4151486  0.8231249
#> 5      cyl    disp   2   3  0.9020329  0.9014094  0.7978578  0.9550496
#> 6      cyl      hp   2   4  0.8324475  0.8257042  0.6623636  0.9077389
#> 7      cyl    drat   2   5 -0.6999381 -0.6981593 -0.8352905 -0.4527759
#> 8     disp      hp   3   4  0.7909486  0.7769767  0.6043145  0.8978313
#> 9     disp    drat   3   5 -0.7102139 -0.6958604 -0.8435625 -0.4710733
#> 10      hp    drat   4   5 -0.4487591 -0.4288617 -0.6733380 -0.1233123
#>            t df            p     pd         BF10 n_pair n_na
#> 1  -8.919699 30 6.112687e-10 1.0000 1.832837e+07     32    0
#> 2  -8.747152 30 9.380327e-10 1.0000 1.233151e+07     32    0
#> 3  -6.742389 30 1.787835e-07 1.0000 9.718143e+04     32    0
#> 4   5.096042 30 1.776240e-05 1.0000 1.451650e+03     32    0
#> 5  11.445474 30 1.802838e-12 1.0000 4.069716e+09     32    0
#> 6   8.228604 30 3.477861e-09 1.0000 3.671160e+06     32    0
#> 7  -5.367824 30 8.244636e-06 1.0000 2.916590e+03     32    0
#> 8   7.080122 30 7.142679e-08 1.0000 2.259954e+05     32    0
#> 9  -5.525681 30 5.282022e-06 1.0000 4.375586e+03     32    0
#> 10 -2.750459 30 9.988772e-03 0.9942 5.262622e+00     32    0
results$table_BF10  # View Bayes factor matrix
#>      mpg      cyl       disp         hp        drat
#> mpg   NA 18328365   12331514   97181.43 1451.650039
#> cyl   NA       NA 4069716342 3671159.87 2916.590374
#> disp  NA       NA         NA  225995.44 4375.586326
#> hp    NA       NA         NA         NA    5.262622
#> drat  NA       NA         NA         NA          NA