-
-
Notifications
You must be signed in to change notification settings - Fork 402
/
generateThreshVsPerf.R
225 lines (216 loc) · 9.06 KB
/
generateThreshVsPerf.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#' @title Generate threshold vs. performance(s) for 2-class classification.
#'
#' @family generate_plot_data
#' @family thresh_vs_perf
#'
#' @template arg_plotroc_obj
#' @template arg_measures
#' @param gridsize [\code{integer(1)}]\cr
#' Grid resolution for x-axis (threshold).
#' Default is 100.
#' @param task.id [\code{character(1)}]\cr
#' Selected task in \code{\link{BenchmarkResult}} to do plots for, ignored otherwise.
#' Default is first task.
#' @export
generateThreshVsPerfData = function(obj, measures, gridsize = 100L, task.id = NULL)
UseMethod("generateThreshVsPerfData")
#' @export
generateThreshVsPerfData.Prediction = function(obj, measures, gridsize = 100L, task.id = NULL)
generateThreshVsPerfData.list(namedList("prediction", obj), measures, gridsize, task.id)
#' @export
generateThreshVsPerfData.ResampleResult = function(obj, measures, gridsize = 100L, task.id = NULL) {
obj = getRRPredictions(obj)
assertClass(obj, "Prediction")
assert(obj$predict.type == "prob")
generateThreshVsPerfData.Prediction(obj, measures, gridsize)
}
#' @export
generateThreshVsPerfData.BenchmarkResult = function(obj, measures, gridsize = 100L, task.id = NULL) {
tids = getBMRTaskIds(obj)
if (is.null(task.id))
task.id = tids[1L]
else
assertChoice(task.id, tids)
obj = getBMRPredictions(obj, task.ids = task.id, as.df = FALSE)[[1L]]
assert(all(extractSubList(obj, "predict.type") == "prob"))
generateThreshVsPerfData.list(obj, measures, gridsize, task.id)
}
#' @export
generateThreshVsPerfData.list = function(obj, measures, gridsize = 100L, task.id = NULL) {
assertList(obj, c("Prediction", "ResampleResult"), min.len = 1L)
## unwrap ResampleResult to Prediction and set default names
if (inherits(obj[[1L]], "ResampleResult")) {
if (is.null(names(obj)))
names(obj) = extractSubList(obj, c("pred", "learner.id"))
obj = extractSubList(obj, "pred", simplify = FALSE)
}
td = BBmisc::extractSubList(obj, "task.desc", simplify = FALSE)[[1L]]
measures = checkMeasures(measures, td)
mids = extractSubList(measures, "id")
if (td$type != "classif" || length(td$class.levels) != 2L)
stopf("Task must be binary classification!")
assertList(obj, names = "unique")
thseq = seq(0, 1, length.out = gridsize)
grid = data.frame(threshold = thseq)
obj = lapply(obj, function(x) {
assertClass(x, "Prediction")
assert(x$predict.type == "prob")
asMatrixRows(lapply(thseq, function(threshold) {
pp = setThreshold(x, threshold = threshold)
performance(pp, measures = measures)
}), col.names = mids)
})
out = plyr::ldply(obj, .id = "learner")
out = cbind(grid, out)
makeS3Obj("ThreshVsPerfData",
measures = mids,
data = out)
}
#' @title Plot threshold vs. performance(s) for 2-class classification using ggplot2.
#'
#' @family plot
#' @family thresh_vs_perf
#'
#' @param obj [\code{ThreshVsPerfData}]\cr
#' Result of \code{\link{generateThreshVsPerfData}}.
#' @param facet [\code{character(1)}]\cr
#' Selects \dQuote{measure} or \dQuote{learner} to be the facetting variable.
#' The variable mapped to \code{facet} must have more than one unique value, otherwise it will
#' be ignored. The variable not chosen is mapped to color if it has more than one unique value.
#' The default is \dQuote{measure}.
#' @param mark.th [\code{numeric(1)}]\cr
#' Mark given threshold with vertical line?
#' Default is \code{NA} which means not to do it.
#' @template ret_gg2
#' @export
#' @examples
#' lrn = makeLearner("classif.rpart", predict.type = "prob")
#' mod = train(lrn, sonar.task)
#' pred = predict(mod, sonar.task)
#' pvs = generateThreshVsPerfData(pred, list(tpr, fpr))
#' plotThreshVsPerf(pvs)
plotThreshVsPerf = function(obj, facet = "measure", mark.th = NA_real_) {
assertClass(obj, classes = "ThreshVsPerfData")
mappings = c("measure", "learner")
assertChoice(facet, mappings)
color = mappings[mappings != facet]
for (i in 1:length(obj$measures)) {
measure.name = get(obj$measures[i])$name
colnames(obj$data)[colnames(obj$data) == obj$measures[i]] = measure.name
obj$measures[i] = measure.name
}
data = reshape2::melt(obj$data, measure.vars = obj$measures,
variable.name = "measure", value.name = "perf",
id.vars = c("learner", "threshold"))
nlearn = length(unique(data$learner))
nmeas = length(unique(data$measure))
if ((color == "learner" & nlearn == 1L) | (color == "measure" & nmeas == 1L))
color = NULL
if ((facet == "learner" & nlearn == 1L) | (facet == "measure" & nmeas == 1L))
facet = NULL
if (!is.null(color))
plt = ggplot2::ggplot(data, aes_string(x = "threshold", y = "perf", color = color))
else
plt = ggplot2::ggplot(data, aes_string(x = "threshold", y = "perf"))
plt = plt + ggplot2::geom_line()
if (!is.na(mark.th))
plt = plt + ggplot2::geom_vline(xintercept = mark.th)
if (!is.null(facet))
plt = plt + ggplot2::facet_wrap(as.formula(paste("~", facet)), scales = "free_y")
return(plt)
}
#' @title Plot threshold vs. performance(s) for 2-class classification using ggvis.
#'
#' @family plot
#' @family thresh_vs_perf
#'
#' @param obj [\code{ThreshVsPerfData}]\cr
#' Result of \code{\link{generateThreshVsPerfData}}.
#' @param mark.th [\code{numeric(1)}]\cr
#' Mark given threshold with vertical line?
#' Default is \code{NA} which means not to do it.
#' @param interaction [\code{character(1)}]\cr
#' Selects \dQuote{measure} or \dQuote{learner} to be used in a Shiny application
#' making the \code{interaction} variable selectable via a drop-down menu.
#' This variable must have more than one unique value, otherwise it will be ignored.
#' The variable not chosen is mapped to color if it has more than one unique value.
#' Note that if there are multiple learners and multiple measures interactivity is
#' necessary as ggvis does not currently support facetting or subplots.
#' The default is \dQuote{measure}.
#' @template ret_ggv
#' @export
#' @examples \dontrun{
#' lrn = makeLearner("classif.rpart", predict.type = "prob")
#' mod = train(lrn, sonar.task)
#' pred = predict(mod, sonar.task)
#' pvs = generateThreshVsPerfData(pred, list(tpr, fpr))
#' plotThreshVsPerfGGVIS(pvs)
#' }
plotThreshVsPerfGGVIS = function(obj, interaction = "measure",
mark.th = NA_real_) {
assertClass(obj, classes = "ThreshVsPerfData")
mappings = c("measure", "learner")
assertChoice(interaction, mappings)
color = mappings[mappings != interaction]
for (i in 1:length(obj$measures)) {
measure.name = get(obj$measures[i])$name
colnames(obj$data)[colnames(obj$data) == obj$measures[i]] = measure.name
obj$measures[i] = measure.name
}
data = reshape2::melt(obj$data, measure.vars = obj$measures,
variable.name = "measure", value.name = "perf",
id.vars = c("learner", "threshold"))
nmeas = length(unique(data$measure))
nlearn = length(unique(data$learner))
if ((color == "learner" & nlearn == 1L) | (color == "measure" & nmeas == 1L))
color = NULL
if ((interaction == "learner" & nlearn == 1L) | (interaction == "measure" & nmeas == 1L))
interaction = NULL
create_plot = function(data, color, measures) {
if (!is.null(color)) {
plt = ggvis::ggvis(data, ggvis::prop("x", as.name("threshold")),
ggvis::prop("y", as.name("perf")),
ggvis::prop("stroke", as.name(color)))
} else {
plt = ggvis::ggvis(data, ggvis::prop("x", as.name("threshold")),
ggvis::prop("y", as.name("perf")))
}
plt = ggvis::layer_lines(plt)
if (!is.na(mark.th) & is.null(interaction)) { ## cannot do vline with reactive data
vline_data = data.frame(x2 = rep(mark.th, 2), y2 = c(min(data$perf), max(data$perf)),
measure = obj$measures[1])
plt = ggvis::layer_paths(plt, ggvis::prop("x", as.name("x2")),
ggvis::prop("y", as.name("y2")),
ggvis::prop("stroke", "grey", scale = FALSE), data = vline_data)
}
plt = ggvis::add_axis(plt, "x", title = "threshold")
if (length(measures) > 1L)
plt = ggvis::add_axis(plt, "y", title = "measure")
else
plt = ggvis::add_axis(plt, "y", title = measures[1])
plt
}
if (!is.null(interaction)) {
ui = shiny::shinyUI(
shiny::pageWithSidebar(
shiny::headerPanel("Threshold vs. Performance"),
shiny::sidebarPanel(
shiny::selectInput("interaction_select",
paste("choose a", interaction),
levels(data[[interaction]]))
),
shiny::mainPanel(
shiny::uiOutput("ggvis_ui"),
ggvis::ggvisOutput("ggvis")
)
))
server = shiny::shinyServer(function(input, output) {
data_sub = shiny::reactive(data[which(data[[interaction]] == input$interaction_select), ])
plt = create_plot(data_sub, color, obj$measures)
ggvis::bind_shiny(plt, "ggvis", "ggvis_ui")
})
shiny::shinyApp(ui, server)
} else {
create_plot(data, color, obj$measures)
}
}