shish
shish

Reputation: 943

confusionMatrix function and different sized matrices

I'm working with the confusionMatrix function using the caret package. The function works perfectly fine with a 2x2 (or 5x5 etc.) matrix. Example:

Table1:

   1  2
1 25 15
2  8 33

confusionMatrix(Table1) -> works!

But if I have a different sized table:

Table2:

    5   6   7
3   1   1   0
4   8   5   0
5 153  57   3
6  57 105  19
7   6  27  27
8   0   3   6

confusionMatrix(Table2) -> Won't work!
Error: Error in !all.equal(nrow(data), ncol(data)) : invalid argument type

How can I bypass this trouble since I'm forced to use the confusionMatrix function?

Upvotes: 3

Views: 3477

Answers (1)

flodel
flodel

Reputation: 89097

Try this:

x <- as.integer(Prediction)
y <- Test$quality
l <- union(x, y)
Table2 <- table(factor(x, l), factor(y, l))
confusionMatrix(Table2)

The idea was to convert your inputs to table to two factors that share the exact same levels (l). Then you are guaranteed that Table2 will be square.

Upvotes: 5

Related Questions