Perform Correlation Tests with Frequentist and Bayesian Methods
Source:R/cor_test_all.r
cor_test_all.RdThis 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,
alpha = 0.05,
continuity = FALSE,
rscale_est = "ultrawide",
rscale_bf = "ultrawide",
iterations = 10000,
map_density_n = 512,
detailed = FALSE,
fullbayes = FALSE
)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. IfTRUE, 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).- alpha
Numeric. Significance level. Defaults to 0.05.
- 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 toBayesFactor::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 toBayesFactor::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).- detailed
Logical. Whether to return detailed results (
TRUE) or minimal output (FALSE, default).- fullbayes
Logical. Whether to show only Bayesian results (
TRUE) or both frequentist and Bayesian results (FALSE, default).
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 p sig n_pair
#> 1 mpg cyl 1 2 -0.852 -0.926 -0.716 -8.92 30 0.00 * 32
#> 2 mpg disp 1 3 -0.848 -0.923 -0.708 -8.75 30 0.00 * 32
#> 3 mpg hp 1 4 -0.776 -0.885 -0.586 -6.74 30 0.00 * 32
#> 4 mpg drat 1 5 0.681 0.436 0.832 5.10 30 0.00 * 32
#> 5 cyl disp 2 3 0.902 0.807 0.951 11.45 30 0.00 * 32
#> 6 cyl hp 2 4 0.832 0.682 0.915 8.23 30 0.00 * 32
#> 7 cyl drat 2 5 -0.700 -0.843 -0.465 -5.37 30 0.00 * 32
#> 8 disp hp 3 4 0.791 0.611 0.893 7.08 30 0.00 * 32
#> 9 disp drat 3 5 -0.710 -0.849 -0.481 -5.53 30 0.00 * 32
#> 10 hp drat 4 5 -0.449 -0.690 -0.119 -2.75 30 0.01 * 32
results$table_cor # View correlation matrix
#> mpg cyl disp hp drat
#> mpg NA -0.852 -0.848 -0.776 0.681
#> cyl NA NA 0.902 0.832 -0.700
#> disp NA NA NA 0.791 -0.710
#> hp NA NA NA NA -0.449
#> 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 t df p sig pd
#> 1 mpg cyl 1 2 -0.852 -0.855 -0.930 -0.710 -8.92 30 0.00 * 1.000
#> 2 mpg disp 1 3 -0.848 -0.847 -0.918 -0.699 -8.75 30 0.00 * 1.000
#> 3 mpg hp 1 4 -0.776 -0.776 -0.894 -0.576 -6.74 30 0.00 * 1.000
#> 4 mpg drat 1 5 0.681 0.660 0.415 0.824 5.10 30 0.00 * 1.000
#> 5 cyl disp 2 3 0.902 0.901 0.797 0.955 11.45 30 0.00 * 1.000
#> 6 cyl hp 2 4 0.832 0.826 0.662 0.907 8.23 30 0.00 * 1.000
#> 7 cyl drat 2 5 -0.700 -0.700 -0.836 -0.453 -5.37 30 0.00 * 1.000
#> 8 disp hp 3 4 0.791 0.779 0.604 0.898 7.08 30 0.00 * 1.000
#> 9 disp drat 3 5 -0.710 -0.697 -0.845 -0.471 -5.53 30 0.00 * 1.000
#> 10 hp drat 4 5 -0.449 -0.422 -0.675 -0.123 -2.75 30 0.01 * 0.994
#> BF10 log10_BF10 favor evidence n_pair
#> 1 1.832837e+07 7.2631237 alt. extreme 32
#> 2 1.233151e+07 7.0910164 alt. extreme 32
#> 3 9.718143e+04 4.9875833 alt. extreme 32
#> 4 1.451650e+03 3.1618619 alt. extreme 32
#> 5 4.069716e+09 9.6095641 alt. extreme 32
#> 6 3.671160e+06 6.5648033 alt. extreme 32
#> 7 2.916590e+03 3.4648754 alt. extreme 32
#> 8 2.259954e+05 5.3540997 alt. extreme 32
#> 9 4.375586e+03 3.6410363 alt. extreme 32
#> 10 5.263000e+00 0.7212022 alt. moderate 32
results$table_BF10 # View Bayes factor matrix
#> mpg cyl disp hp drat
#> mpg NA 18328365 12331514 97181.43 1451.650
#> cyl NA NA 4069716342 3671159.87 2916.590
#> disp NA NA NA 225995.44 4375.586
#> hp NA NA NA NA 5.263
#> drat NA NA NA NA NA