Skip to content

Instantly share code, notes, and snippets.

@prasad83
Forked from ryanwitt/gist:2911560
Created March 15, 2017 19:15
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 prasad83/ec49d63ae3e7d29b25bbd1974a34b602 to your computer and use it in GitHub Desktop.
Save prasad83/ec49d63ae3e7d29b25bbd1974a34b602 to your computer and use it in GitHub Desktop.
Confusion matrix for a logistic glm model in R. Helpful for comparing glm to randomForests.
confusion.glm <- function(data, model) {
prediction <- ifelse(predict(model, data, type='response') > 0.5, TRUE, FALSE)
confusion <- table(prediction, as.logical(model$y))
confusion <- cbind(confusion, c(1 - confusion[1,1]/(confusion[1,1]+confusion[2,1]), 1 - confusion[2,2]/(confusion[2,2]+confusion[1,2])))
confusion <- as.data.frame(confusion)
names(confusion) <- c('FALSE', 'TRUE', 'class.error')
confusion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment