R/confusionMatrix_metrics.R

Defines functions confusionMatrix_metrics

Documented in confusionMatrix_metrics

confusionMatrix_metrics <-
function(cumSums,rankings=seq_along(cumSums),total=length(cumSums),seed=102){ #cumSums would be a numeric vector
    
    
    df=data.frame(
        TP=cumSums,
        FP=rankings-cumSums,
        TN=total-rankings-(max(cumSums)-cumSums),
        FN=max(cumSums)-cumSums,
        
        sensitivity=cumSums/max(cumSums),  # = TP/(TP+FN) #Note: this can also be viewed as a kind of coverage
        specificity = (total-rankings-(max(cumSums)-cumSums)) / (total-max(cumSums)), # = TN / (TN+FP)
        precision= cumSums / rankings, # = TP/(TP+FP)
        accuracy =(cumSums+total-rankings-max(cumSums)+cumSums)/total # = (TP + TN)/total     ##Equation looks complicated. Have to double check
        
    )
    
    randomCoAnnotation=rep(0,total); set.seed(seed); randomCoAnnotation[sample(total,max(cumSums))]=1
    randomCumSums=cumsum(randomCoAnnotation)
    
    random_df=data.frame(
        random_TP=randomCumSums,
        random_FP=rankings-randomCumSums,
        random_TN=total-rankings-(max(randomCumSums)-randomCumSums),
        random_FN=max(randomCumSums)-randomCumSums,
        
        random_sensitivity=randomCumSums/max(randomCumSums),  # = TP/(TP+FN)
        random_specificity = (total-rankings-(max(randomCumSums)-randomCumSums)) / (total-max(randomCumSums)), # = TN / (TN+FP)
        random_precision= randomCumSums / rankings, # = TP/(TP+FP)
        random_accuracy =(randomCumSums+total-rankings-max(randomCumSums)+randomCumSums)/total # = (TP + TN)/total     ##Equation looks complicated. Have to double check
        
    )
    
    
    return(cbind(df,random_df))
    
}
peterwu19881230/microbialPhenotypes documentation built on March 19, 2021, 6:36 a.m.