R/assessTwoBooleanColumnsConcordanceFor.TP.TN.FP.FN.Sens.Spec.FPR.FNR.R

Defines functions assessTwoBooleanColumnsConcordanceFor.TP.TN.FP.FN.Sens.Spec.FPR.

Documented in assessTwoBooleanColumnsConcordanceFor.TP.TN.FP.FN.Sens.Spec.FPR.

#' Assess two boolean columns to calculate true positives, true negatives, false positives, false negatives, sensitivity, specificity, false positive rate, false negative rate
#' 
#' This function takes data frame where first columns is a boolean indicating TRUE cases while second column indicating TRUE cases according to assay specifications.
#' 
#' @param df Data frame where df[,1] is boolean vector of TRUE cases (i.e. TRUE represents equivalnce between the samples cases and column category), and df[,2] is boolean vector of what is TRUE according to given threshold criteria of an assay (i.e. TRUE respresent positive according to an assay)
#' @return A list showing TP, TN, FP, FN, Sens, Spec, FPR, FNR
#' @export
assessTwoBooleanColumnsConcordanceFor.TP.TN.FP.FN.Sens.Spec.FPR.FNR <- function(df){
  # make sure df is of two columns
  # make sure df[,1] is boolean vector of TRUE cases (i.e. TRUE represents equivalnce between the samples cases and column category)
  # make sure df[,2] is boolean vector of what is TRUE according to given threshold criteria of an assay (i.e. TRUE respresent positive according to an assay)
  TP = 0
  TN = 0
  FP = 0
  FN = 0
  concordance <- df[,1] == df[,2]
  df <- cbind(df, concordance)
  for(i in 1:nrow(df)){
    if(df[i,3]==TRUE){
      if(df[i,1]==TRUE){
        TP <- TP + 1
        #print("TruePos")
      }
      else{
        TN <- TN + 1
        #print("TrueNeg")
      }
    }
    if(df[i,3]==FALSE){
      if(df[i,1]==FALSE){
        FP <- FP + 1
        #print("FalsePos")
      }
      else{
        FN <- FN + 1
        #print("FalseNeg")
      }
    }
  }
  return(list(TP=TP,
              TN=TN,
              FP=FP,
              FN=FN,
              Sensitivity=CalcSensitivity(TP = TP, FN = FN), 
              Specificity=CalcSpecificity(TN = TN, FP = FP), 
              FalsePosRate=CalcFalsePositiveRate(FP = FP, TN = TN), 
              FalseNegRate=CalcFalseNegativeRate(FN = FN, TP = TP)))
}
msxakk89/dat documentation built on Aug. 3, 2020, 6:39 p.m.