Receiver Operator Characteristics

ROC.factor R Documentation

Description

The ROC()-function computes the tpr() and fpr() at thresholds provided by the \(response\)- or \(thresholds\)-vector. The function constructs a data.frame() grouped by \(k\)-classes where each class is treated as a binary classification problem.

Usage

## S3 method for class 'factor'
ROC(actual, response, thresholds = NULL, presorted = FALSE, ...)

## S3 method for class 'factor'
weighted.ROC(actual, response, w, thresholds = NULL, presorted = FALSE, ...)

## Generic S3 method
ROC(
 actual,
 response,
 thresholds = NULL,
 presorted  = FALSE,
 ...
)

## Generic S3 method
weighted.ROC(
 actual,
 response,
 w,
 thresholds = NULL,
 presorted  = FALSE,
 ...
)

Arguments

actual

A vector of values of length \(n\), and \(k\) levels.

response

A \(n \times k\) <numeric>-matrix. The estimated response probabilities for each class \(k\).

thresholds

An optional <numeric> vector of length \(n\) (default: NULL).

presorted

A -value length 1 (default: FALSE). If TRUE the input will not be sorted by threshold.

Arguments passed into other methods.

w

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

Value

A data.frame on the following form,

threshold

<numeric> Thresholds used to determine tpr() and fpr()

level

The level of the actual

label

The levels of the actual

fpr

<numeric> The false positive rate

tpr

<numeric> The true positve rate

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
response <- predict(model, type = "response")

# 3.1) generate actual
# classes
actual <- factor(
  x = iris$species_num,
  levels = c(1,0),
  labels = c("Virginica", "Others")
)

# 4) generate reciever
# operator characteristics

# 4.1) calculate residual
# probability and store as matrix
response <- matrix(
  data = cbind(response, 1-response),
  nrow = length(actual)
)

# 4.2) construct 
# data.frame
roc <- ROC(
  actual   = actual,
  response = response
)

# 5) plot by species
plot(roc)

# 5.1) summarise
summary(roc)

# 6) provide custom
# threholds
roc <- ROC(
  actual     = actual,
  response   = response,
  thresholds = seq(
    1,
    0,
    length.out = 20
  )
)

# 5) plot by species
plot(roc)