I am working with 4 lists of confusion matrices and I want to store the results in a table.
In order to get the confusion matrix list I created the following example:
data(iris)
data <- list(
iris %>% filter(Species != "setosa") %>% mutate(Species_to_predict = +(Species == "virginica")),
iris %>% filter(Species != "setosa") %>% mutate(Species_to_predict = +(Species == "virginica")),
iris %>% filter(Species != "setosa") %>% mutate(Species_to_predict = +(Species == "virginica")),
iris %>% filter(Species != "setosa") %>% mutate(Species_to_predict = +(Species == "virginica"))
)
LogitModels <- map(data, ~glm(formula = Species_to_predict ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width,
data = ., family = "binomial"))
Preds <- map2(
.x = data,
.y = LogitModels,
~predict(.y, newdata = .x, type = "response")
) %>%
map(., ~data.frame(.) %>%
setNames("Prediction") %>%
mutate(pred_status = ifelse(Prediction > 0.5, 1, 0)))
full_data <- list(
cbind(data[[1]], Preds[[1]]),
cbind(data[[2]], Preds[[2]]),
cbind(data[[3]], Preds[[3]]),
cbind(data[[4]], Preds[[4]])
)
CM <- full_data %>%
map(.,
~caret::confusionMatrix(
as.factor(..1$pred_status),
as.factor(..1$Species_to_predict),
mode = "everything",
positive = "1"
)
)
Once I have the confusion matrix I compute a calculation using:
Table <- list(
TP <- CM %>%
map(.,
~as.numeric(.x$table[[1]])
),
FP <- CM %>%
map(.,
~as.numeric(.x$table[[2]])
),
FN <- CM %>%
map(.,
~as.numeric(.x$table[[3]])
),
TN <- CM %>%
map(.,
~as.numeric(.x$table[[4]])
)
)
result1 <- pmap_dbl(Table, ~ ((..1 * ..4) - (..2 * ..3))/sqrt((..1 + ..2) * (..1 + ..3) * (..4 + ..2) * (..4 + ..3)))
I also compute the AUC
s.
AUCs = full_data %>%
map(., ~pROC::auc(as.numeric(.x$Species_to_predict), as.numeric(.x$pred_status)))
Finally I extract some further statistics from the confusion matrix (for only the first confusion matrix in the list).
data.frame(
Accuracy = CM[[1]]$overall[[1]],
Sensitivity = CM[[1]]$byClass[[1]],
Specificity = CM[[1]]$byClass[[2]],
Precision = CM[[1]]$byClass[[5]],
F1 = CM[[1]]$byClass[[7]]
) %>%
t()
Which gives this output:
[,1]
Accuracy 0.98
Sensitivity 0.98
Specificity 0.98
Precision 0.98
F1 0.98
I want to compute the above for all 4 confusion matrices in the list and add the additional calculations. That is the final output being a data table where I can use stargazer
to put into a LaTeX table.
Expected output:
[,1] [,2] [,3] [,4]
Accuracy 0.98 0.98 0.98 0.98
Sensitivity 0.98 0.98 0.98 0.98
Specificity 0.98 0.98 0.98 0.98
Precision 0.98 0.98 0.98 0.98
F1 0.98 0.98 0.98 0.98
AUCs 1 1 1 1
result1 0.96 0.96 0.96 0.96