R Functions : AUC and KS

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])

R Tutorials : 75 Free R Tutorials

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

0 Response to "R Functions : AUC and KS"

Post a Comment

Next → ← Prev