Search code examples
rconfusion-matrix

Confusion matrix for ranges


I have two data frames, first on is my predicted values in range format:

structure(c("(-3,2]", "(2,7]", "(-3,2]", "(2,7]", "(-3,2]", 
"(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", 
"(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", 
"(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(2,7]"), .Dim = c(5L, 
5L), .Dimnames = list(NULL, c("Q", "Y", "S", "L", "X")))

and other one is the reference values:

structure(c("(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", 
"(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", 
"(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", 
"(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]", "(-3,2]"), .Dim = c(5L, 
 5L), .Dimnames = list(NULL, c("Q", "Y", "S", "L", "X")))

I have tried couple of ways, but I could not figure out how to perform a confusion matrix. Can anyone please help me? Thanks!


Solution

  • There cannot be much "confusion" if all the values of one of the objects are all the same but here's code that should work for a more complete listing:

    table(unlist(dat2),unlist(dat1))
    
             (-3,2] (2,7]
      (-3,2]     22     3
    

    With your earlier print-ed data I got:

    table(unlist(dat2),unlist(dat1))
    
             (-3,2] (2,7]
      (-3,2]     21     0
      (2,7]       2     2
    

    If they had been factors you might not have succeeded. Might have needed to run lapply using as.character to coerce to "character".