rw2
rw2

Reputation: 1815

Apply confusionMatrix R function across multiple pairs of columns, saving output in lists or tidy df

I would like to apply caret's confusionMatrix function across multiple columns, saving the results in a data-frame.

I have two data frames of the same format - "actuals" and "predictions". They look like this:

x1   x2   x3   x4
 N    N    S    E
 E    W    E    E
 N    W    E    E

etc... for many rows, with 4 levels in each column, and quite a few columns

I would like to compare the corresponding columns of the two data frames using confusionMatrix. I can do this column by column, for example starting with column 1:

confusionMatrix(predictions[,1], actuals[,1])

but rather than go through each column one-by-one, I would like to apply the confusionMatrix to all corresponding columns in the two data frames.

Ideally I would like the output saved in some kind of list or data frame, so that I can easily pull out particular outputs from confusionMatrix (e.g. kappa or sensitivity) for all the comparisons. I've used purrr and broom to do something similar with the outputs from linear models, but the inputs here are quite different and I haven't been able to find a way to get this to work.

Many thanks

Upvotes: 1

Views: 581

Answers (1)

Anil Kumar
Anil Kumar

Reputation: 445

a <- list()

for (i in 1:ncol(predictions))
    {
      a[[i]] <- confusionMatrix(predictions[,i], actuals[,i])
    }

print(a)

Upvotes: 1

Related Questions