## S3 method for class 'factor'
cmatrix(actual, predicted, ...)
## S3 method for class 'factor'
weighted.cmatrix(actual, predicted, w, ...)
## Generic S3 method
cmatrix(
actual,
predicted,
...
)
## Generic S3 method
weighted.cmatrix(
actual,
predicted,
w,
... )
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\)
Dimensions
There is no robust defensive measure against mis-specifying 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
<- factor(
actual x = iris$species_num,
levels = c(1,0),
labels = c("Virginica", "Others")
)
# 4) summarise performance
# in a confusion matrix
# 4.1) unweighted matrix
<- cmatrix(
confusion_matrix 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
<- weighted.cmatrix(
confusion_matrix 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 )