Created
October 8, 2022 19:33
-
-
Save jmbarbone/b529190518410667b4cc5b12bc3bda48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spearman_ci <- function(x, y, alpha = 0.95, method = c("norm", "t")) { | |
# modified from https://stats.stackexchange.com/questions/18887/how-to-calculate-a-confidence-interval-for-spearmans-rank-correlation/506367#506367 | |
method <- mark::match_param(method) | |
ok <- stats::complete.cases(x, y) | |
x <- x[ok] | |
y <- y[ok] | |
r <- cor(x, y, method = "spearman") | |
n <- sum(ok) | |
q <- switch( | |
method, | |
norm = stats::qnorm(p = (1 - alpha) / 2), | |
t = stats::qt(p = (1 - alpha) / 2, df = n - 3) | |
) | |
ones <- c(-1L, 1L) | |
ci <- tanh(atanh(r) + ones * sqrt((1 + r^2/2) / (n - 3)) * q) | |
ci <- sort(ci) | |
attr(ci, "conf.level") <- alpha | |
attr(ci, "conf.method") <- method | |
ci | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment