## S3 method for class 'factor'
baccuracy(actual, predicted, adjust = FALSE, na.rm = TRUE, ...)
## S3 method for class 'factor'
weighted.baccuracy(actual, predicted, w, adjust = FALSE, na.rm = TRUE, ...)
## S3 method for class 'cmatrix'
baccuracy(x, adjust = FALSE, na.rm = TRUE, ...)
## Generic S3 method
baccuracy(
...,adjust = FALSE,
na.rm = TRUE
)
## Generic S3 method
weighted.baccuracy(
...,
w,adjust = FALSE,
na.rm = TRUE
)
Balanced Accuracy
baccuracy.factor | R Documentation |
Description
A generic function for the (normalized) balanced accuracy. Use weighted.baccuracy()
for the weighted balanced accuracy.
Usage
Arguments
actual
|
A vector of |
predicted
|
A vector of |
adjust
|
A logical value (default: FALSE). If TRUE the metric is adjusted for random chance \(\frac{1}{k}\). |
na.rm
|
A logical value (default: TRUE). If TRUE calculation of the metric is based on valid classes. |
…
|
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. If adjust == false
, the balanced accuracy of the classifier is calculated as,
\[ \hat{\alpha} = \frac{\text{sensitivity} + \text{specificity}}{2} \]
otherwise,
\[ \hat{\alpha} = \frac{\text{sensitivity} + \text{specificity}}{2} \frac{1}{k} \]
Where:
-
\(k\) is the number of classes
-
\(\text{sensitivity}\) is the overall sensitivity, and
-
\(\text{specificity}\) is the overall specificity
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 the
# model
cat(
"Balanced accuracy", baccuracy(
actual = actual,
predicted = predicted
),
"Balanced accuracy (weigthed)", weighted.baccuracy(
actual = actual,
predicted = predicted,
w = iris$Petal.Length/mean(iris$Petal.Length)
),sep = "\n"
)