R Functions : AUC and KS

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

Parameters
  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){
  require("ROCR")
  p   <- prediction(as.numeric(pred),depvar)
  auc <- performance(p,"auc")
  auc <- unlist(slot(auc, "y.values"))
  return(auc)
}
KS <- function(pred,depvar){
  require("ROCR")
  p   <- prediction(as.numeric(pred),depvar)
  perf <- performance(p, "tpr", "fpr")
  ks <- max(attr(perf, "y.values")[[1]] - (attr(perf, "x.values")[[1]]))
  return(ks)
}
KS(rfPred[,2], dev[,1])
AUC(rfPred[,2], dev[,1])
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

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

Post Comment 0 Response to "R Functions : AUC and KS"
Next → ← Prev