-
Notifications
You must be signed in to change notification settings - Fork 7
/
qroc.R
132 lines (122 loc) · 3.16 KB
/
qroc.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#' @title Receiver-Operator and Precision-Recall Curves
#'
#' @description Construction of ROC and PRC data and plots.
#'
#' @details
#'
#' The area under the curve (AUC) is determined by a trapezoid approximation for
#' both the AUROC and AUPRC.
#'
#' More details and examples for graphics within qwraps2 are in the
#' vignette(\dQuote{qwraps2-graphics}, package = \dQuote{qwraps2})
#'
#' @param x an object
#' @param ... pass through
#'
#' @return a ggplot. Minimal aesthetics have been used so that the user may
#' modify the graphic as desired with ease.
#'
#' @examples
#' #########################################################
#' # Example 1
#'
#' df <-
#' data.frame(
#' truth = c(1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0)
#' , pred = c(1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0)
#' )
#'
#' cm <- confusion_matrix(df$truth, df$pred)
#' qroc(cm)
#' qprc(cm)
#'
#' #########################################################
#' # Getting a ROC or PRC plot from a glm object:
#'
#' mod <- glm(
#' formula = spam ~ word_freq_our + word_freq_over + capital_run_length_total
#' , data = spambase
#' , family = binomial()
#' )
#'
#' qroc(mod)
#' qprc(mod)
#'
#' #########################################################
#' # View the vignette for more examples
#' \dontrun{
#' vignette("qwraps2-graphics")
#' }
#'
#' @name
#' qroc-qprc
NULL
#' @export
#' @rdname qroc-qprc
qroc <- function(x, ...) {
UseMethod("qroc")
}
#' @export
#' @rdname qroc-qprc
qroc.default <- function(x, ...) {
qroc_ggplot(x, ...)
}
#' @export
#' @rdname qroc-qprc
qroc.qwraps2_confusion_matrix <- function(x, ...) {
qroc_ggplot(x[["cm_stats"]], ...)
}
#' @export
#' @rdname qroc-qprc
qroc.glm <- function(x, ...) {
qroc(confusion_matrix(x, ...))
}
qroc_ggplot <- function(data) {
stopifnot("specificity" %in% names(data))
stopifnot("sensitivity" %in% names(data))
data[["FPR"]] <- 1 - data[["specificity"]]
data[["TPR"]] <- data[["sensitivity"]]
ggplot2::ggplot(data) +
eval(substitute(ggplot2::aes(x = X, y = Y), list(X = as.name("FPR"), Y = as.name("TPR")))) +
ggplot2::geom_point() +
ggplot2::geom_path() +
ggplot2::xlim(0, 1) +
ggplot2::ylim(0, 1) +
ggplot2::annotate(geom = "segment", x = 0, y = 0, xend = 1, yend = 1, color = "black", linetype = 2)
}
#' @export
#' @rdname qroc-qprc
qprc <- function(x, ...) {
UseMethod("qprc")
}
#' @export
#' @rdname qroc-qprc
qprc.default <- function(x, ...) {
qprc_ggplot(x, ...)
}
#' @export
#' @rdname qroc-qprc
qprc.qwraps2_confusion_matrix <- function(x, ...) {
qprc_ggplot(x[["cm_stats"]], prevalence = x$prevalence, ...)
}
#' @export
#' @rdname qroc-qprc
qprc.glm <- function(x, ...) {
qprc(confusion_matrix(x, ...))
}
qprc_ggplot <- function(data, prevalence = NULL) {
stopifnot("sensitivity" %in% names(data)) # recall
stopifnot("ppv" %in% names(data)) # Precision
g <-
ggplot2::ggplot(data = data) +
eval(substitute(ggplot2::aes(x = X, y = Y), list(X = as.name("sensitivity"), Y = as.name("ppv")))) +
ggplot2::geom_point() +
ggplot2::geom_path() +
ggplot2::xlim(0, 1) +
ggplot2::ylim(0, 1)
if (!is.null(prevalence)) {
stopifnot(is.numeric(prevalence))
g <- g + ggplot2::geom_hline(yintercept = prevalence)
}
g
}