## S3 method for class 'factor'
cmatrix(actual, predicted, ...)
## S3 method for class 'factor'
weighted.cmatrix(actual, predicted, w, ...)
cmatrix(...)
weighted.cmatrix(...)
confusion matrix
cmatrix.factor | R Documentation |
Description
The cmatrix()
-function uses cross-classifying factors to build a confusion matrix of the counts at each combination of the factor levels. Each row of the matrix represents the actual factor levels, while each column represents the predicted factor levels.
Usage
Arguments
actual
|
A |
predicted
|
A |
…
|
Arguments passed into other methods. |
w
|
A |
Value
A named \(k\) x \(k\) <matrix>
of class
Dimensions
There is no robust defensive measure against misspecififying the confusion matrix. If the arguments are correctly specified, the resulting confusion matrix is on the form:
A (Predicted) | B (Predicted) | |
A (Actual) | Value | Value |
B (Actual) | Value | Value |
Examples
# 1) recode Iris
# to binary classification
# problem
$species_num <- as.numeric(
iris$Species == "virginica"
iris
)
# 2) fit the logistic
# regression
<- glm(
model formula = species_num ~ Sepal.Length + Sepal.Width,
data = iris,
family = binomial(
link = "logit"
)
)
# 3) generate predicted
# classes
<- factor(
predicted as.numeric(
predict(model, type = "response") >` 0.5
),
levels = c(1,0),
labels = c("Virginica", "Others")
)
# 3.1) generate actual
# classes
actual <- factor(
x = iris$species_num,
levels = c(1,0),
labels = c("Virginica", "Others")
)
# 4) summarise performance
# in a confusion matrix
# 4.1) unweighted matrix
confusion_matrix <- cmatrix(
actual = actual,
predicted = predicted
)
# 4.1.1) summarise matrix
summary(
confusion_matrix
)
# 4.1.2) plot confusion
# matrix
plot(
confusion_matrix
)
# 4.2) weighted matrix
confusion_matrix <- weighted.cmatrix(
actual = actual,
predicted = predicted,
w = iris$Petal.Length/mean(iris$Petal.Length)
)
# 4.2.1) summarise matrix
summary(
confusion_matrix
)
# 4.2.1) plot confusion
# matrix
plot(
confusion_matrix
)