positive likelihood ratio

plr.factor R Documentation

Description

The plr()-function computes the positive likelihood ratio, also known as the likelihood ratio for positive results, between two vectors of predicted and observed factor() values. The weighted.plr() function computes the weighted positive likelihood ratio.

Usage

## S3 method for class 'factor'
plr(actual, predicted, ...)

## S3 method for class 'factor'
weighted.plr(actual, predicted, w, ...)

## S3 method for class 'cmatrix'
plr(x, ...)

plr(...)

weighted.plr(...)

Arguments

actual

A vector of <factor>- of length \(n\), and \(k\) levels.

predicted

A vector of <factor>-vector of length \(n\), and \(k\) levels.

Arguments passed into other methods

w

A <numeric>-vector of length \(n\). NULL by default.

x

A confusion matrix created cmatrix().

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{\text{Sensitivity}_k}{1 - \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}\).

When aggregate = TRUE, the micro-average is calculated,

\[ \frac{\sum_{k=1}^k \text{Sensitivity}_k}{1 - \sum_{k=1}^k \text{Specificity}_k} \]

Examples

# 1) recode Iris
# to binary classification
# problem
iris$species_num <- as.numeric(
  iris$Species == "virginica"
)

# 2) fit the logistic
# regression
model <- glm(
  formula = species_num ~ Sepal.Length + Sepal.Width,
  data    = iris,
  family  = binomial(
    link = "logit"
  )
)

# 3) generate predicted
# classes
predicted <- factor(
  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 positive likelihood ratios
cat("Positive Likelihood Ratio", sep = "\n")
plr(
  actual    = actual, 
  predicted = predicted
)

cat("Positive Likelihood Ratio (weighted)", sep = "\n")
weighted.plr(
  actual    = actual,
  predicted = predicted,
  w         = iris$Petal.Length/mean(iris$Petal.Length)
)