klaR (version 0.6-1)

errormatrix: Tabulation of prediction errors by classes

Description

Cross-tabulates true and predicted classes with the option to show relative frequencies.

Usage

errormatrix(true, predicted, relative = FALSE)

Arguments

true
Vector of true classes.
predicted
Vector of predicted classes.
relative
Logical. If TRUE rows are normalized to show relative frequencies (see below).

Value

  • A (named) matrix.

encoding

latin1

concept

  • Visualizing classification results
  • Confusion matrix

Details

Given vectors of true and predicted classes, a (symmetric) table of misclassifications is constructed. Element [i,j] shows the number of objects of class i that were classified as class j; so the main diagonal shows the correct classifications. The last row and column show the corresponding sums of misclassifications, the lower right element is the total sum of misclassifications. If relative is TRUE, the rows are normalized so they show relative frequencies instead. The lower right element now shows the total error rate, and the remaining last row sums up to one, so it shows where the misclassifications went.

See Also

table

Examples

Run this code
data(iris)
library(MASS)
x <- lda(Species ~ Sepal.Length + Sepal.Width, data=iris)
y <- predict(x, iris)

# absolute numbers: 
errormatrix(iris$Species, y$class)

# relative frequencies: 
errormatrix(iris$Species, y$class, relative = TRUE)

# percentages: 
round(100 * errormatrix(iris$Species, y$class, relative = TRUE), 0)

# expected error rate in case of class prior: 
indiv.rates <- errormatrix(iris$Species, y$class, relative = TRUE)[1:3, 4]
prior <- c("setosa" = 0.2, "versicolor" = 0.3, "virginica" = 0.5)
total.rate <- t(indiv.rates) %*% prior
total.rate

Run the code above in your browser using DataCamp Workspace