Peter Porskamp
Peter Porskamp

Reputation: 23

Apply caret's confusionMatrix to a list made of data.frames resulting in multiple confusion matrices

I have a list of data.frames that I would like to run through caret's confusionMatrix function resulting in a list of confusion matrices, one confusion matrix for each data.frame.

Each data.frame has 14 variables with the two last variables containing the reference data (variable 13) and predicted data (variable 14).

Example below:

d1 <- data.frame(y1 = c(1,2,3,4,5,5,5,5,5), y2 = c(1,1,1,2,2,2,3,3,3), y3 = c(1,1,2,2,2,2,3,3,1))
d2 <- data.frame(y1 = c(3,2,1,4,5,6,7,5,4), y2 = c(1,1,1,1,2,2,2,3,3), y3 = c(1,2,1,1,2,3,3,3,3))
my.list <- list(d1, d2)

CM <- lapply(my.list, function(x) confusionMatrix(data = x[,3],
                            reference = x[,2],
                            positive = 'yes'))

Upvotes: 2

Views: 270

Answers (1)

paoloeusebi
paoloeusebi

Reputation: 1086

This extracts just the confusion matrices and put them in the CM list!

CM <- lapply(my.list, function(x) confusionMatrix(data = x[,3],
                                              reference = x[,2],
                                              positive = 'yes')$table)
CM

Upvotes: 1

Related Questions