Good evening R experts,
I am generating a Spearman's correlation matrix in R using all variables under study for my current project. To be consistent with the other analyses I am conducting, I would like to construct BCa bootstrapped confidence intervals using 10,000 iterations around each Spearman's coefficient. However, I have not found an efficient and reliable method by which to achieve this end.
I have created my Spearman's correlation matrix with coefficients and p-values using the following code. Note that I have generated random data for the sake of this example.
library(psych)
library(rstatix)
df_test <- data.frame(
recall=c(45, 1, 32, 17, 79, 15, 75, 100, 43, 80, 74, 91, 60, 54, 67, 26, 97, 53, 51, 30),
recog=c(90, 29, 93, 73, 34, 68, 78, 56, 92, 85, 35, 81, 7, 58, 4, 52, 82, 31, 6, 23),
hits=c(77, 89, 44, 8, 70, 96, 76, 62, 95, 27, 49, 12, 16, 28, 83, 2, 36, 10, 61, 86),
misses=c(59, 78, 14, 44, 86, 61, 80, 72, 25, 93, 5, 42, 64, 95, 73, 54, 7, 67, 11, 53),
false_alarms=c(32, 34, 55, 41, 77, 76, 89, 36, 12, 100, 70, 62, 47, 81, 90, 63, 13, 83, 79, 38)
)
df_test.cor <- cor(df_test, y = NULL, use = "pairwise.complete.obs",
method = c("spearman"))
#Get coefficients
df_test.mat <- cor_mat(
df_test,
vars = NULL,
method = "spearman",
alternative = "two.sided",
conf.level = 0.95
)
df_test.mat
#Get sig values
df_test.pmat <- cor_pmat(
df_test,
vars = NULL,
method = "spearman",
alternative = "two.sided",
conf.level = 0.95
)
df_test.pmat
ggcorrplot(df_test.mat,
type = "lower", method = "circle", colors = c("turquoise3", "khaki1", "violetred3"), p.mat = df_test.pmat)
cor.ci(df_test, keys = NULL, n.iter = 10000, p = 0.050, poly = FALSE, method = "spearman")
The results are as follows:
1. Correlation coefficients with p-values:

2. Visualization
3. Bootstrapped 95% CI's Using psych Package

There are a number of issues with these confidence intervals, enumerated below:
- First, the p-values from the CI calculation do not correspond to those generated using the
cor_pmat()command from therstatixpackage. - The CI approach used here does not allow me to select BCa as the bootstrapping method.
- The p-values and CI boundaries are only reported up to 2 decimal places using the aforementioned approach. I require greater precision for my project, ideally with p-values and CI boundaries calculated up to 3 or 4 decimals.
Hence, my question is: How might I construct bootstrapped confidence intervals around Spearman's coefficients in my entire correlation matrix using the BCa approach with precision up to 4 decimal places?

You can calculate BCa intervals with the boot package.
First, create a statistic function for the Spearman correlations for a subset
iof the data:Then, get bootstrap samples with
boot():Finally, calculate confidence intervals with
boot.ci().indexspecifies which element of the output statistic you want the intervals for:A vector
indexhas a different meaning (the 2nd element is interpreted as giving the variance of the 1st), so to get all CIs you’ll need to explicitly iterate, for example withlapply():If you want only the intervals from the output, you can extract them from the
$bcaelement: