Skip to content

Instantly share code, notes, and snippets.

@sudevschiz
Created December 21, 2018 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sudevschiz/f5e6455df06bdb048bdedc268123e6b1 to your computer and use it in GitHub Desktop.
Save sudevschiz/f5e6455df06bdb048bdedc268123e6b1 to your computer and use it in GitHub Desktop.
#### Function to validate classification models####
accuracy_metrics <- function(y_actual, y_predicted) {
#Check if the input is factor
if(is.factor(y_actual)){
print("Info : Actual values provided as factor. Factor converted to numeric. ")
#Convert to numeric
y_actual <- as.numeric(y_actual) - 1
}
if(is.factor(y_predicted)){
print("Info : Predicted values provided as factor. Factor converted to numeric. ")
#Convert to numeric
y_predicted <- as.numeric(y_predicted) - 1
}
if(is.logical(y_actual)){
print("Info : Predicted values provided as Logical vector. Logical vector converted to numeric. ")
#Convert to numeric
y_actual<- as.numeric(y_actual)
}
if(is.logical(y_predicted)){
print("Info : Predicted values provided as Logical vector. Logical vector converted to numeric. ")
#Convert to numeric
y_predicted<- as.numeric(y_predicted)
}
# AUC ROC
pred_ROCR <- prediction(y_predicted, y_actual)
roc_ROCR <- performance(pred_ROCR, measure = "tpr", x.measure = "fpr")
plot(roc_ROCR, main = "ROC curve", colorize = T)
abline(a = 0, b = 1)
auc_ROCR <- performance(pred_ROCR, measure = "auc")
auc_ROCR <- auc_ROCR@y.values[[1]]
print(paste("Area under ROC = ", auc_ROCR))
return(auc_ROCR)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment