knitr::opts_chunk$set( fig.path = "man/figures/", comment = NA )
set.seed(1234) N = 2500 x = rnorm(N) lp = 0 + .5*x y = rbinom(N, size = 1, prob = plogis(lp)) model = glm(y ~ x, family = binomial) predict_class = predict(model) > 0
library(confusionMatrix) confusion_matrix(predict_class, y)
Works for the multi-class setting too, but not all statistics are applicable/available. Note that the 'Other' statistics are provided for each class, as well as averaged.
p_multi = sample(letters[1:4], 250, replace = TRUE, prob = 1:4) o_multi = sample(letters[1:4], 250, replace = TRUE, prob = 1:4) confusion_matrix(p_multi, o_multi)
In wide format, you have a simple way to pluck any particular statistic.
cm = confusion_matrix(predict_class, y) cm[['Other']][['N']] cm[['Association and Agreement']][['Yule']]
However, if you want to present a lot of statistics, a longer format might be more ideal.
confusion_matrix(predict_class, y, longer = TRUE)
Either way, the tibble output will work nicely with various packages for formatting tables.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.