#' Calculate Matthews correlation coefficient (MCC), F1, etc
#'
#' @param tmpcm confusion matrix that follows the default format returned by `caret::confusionMatrix`
#'
#' @return matthews correlation coef value
#' @export
#'
#' @examples
#' cm = matrix(c(20,5,
#' 10,15),
#' nrow = 2,
#' ncol = 2,
#' byrow = TRUE,
#' dimnames = list(c("pos", "neg"), c("pos", "neg")))
#' get_mcc(cm) # 0.4082483
#' get_f1(cm) # 0.7272727
#' get_cohens(cm) # 0.4
get_mcc = function(tmpcm){
# confusion matrix where the,
# ... rows indicate the prediction, and columns indicate the reference
# ... first element of each row or column represents the "positive" class
# ... second element of each row or column represents the "negative" class
# tmpcm = [ TP, FP ]
# [ FN, TN ]
TP = tmpcm[1,1]
TN = tmpcm[2,2]
FP = tmpcm[1,2]
FN = tmpcm[2,1]
numer = (TP * TN) - (FP * FN)
#denom = (TP+FN) * (TP+FP) * (TN+FN) * (TN+FP)
logdenom = log(TP+FN) + log(TP+FP) + log(TN+FN) + log(TN+FP)
return( numer / exp(0.5 * logdenom) )
#return( numer / sqrt(denom) )
}
#' @describeIn get_mcc calculate f1 score from confusion matrix
#' @export
get_f1 = function(tmpcm){
TP = tmpcm[1,1]
TN = tmpcm[2,2]
FP = tmpcm[1,2]
FN = tmpcm[2,1]
(2*TP) / ( (2*TP) + FP + FN )
}
#' @describeIn get_mcc calculate Cohen's kappa coefficient from a confusion matrix
#' @export
get_cohens = function(tmpcm){
# From [wikipedia](https://en.wikipedia.org/wiki/Cohen%27s_kappa):
TP = tmpcm[1,1]
TN = tmpcm[2,2]
FP = tmpcm[1,2]
FN = tmpcm[2,1]
numer = 2 * (TP * TN - FP * FN)
logdenom1 = log(TP+FP) + log(FP+TN)
logdenom2 = log(TP+FN) + log(FN+TN)
return( numer / (exp(logdenom1) + exp(logdenom2)) )
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.