Search code examples
rcross-validationcontingency

Error rate in multidimensional contingency tables R


Good evening,

I found the classAgreement function from package e1071 in R very useful and informative when working with contingency tables.

It works great with 2-dimensions. But if I have 3 or more dimensions it doesn't work.

I have reviewed several books, papers and web pages, and still cannot find a built-in function that works as classAgreement() with more than 2 dimensions.

Does anybody know a function/package in R that can do that?

If it doesn't exist, can somebody point me in the right direction for building a function that works with 3 dimensions in cross-validation?

Thank you.


Solution

  • You can use apply, with the third margin index. For example:

    g1 <- sample(1:5, size=1000, replace=TRUE) 
    g2 <- sample(1:5, size=1000, replace=TRUE)
    g3 <- sample(1:5, size=1000, replace=TRUE)
    tab <- table(g1, g2,g3)
    apply(tab,3,classAgreement)
    

    EDIT to get the diagonal elements

    res <- apply(tab,3,classAgreement)
    sapply(res, '[[','diag')
            1         2         3         4         5 
    0.2146341 0.2029703 0.2227488 0.1513761 0.2073171 
    sum(sapply(res, '[[','diag'))
    [1] 0.9990465