R/getCIPvals.R

Defines functions getCIPvals

#' Exact computation of Concordance Index p-values
#' 
#' This function takes as input a previously calculated null distribution (CDF) and a count
#' of the number of discordant pairs from a concordance index calculation and returns a p-value.
#'
#' @param nullCICDF {numeric} A CDF on the number of inversions from a null CI distribution with
#' the pertinent length and tie structure. Generated by the function nullCIDist.R
#' @param discpairs {numeric} An integer, the number of discordant pairs observed, the test statistic
#' for which to compute a p-value.
#' @param alternative {character} What is the alternative hypothesis? Must be one of "two.sided", "less", 
#' and "greater".
#' @return {numeric} The p-value under the distribution, i.e. the fraction of permutations with the 
#' specified length and tie structure with a more extreme number of discordant pairs in the indicated 
#' alternative.
#' @export

getCIPvals <- function(nullCICDF, discpairs, alternative=c("two.sided", "greater", "less")){
  if (nullCICDF[length(nullCICDF)] < 0.99) {
    return("Error: nullCICDF is not a CDF.  Run nullCIDist with argument cumulative=1")
  } else {
    alternative <- match.arg(alternative)
    ix_twosided <- min(discpairs, length(nullCICDF) - 1 - discpairs)
    
    switch (alternative, 
            "two.sided" = ifelse(ix_twosided == (length(nullCICDF)-1)/2, 
                                 1, 
                                 2*nullCICDF[ix_twosided+1]),
            "greater" = 1 - nullCICDF[discpairs+1],
            "less" = nullCICDF[discpairs+1]
    )
  }
}
bhklab/wCI documentation built on Jan. 26, 2024, 5:36 p.m.