Given actual and predicted group assignments, give the confusion matrix

1 2 |

`actual` |
Actual (prior) group assigments |

`predicted` |
Predicted group assigments. |

`gpnames` |
Names for groups, if different from |

`rowcol` |
For predicted categories to appear as rows,
specify |

`printit` |
Character vector. Print |

`prior` |
Prior probabilities for groups, if different from the relative group frequencies |

`digits` |
Number of decimal digits to display in printed output |

Predicted group assignments should be estimated from cross-validation or from bootstrap out-of-bag data. Better still, work with assignments for test data that are completely separate from the data used to dervive the model.

A list with elements overall (overall accuracy), confusion (confusion matrix) and prior (prior used for calculation of overall accuracy)

John H Maindonald

Maindonald and Braun: 'Data Analysis and Graphics Using R', 3rd edition 2010, Section 12.2.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | ```
library(MASS)
library(DAAG)
cl <- lda(species ~ length+breadth, data=cuckoos, CV=TRUE)$class
confusion(cl, cuckoos$species)
## The function is currently defined as
function (actual, predicted, gpnames = NULL,
rowcol = c("actual", "predicted"),
printit = c("overall","confusion"),
prior = NULL, digits = 3)
{
if (is.null(gpnames))
gpnames <- levels(actual)
if (is.logical(printit)){
if(printit)printit <- c("overall","confusion")
else printit <- ""
}
tab <- table(actual, predicted)
acctab <- t(apply(tab, 1, function(x) x/sum(x)))
dimnames(acctab) <- list(Actual = gpnames, `Predicted (cv)` = gpnames)
if (is.null(prior)) {
relnum <- table(actual)
prior <- relnum/sum(relnum)
acc <- sum(tab[row(tab) == col(tab)])/sum(tab)
}
else {
acc <- sum(prior * diag(acctab))
}
names(prior) <- gpnames
if ("overall"%in%printit) {
cat("Overall accuracy =", round(acc, digits), "\n")
if(is.null(prior)){
cat("This assumes the following prior frequencies:",
"\n")
print(round(prior, digits))
}
}
if ("confusion"%in%printit) {
cat("\nConfusion matrix", "\n")
print(round(acctab, digits))
}
invisible(list(overall=acc, confusion=acctab, prior=prior))
}
``` |

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

Please suggest features or report bugs with the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.