R Functions : AUC and KS

If ROCR package is not installed on your machine, please install it before running the following functions.

  1.  pred - Predicted probability column of event (interesting class)
  2. depvar - Dependent / Target / Outcome Variable
Training Dataset Prediction Column
rfPred <- predict(tuned, type ="prob")
Validation Dataset Prediction Column
rfPred <- predict(tuned, val, type ="prob")
R Functions for AUC and KS Statistics

AUC <- function(pred,depvar){
  p   <- prediction(as.numeric(pred),depvar)
  auc <- performance(p,"auc")
  auc <- unlist(slot(auc, "y.values"))
KS <- function(pred,depvar){
  p   <- prediction(as.numeric(pred),depvar)
  perf <- performance(p, "tpr", "fpr")
  ks <- max(attr(perf, "y.values")[[1]] - (attr(perf, "x.values")[[1]]))
KS(rfPred[,2], dev[,1])
AUC(rfPred[,2], dev[,1])
Related Posts
About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 8 years of experience in data science. During his tenure, he has worked with global clients in various domains like Banking, Insurance, Telecom and Human Resource.

0 Response to "R Functions : AUC and KS"

Post a comment

Next → ← Prev
Love this Post? Spread the Word!