Created
June 11, 2012 17:46
-
-
Save ryanwitt/2911560 to your computer and use it in GitHub Desktop.
Confusion matrix for a logistic glm model in R. Helpful for comparing glm to randomForests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
In the case of a prediction vector of all True or all False [Class-Error 100%] this throws an error which is fixed by changing to
confusion <- table(factor(prediction, levels = c(FALSE, TRUE)), as.logical(model$y))
Great! Thank
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What do you do with that, after you have run that code? There is no output, so I assume something additional coding follows this...?