R/modeling_functions.R

Defines functions mlokNormalize mlokPerform

Documented in mlokNormalize mlokPerform

#' @title mlokNormalize
#' @description A way to do quick minmax normalizations for table columns
#' @param x A vector of list of class numeric
#' @export

mlokNormalize <- function(x) {

  num <- x - min(x)
  denom <- max(x) - min(x)
  return(num/denom)

}

#' @title mlokPerform
#' @description Quick calculations for common performance measures
#' @param act A vector of actual values or classifications
#' @param pred A vector of predictions made for the corresponding act vector
#' @export

mlokPerform <- function(act, pred){

  rmse <- sqrt((1/length(pred))*
                 sum((pred-act)^2))
  mae <- (1/length(pred))*sum(abs(pred-act))

  conmat <- table(act, pred)
  tp <- round(conmat[4])
  tn <- round(conmat[1])
  fp <- round(conmat[3])
  fn <- round(conmat[2])

  acc <- round((tp+tn)/sum(conmat), 4)
  err <- round(1 - acc, 4)
  tpr <- round(tp/(tp+fn), 4)
  tnr <- round(tn/(tn+fp), 4)
  ppv <- round(tp/(tp+fp), 4)
  npv <- round(tn/(tn+fn), 4)

  results <- list('Confusion Matrix' =
                    conmat,
                  'Error Rates' =
                    c('RMSE' = rmse,  'MAE' = mae, 'Error' = err),
                  'Performance' =
                    c('Accuracy' = acc,
                      'Sensitivity' = tpr,
                      'Specificity' = tnr,
                      'Positive Predictive Value' = ppv,
                      'Negative Predictive Value' = npv))

  return(results)
}
michael-lok/mlokFunctions documentation built on April 8, 2020, 9:34 p.m.