R/wtd_rank.R

Defines functions wtd_rank

Documented in wtd_rank

#' Implement weighted rank correlation, as described in Jerrold H Zar, 
#' "Biostatistical Analysis" 5th ed. 
#' 
#' @param mat a matrix of data
#' @return the estimated correlation coefficients
#' @importFrom stats cor
#' @export
wtd_rank = function(mat) {
  ranks = apply(mat, 2, rank, ties = "average")
  # weight the ranks 
  # calculate the savage scores 
  n = nrow(mat)
  reciprocals = 1 / seq_len(n)
  savage = sapply(seq_len(n), function(i) sum(reciprocals[i:n]))
  # replace each rank with the savage score 
  savages = ranks
  savages[] = savage[ranks]
  # calculate pearson correlation
  cor = cor(savages, method = "p")
  return(cor)
}
fosterlab/CFTK documentation built on Jan. 19, 2021, 10:31 p.m.