Extracting F1 scores from a sequence of confusion matrices

I have produced a set of confusion matrices in order to evaluate their fitness with different k values (in a knn3 regression.

cal <- seq(5, 15, 2)
train_knn <- function(x) {
  knn3(A ~ B, data = dat, k = x)
}

mods <- lapply(cal, train_knn)

cms <- lapply(mods, function(model) confusionMatrix(predict(model, test_set, type = "class"), test_set$A))
acc <- sapply(cms, function(cm) cm$overall["Accuracy"])
plot(cal, accuracy, type = "l")

I would like a separate plot with F1 scores. My understanding is I should be able to add that functionality with a snippet like this:

f_1 <- sapply(cms, function(cm) cm$byClass["F1"])
plot(cal, f_1, type = "l")

Can I subset a cm object in this manner? Is this the proper syntax?

Thank you very much!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.