R/AUCR.test_function.R

Defines functions AUCR.test

Documented in AUCR.test

AUCR.test <- function(x, y, conf.level = 0.95){

  if(is.numeric(x) != T)
  {stop("x must be numeric.")}
  if(is.numeric(y) != T)
  {stop("y must be numeric.")}
  if(length(x) == 0)
  {stop("Not enough samples for x, minimal 1.")}
  if(length(y) == 0)
  {stop("Not enough samples for y, minimal 1.")}
  if(conf.level > 1 || conf.level < 0 || is.numeric(conf.level) != T)
  {stop("Argument conf.level needs to be a numeric value inbetween 0 and 1.")}

  df <- data.frame(x=x, y=y)
  df <- na.omit(df)
  df <- df[order(df$x),]

  AUCRvec <- NULL
  for(i in 1:length(df$y)){

    if(i == 1){AUCRvec <- c(AUCRvec, suppressWarnings(wilcox.test(df$y[2:length(df$y)], df$y[1:1])[[1]]/((length(df$y)-1)*1)))}
    else if(i == length(df$y)){AUCRvec <- c(AUCRvec, suppressWarnings(wilcox.test(df$y[i], df$y[1:(length(df$y)-1)])[[1]]/(1*(length(df$y)-1))))}
    else{AUCRvec <- c(AUCRvec, suppressWarnings(wilcox.test(df$y[(i+1):length(df$y)], df$y[1:i])[[1]]/(length(df$y[(i+1):length(df$y)])*i)))}}

  A  <- mean(AUCRvec, na.rm = T)
  CI <- quantile(AUCRvec, c((1-conf.level)/2, (1-conf.level)/2+conf.level), na.rm = T)
  LC <- as.numeric(CI[1])
  HC <- as.numeric(CI[2])

  res <- list(AUC.stat.estimate = A, Low.conf.AUC = LC, High.conf.AUC = HC,
              x.obs = length(x), y.obs = length(y),
              x = df$x, y = df$y, AUC.stat.gradient = as.numeric(AUCRvec))

  text <- paste(paste0("The area under the curve = ", round(A,2)), "\n",
                "At a confidence level of", paste0(conf.level*100,"%"), "\n",
                paste0("the lower confidence interval = ", round(LC,2)), "and", "\n",
                paste0("the higher confidence interval = ", round(HC,2)), "\n")
  cat(text)
  invisible(res)}
snwikaij/GRASS documentation built on July 29, 2020, 1:54 p.m.