## S3 method for class 'factor'
nlr(actual, predicted, ...)
## S3 method for class 'factor'
weighted.nlr(actual, predicted, w, ...)
## S3 method for class 'cmatrix'
nlr(x, ...)
nlr(...)
weighted.nlr(...)
negative likelihood ratio
nlr.factor | R Documentation |
Description
The nlr()
-function computes the negative likelihood ratio, also known as the likelihood ratio for negative results, between two vectors of predicted and observed factor()
values. The weighted.nlr()
function computes the weighted negative likelihood ratio.
Usage
Arguments
actual
|
A vector of |
predicted
|
A vector of |
…
|
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
Calculation
The metric is calculated for each class \(k\) as follows,
\[ \frac{1 - \text{Sensitivity}_k}{\text{Specificity}_k} \]
Where sensitivity (or true positive rate) is calculated as \(\frac{\#TP_k}{\#TP_k + \#FN_k}\) and specificity (or true negative rate) is calculated as \(\frac{\#TN_k}{\#TN_k + \#FP_k}\).
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) evaluate model performance
# with class-wise negative likelihood ratios
cat("Negative Likelihood Ratio", sep = "\n")
nlr(
actual = actual,
predicted = predicted
)
cat("Negative Likelihood Ratio (weighted)", sep = "\n")
weighted.nlr(
actual = actual,
predicted = predicted,
w = iris$Petal.Length/mean(iris$Petal.Length)
)