Code snippet to calculate CI for Spearman’s rho with Fieller or Bonett-Wright correction. These corrections are reccomended for correlations with absolute value below 0.8. Bonett and Wright (2000) claim their correction performs better though the Bishara and Hittner paper favours the Fieller correction (and both are generally very similar).
See: https://link.springer.com/article/10.3758/s13428-016-0702-8
rho.ci <- function(rs, N, conf.level = 0.95, correct='fieller') {
if(correct=='none') zrs.se <- 1/(N - 3)^0.5
if(correct=='fieller') zrs.se <- (1.06/(N - 3))^0.5
if(correct=='bw') zrs.se <- ((1 + (rs^2)/2)/(N - 3))^0.5
moe <- qnorm(1 - (1 - conf.level)/2) * zrs.se
zu <- atanh(rs) + moe
zl <- atanh(rs) - moe
tanh(c(zl, zu))
}
Rankit version (from raw data) using the base cor.test() function:
spearman.test <- function(x, y, conf.level = 0.95) {
RIN <- function(x){qnorm((rank(x) - 0.5)/(length(rank(x))))}
x_rin <- RIN(x)
y_rin <- RIN(y)
list(cor.test(x,y, method='spearman'),
'RIN corrected CI'= cor.test(x_rin,y_rin)$conf.int[1:2]
)
}
The following function based on the Fieller et al. (1957) correction is advised for tau. This is apparently reasonable for n > 10 and absolute correlations below 0.8.
tau.ci <- function(tau, N, conf.level = 0.95, correct='fieller') {
if(correct=='none') tau.se <- 1/(N - 3)^0.5
if(correct=='fieller') tau.se <- (0.437/(N - 4))^0.5
moe <- qnorm(1 - (1 - conf.level)/2) * tau.se
zu <- atanh(tau) + moe
zl <- atanh(tau) - moe
tanh(c(zl, zu))
}