## S3 method for class 'factor'
npv(actual, predicted, micro = NULL, na.rm = TRUE, ...)
## S3 method for class 'factor'
weighted.npv(actual, predicted, w, micro = NULL, na.rm = TRUE, ...)
## S3 method for class 'cmatrix'
npv(x, micro = NULL, na.rm = TRUE, ...)
npv(...)
weighted.npv(...)
negative predictive value
npv.factor | R Documentation |
Description
The npv()
-function computes the negative predictive value, also known as the True Negative Predictive Value, between two vectors of predicted and observed factor()
values. The weighted.npv()
function computes the weighted negative predictive value.
Usage
Arguments
actual
|
A vector of |
predicted
|
A vector of |
micro
|
A |
na.rm
|
A |
…
|
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{\#TN_k}{\#TN_k + \#FN_k} \]
Where \(\#TN_k\) and \(\#FN_k\) are the number of true negatives and false negatives, respectively, for each class \(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 class-wise performance
# using Negative Predictive Value
# 4.1) unweighted Negative Predictive Value
npv(
actual = actual,
predicted = predicted
)
# 4.2) weighted Negative Predictive Value
weighted.npv(
actual = actual,
predicted = predicted,
w = iris$Petal.Length/mean(iris$Petal.Length)
)
# 5) evaluate overall performance
# using micro-averaged Negative Predictive Value
cat(
"Micro-averaged Negative Predictive Value", npv(
actual = actual,
predicted = predicted,
micro = TRUE
),
"Micro-averaged Negative Predictive Value (weighted)", weighted.npv(
actual = actual,
predicted = predicted,
w = iris$Petal.Length/mean(iris$Petal.Length),
micro = TRUE
),
sep = "\n"
)