R/ROCPlot.R

Defines functions plotroc plotproper proproc

Documented in plotproper plotroc proproc

#---------------------------------#
#            plotroc              #
#          Plot ROC curves        #
# (no more than 3 curves allowed) #
#---------------------------------#
#
#' @title Plot One or More ROC Curves from the Corresponding ROC Tables (S3 Objects)
#'
#' @description Plot up to three empirical ROC curves from the corresponding
#' ROC tables. A ROC table is an S3 object derived from an R matrix, generated
#' by the roctable() function
#'
#' @param roctable1 A ROC Table (S3 object)
#' @param roctable2 A ROC Table (S3 object)
#' @param roctable3 A ROC Table (S3 object)
#'
#' @return NULL
#'
#' @importFrom graphics abline plot plot.new
#'
#' @export
#'
#' @examples
#' ROCGene1 <- roctable(OvarianData$Gene1, OvarianData$Status)
#' ROCGene2 <- roctable(OvarianData$Gene2, OvarianData$Status)
#' plotroc(ROCGene1, ROCGene2)
#'
plotroc <- function(roctable1, roctable2 = NULL, roctable3 = NULL) {

  plot.new()
  sens1 <- roctable1[,1]
  onespec1 <- roctable1[,2]

  if (!is.null(roctable2)) {
    sens2 <- roctable2[,1]
    onespec2 <- roctable2[,2]
  }

  if (!is.null(roctable3)) {
    sens3 <- roctable3[,1]
    onespec3 <- roctable3[,2]
  }

  plot(onespec1, sens1, type = "o", col = "red",
       ylab = "Sensitivity", xlab = "1-Specificity")
  abline(0,1, lty = 2)

  if (!is.null(roctable2)) {
    lines(onespec2, sens2, type = "o", col = "blue")
  }

  if (!is.null(roctable3)) {
    lines(onespec3, sens3, type = "o", col = "green")
  }

  return(NULL)

}


#----------------------#
#      plotproper      #
#   Add a theoretical  #
# proper ROC to a plot #
#----------------------#
#
#' @title Add a Theoretical Proper ROC Curve to a ROC Plot
#'
#' @description Add a theoretical proper ROC curve to a pre-existing ROC plot
#' generated by plotroc()
#'
#' @param dor The Diagnostic Odds Ratio of the proper curve
#' @param color The color of the theoretical proper ROC curve
#'
#' @return NULL
#'
#' @importFrom graphics lines
#'
#' @export
#'
#' @examples
#' ROCGene2 <- roctable(OvarianData$Gene2, OvarianData$Status)
#' plotroc(ROCGene2)
#' plotproper(rocdor(rocauc(ROCGene2)))
#'
plotproper <- function(dor, color = "black") {

  datamatr <- proproc(dor)
  onespec <- datamatr[,1]
  sens <- datamatr[,2]
  lines(onespec, sens, col = color)

  return(NULL)

}


#-------------------------------------#
#                proproc              #
#        Proper ROC coordinates       #
# (make a theoretical curve from DOR) #
#-------------------------------------#
#
#' @title Calculate the Coordinates of a Proper ROC Curve (internal usage)
#'
#' @description Calculate the coordinates of a proper ROC curve
#' from the corresponding Diagnostic Odds Ratio (DOR). Called by
#' plotproper(), internal usage only
#'
#' @param dor The Diagnostic Odds Ratio of the proper curve
#' @param npoints Number of points to be plotted
#'
#' @return NULL
proproc <- function(dor, npoints = 2001L) {

  if (dor < 0.5) {
    print("ROC curve is non proper")
    return -1
  }

  x <- c(1:npoints)      # 1 - Specificity
  x <- (x-1)/npoints
  x[npoints] = 1.0
  y <- dor*x/(dor*x+1-x) # Sensitivity

  datamatr <- matrix(c(x,y), nrow = npoints, ncol = 2)
  return(datamatr)

}
parodistefano/properROC documentation built on May 24, 2019, 6:16 p.m.