zero-one loss

zerooneloss.factor R Documentation

Description

The zerooneloss()-function computes the zero-one Loss, a classification loss function that calculates the proportion of misclassified instances between two vectors of predicted and observed factor() values. The weighted.zerooneloss() function computes the weighted zero-one loss.

Usage

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

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

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

zerooneloss(...)

weighted.zerooneloss(...)

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

A <numeric>-vector of length 1

Calculation

The metric is calculated as follows,

\[ \frac{\#FP + \#FN}{\#TP + \#TN + \#FP + \#FN} \]

Where \(\#TP\), \(\#TN\), \(\#FP\), and \(\#FN\) represent the true positives, true negatives, false positives, and false negatives, respectively.

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 using Zero-One Loss
cat(
  "Zero-One Loss", zerooneloss(
    actual    = actual,
    predicted = predicted
  ),
  "Zero-One Loss (weigthed)", weighted.zerooneloss(
    actual    = actual,
    predicted = predicted,
    w         = iris$Petal.Length/mean(iris$Petal.Length)
  ),
  sep = "\n"
)