Learn R Programming

qwraps2 (version 0.1.2)

qroc: Receiver Operating Curves

Description

Construction of ROC curves.

Usage

qroc(x, ...)

qroc_build_data_frame(fit, n_threshold = 200)

auc(.data)

Arguments

x
a glm fit or data.frame generated by qroc_build_data_frame.
...
Ignored
fit
a glm fit with family = binomial().
n_threshold
number of thresholds to test against.
.data
a data.frame generated by qroc_build_data_frame.

Value

  • a ggplot. Minimal aesthetics have been used so that the user may modify the graphic as desired with ease.

    AUC for the data set generated by

Details

Given a glm fit with family = "binomial" (either a log-link or logit-link should be fine, a data set will be constructed and ROC plots generated.

The area under the curve (AUC) is determined by a trapezoid approximation.

Examples

Run this code
# load ggplot2 and the diamonds data set
library(ggplot2)
data(diamonds, package = "ggplot2")

# Create two logistic regression models
fit1 <- glm(I(price > 2800) ~ cut * color, data = diamonds, family = binomial())
fit2 <- glm(I(price > 2800) ~ cut + color + clarity, data = diamonds, family = binomial())

# Easiest way to get an ROC plot:
qroc(fit1)
qroc(fit2)

# Create two data sets, this will also let you get the AUC out
data1 <- qroc_build_data_frame(fit1)
data2 <- qroc_build_data_frame(fit2)

auc(data1)
auc(data2)

# Plotting the ROC from the data set can be done too
qroc(data1)

# Add the AUC value to the plot title
qroc(data2) + ggtitle(paste("Fit 2\nAUC =", round(auc(data2), 2)))

# build a data set for plotting to ROCs on one plot
plot_data <- rbind(cbind(Model = "fit1", data1),
                   cbind(Model = "fit2", data2))
qroc(plot_data) + aes(color = Model)

# with AUC in the legend
plot_data <- rbind(cbind(Model = paste("Fit1\nauc =", round(auc(data1), 3)), data1),
                   cbind(Model = paste("Fit2\nauc =", round(auc(data2), 3)), data2))
qroc(plot_data) +
  theme_bw() +
  aes(color = Model, linetype = Model) +
  theme(legend.position   = "bottom",
        legend.text.align = 0.5)

Run the code above in your browser using DataLab