Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 640
shish Avatar asked Dec 22 '25 04:12

shish


1 Answers

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.

like image 192
flodel Avatar answered Dec 23 '25 21:12

flodel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!