2

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'))
  • Welcome to SO. I will try to help you, but it is always difficult without reproducible examples. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – paoloeusebi Nov 01 '18 at 23:56
  • @paoloeusebi thanks, hopefully the example I attached helps clarify. – Peter Porskamp Nov 02 '18 at 00:14

1 Answers1

1

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
paoloeusebi
  • 1,056
  • 8
  • 19