## S3 method for class 'factor'
accuracy(actual, predicted, ...)
## S3 method for class 'factor'
weighted.accuracy(actual, predicted, w, ...)
## S3 method for class 'cmatrix'
accuracy(x, ...)
## Generic S3 method
accuracy(...)
## Generic S3 method
weighted.accuracy(
...,
w )
Accuracy
accuracy.factor | R Documentation |
Description
A generic function for the (normalized) accuracy in classification tasks. Use weighted.accuracy()
for the weighted accuracy.
Usage
Arguments
actual
|
A vector of |
predicted
|
A vector of |
…
|
micro = NULL, na.rm = TRUE Arguments passed into other methods |
w
|
A |
x
|
A confusion matrix created |
Value
A <numeric>
-vector of length 1
Definition
Let \(\hat{\alpha} \in [0, 1]\) be the proportion of correctly predicted classes. The accuracy of the classifier is calculated as,
\[ \hat{\alpha} = \frac{\#TP + \#TN}{\#TP + \#TN + \#FP + \#FN} \]
Where:
-
\(\#TP\) is the number of true positives,
-
\(\#TN\) is the number of true negatives,
-
\(\#FP\) is the number of false positives, and
-
\(\#FN\) is the number of false negatives.
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) evaluate model
# performance
cat(
"Accuracy", accuracy(
actual = actual,
predicted = predicted
),
"Accuracy (weigthed)", weighted.accuracy(
actual = actual,
predicted = predicted,
w = iris$Petal.Length/mean(iris$Petal.Length)
),sep = "\n"
)