## S3 method for class 'factor'
fbeta(actual, predicted, beta = 1, micro = NULL, na.rm = TRUE, ...)
## S3 method for class 'factor'
weighted.fbeta(actual, predicted, w, beta = 1, micro = NULL, na.rm = TRUE, ...)
## S3 method for class 'cmatrix'
fbeta(x, beta = 1, micro = NULL, na.rm = TRUE, ...)
## Generic S3 method
fbeta(
...,beta = 1,
micro = NULL,
na.rm = TRUE
)
## Generic S3 method
weighted.fbeta(
...,
w,beta = 1,
micro = NULL,
na.rm = TRUE
)
F-beta Score
fbeta.factor | R Documentation |
Description
A generic function for the \(F_{\beta}\)-score. Use weighted.fbeta()
for the weighted \(F_{\beta}\)-score.
Usage
Arguments
actual
|
A vector of |
predicted
|
A vector of |
beta
|
A |
micro
|
A |
na.rm
|
A |
…
|
micro = NULL, na.rm = TRUE Arguments passed into other methods |
w
|
A |
x
|
A confusion matrix created |
Value
If micro
is NULL (the default), a named <numeric>
-vector of length k
If micro
is TRUE or FALSE, a <numeric>
-vector of length 1
Definition
Let \(\hat{F}_{\beta} \in [0, 1]\) be the \(F_{\beta}\) score, which is a weighted harmonic mean of precision and recall. \(F_{\beta}\) score of the classifier is calculated as,
\[ \hat{F}_{\beta} = \left(1 + \beta^2\right) \frac{\text{Precision} \times \text{Recall}} {\beta^2 \times \text{Precision} + \text{Recall}} \]
Substituting \(\text{Precision} = \frac{\#TP_k}{\#TP_k + \#FP_k}\) and \(\text{Recall} = \frac{\#TP_k}{\#TP_k + \#FN_k}\) yields:
\[ \hat{F}_{\beta} = \left(1 + \beta^2\right) \frac{\frac{\#TP_k}{\#TP_k + \#FP_k} \times \frac{\#TP_k}{\#TP_k + \#FN_k}} {\beta^2 \times \frac{\#TP_k}{\#TP_k + \#FP_k} + \frac{\#TP_k}{\#TP_k + \#FN_k}} \]
Where:
-
\(\#TP_k\) is the number of true positives,
-
\(\#FP_k\) is the number of false positives,
-
\(\#FN_k\) is the number of false negatives, and
-
\(\beta\) is a non-negative real number that determines the relative importance of precision vs. recall in the score.
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 class-wise performance
# using F1-score
# 4.1) unweighted F1-score
fbeta(
actual = actual,
predicted = predicted,
beta = 1
)
# 4.2) weighted F1-score
weighted.fbeta(
actual = actual,
predicted = predicted,
w = iris$Petal.Length/mean(iris$Petal.Length),
beta = 1
)
# 5) evaluate overall performance
# using micro-averaged F1-score
cat(
"Micro-averaged F1-score", fbeta(
actual = actual,
predicted = predicted,
beta = 1,
micro = TRUE
),"Micro-averaged F1-score (weighted)", weighted.fbeta(
actual = actual,
predicted = predicted,
w = iris$Petal.Length/mean(iris$Petal.Length),
beta = 1,
micro = TRUE
),sep = "\n"
)