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))
}
Run the code above in your browser using DataLab