evalclass / precrec
Showing 33 of 269 files from the diff.
Other files ignored by Codecov
man/mmdata.Rd has changed.
man/auc.Rd has changed.
.gitignore has changed.
NAMESPACE has changed.
man/fortify.Rd has changed.
.Rbuildignore has changed.
man/precrec.Rd has changed.
tests/testthat.R has changed.
man/auc_ci.Rd has changed.
man/part.Rd has changed.
man/plot.Rd has changed.
README.md has changed.
NEWS.md has changed.
man/evalmod.Rd has changed.
man/autoplot.Rd has changed.
man/pauc.Rd has changed.
README.Rmd has changed.
DESCRIPTION has changed.
cran-comments.md has changed.

@@ -35,8 +35,10 @@
Loading
35 35
#' head(M2N50F5)
36 36
#'
37 37
#' ## Convert with format_nfold
38 -
#' nfold_list1 <- format_nfold(nfold_df = M2N50F5, score_cols = c(1, 2),
39 -
#'                             lab_col = 3, fold_col = 4)
38 +
#' nfold_list1 <- format_nfold(
39 +
#'   nfold_df = M2N50F5, score_cols = c(1, 2),
40 +
#'   lab_col = 3, fold_col = 4
41 +
#' )
40 42
#'
41 43
#' ## Show the list structure
42 44
#' str(nfold_list1)
@@ -49,8 +51,10 @@
Loading
49 51
#' ###
50 52
#'
51 53
#' ## Convert with format_nfold
52 -
#' nfold_list2 <- format_nfold(nfold_df = M2N50F5, score_cols = 1,
53 -
#'                             lab_col = 3, fold_col = 4)
54 +
#' nfold_list2 <- format_nfold(
55 +
#'   nfold_df = M2N50F5, score_cols = 1,
56 +
#'   lab_col = 3, fold_col = 4
57 +
#' )
54 58
#'
55 59
#' ## Show the list structure
56 60
#' str(nfold_list2)
@@ -63,9 +67,11 @@
Loading
63 67
#' ###
64 68
#'
65 69
#' ## Convert with format_nfold
66 -
#' nfold_list3 <- format_nfold(nfold_df = M2N50F5,
67 -
#'                            score_cols = c("score1", "score2"),
68 -
#'                            lab_col = "label", fold_col = "fold")
70 +
#' nfold_list3 <- format_nfold(
71 +
#'   nfold_df = M2N50F5,
72 +
#'   score_cols = c("score1", "score2"),
73 +
#'   lab_col = "label", fold_col = "fold"
74 +
#' )
69 75
#'
70 76
#' ## Show the list structure
71 77
#' str(nfold_list3)
@@ -74,7 +80,6 @@
Loading
74 80
#'
75 81
#' @export
76 82
format_nfold <- function(nfold_df, score_cols, lab_col, fold_col) {
77 -
78 83
  # Validate arguments
79 84
  .validate_format_nfold_args(nfold_df, score_cols, lab_col, fold_col)
80 85
@@ -113,8 +118,8 @@
Loading
113 118
#
114 119
# Validate arguments of format_nfold()
115 120
#
116 -
.validate_format_nfold_args <- function(nfold_df, score_cols, lab_col, fold_col) {
117 -
121 +
.validate_format_nfold_args <- function(nfold_df, score_cols,
122 +
                                        lab_col, fold_col) {
118 123
  if (!is.data.frame(nfold_df)) {
119 124
    stop("nfold_df must be a data frame.", call. = FALSE)
120 125
  }
@@ -127,6 +132,4 @@
Loading
127 132
128 133
  # Check fold column name
129 134
  .validate_fold_col(fold_col, nfold_df)
130 -
131 135
}
132 -

@@ -23,8 +23,8 @@
Loading
23 23
#' @return The \code{auc} function returns a data frame with AUC scores.
24 24
#'
25 25
#' @seealso \code{\link{evalmod}} for generating \code{S3} objects with
26 -
#'   performance evaluation measures. \code{\link{pauc}} for retrieving a dataset
27 -
#'   of pAUCs.
26 +
#'   performance evaluation measures. \code{\link{pauc}} for retrieving
27 +
#'   a dataset of pAUCs.
28 28
#'
29 29
#' @examples
30 30
#'
@@ -49,7 +49,8 @@
Loading
49 49
#' ## Create sample datasets with 100 positives and 100 negatives
50 50
#' samps <- create_sim_samples(1, 100, 100, "all")
51 51
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
52 -
#'                modnames = samps[["modnames"]])
52 +
#'   modnames = samps[["modnames"]]
53 +
#' )
53 54
#'
54 55
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
55 56
#' mscurves <- evalmod(mdat)
@@ -65,8 +66,9 @@
Loading
65 66
#' ## Create sample datasets with 100 positives and 100 negatives
66 67
#' samps <- create_sim_samples(4, 100, 100, "good_er")
67 68
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
68 -
#'                modnames = samps[["modnames"]],
69 -
#'                dsids = samps[["dsids"]])
69 +
#'   modnames = samps[["modnames"]],
70 +
#'   dsids = samps[["dsids"]]
71 +
#' )
70 72
#'
71 73
#' ## Generate an smcurve object that contains ROC and Precision-Recall curves
72 74
#' smcurves <- evalmod(mdat, raw_curves = TRUE)
@@ -90,8 +92,9 @@
Loading
90 92
#' ## Create sample datasets with 100 positives and 100 negatives
91 93
#' samps <- create_sim_samples(4, 100, 100, "all")
92 94
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
93 -
#'                modnames = samps[["modnames"]],
94 -
#'                dsids = samps[["dsids"]])
95 +
#'   modnames = samps[["modnames"]],
96 +
#'   dsids = samps[["dsids"]]
97 +
#' )
95 98
#'
96 99
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
97 100
#' mmcurves <- evalmod(mdat, raw_curves = TRUE)

@@ -4,7 +4,8 @@
Loading
4 4
#'   \code{\link{evalmod}} to a data frame.
5 5
#'
6 6
#' @param x An \code{S3} object generated by \code{\link{evalmod}}.
7 -
#'   The \code{as.data.frame} function takes one of the following \code{S3} objects.
7 +
#'   The \code{as.data.frame} function takes
8 +
#'     one of the following \code{S3} objects.
8 9
#'
9 10
#' \enumerate{
10 11
#'
@@ -63,7 +64,6 @@
Loading
63 64
#'   performance evaluation measures.
64 65
#'
65 66
#' @examples
66 -
#'
67 67
#' \dontrun{
68 68
#' ##################################################
69 69
#' ### Single model & single test dataset
@@ -82,8 +82,10 @@
Loading
82 82
#' head(sscurves.df)
83 83
#'
84 84
#' ## Generate an sspoints object that contains basic evaluation measures
85 -
#' sspoints <- evalmod(mode = "basic", scores = P10N10$scores,
86 -
#'                     labels = P10N10$labels)
85 +
#' sspoints <- evalmod(
86 +
#'   mode = "basic", scores = P10N10$scores,
87 +
#'   labels = P10N10$labels
88 +
#' )
87 89
#' ## Convert sspoints to a data frame
88 90
#' sspoints.df <- as.data.frame(sspoints)
89 91
#'
@@ -98,7 +100,8 @@
Loading
98 100
#' ## Create sample datasets with 100 positives and 100 negatives
99 101
#' samps <- create_sim_samples(1, 100, 100, "all")
100 102
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
101 -
#'                modnames = samps[["modnames"]])
103 +
#'   modnames = samps[["modnames"]]
104 +
#' )
102 105
#'
103 106
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
104 107
#' mscurves <- evalmod(mdat)
@@ -126,8 +129,9 @@
Loading
126 129
#' ## Create sample datasets with 100 positives and 100 negatives
127 130
#' samps <- create_sim_samples(10, 100, 100, "good_er")
128 131
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
129 -
#'                modnames = samps[["modnames"]],
130 -
#'                dsids = samps[["dsids"]])
132 +
#'   modnames = samps[["modnames"]],
133 +
#'   dsids = samps[["dsids"]]
134 +
#' )
131 135
#'
132 136
#' ## Generate an smcurve object that contains ROC and Precision-Recall curves
133 137
#' smcurves <- evalmod(mdat, raw_curves = TRUE)
@@ -155,8 +159,9 @@
Loading
155 159
#' ## Create sample datasets with 100 positives and 100 negatives
156 160
#' samps <- create_sim_samples(10, 100, 100, "all")
157 161
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
158 -
#'                modnames = samps[["modnames"]],
159 -
#'                dsids = samps[["dsids"]])
162 +
#'   modnames = samps[["modnames"]],
163 +
#'   dsids = samps[["dsids"]]
164 +
#' )
160 165
#'
161 166
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
162 167
#' mmcurves <- evalmod(mdat, raw_curves = TRUE)
@@ -185,9 +190,11 @@
Loading
185 190
#' data(M2N50F5)
186 191
#'
187 192
#' ## Speficy nessesary columns to create mdat
188 -
#' cvdat <- mmdata(nfold_df = M2N50F5, score_cols = c(1, 2),
189 -
#'                 lab_col = 3, fold_col = 4,
190 -
#'                 modnames = c("m1", "m2"), dsids = 1:5)
193 +
#' cvdat <- mmdata(
194 +
#'   nfold_df = M2N50F5, score_cols = c(1, 2),
195 +
#'   lab_col = 3, fold_col = 4,
196 +
#'   modnames = c("m1", "m2"), dsids = 1:5
197 +
#' )
191 198
#'
192 199
#' ## Generate an mmcurve object that contains ROC and Precision-Recall curves
193 200
#' cvcurves <- evalmod(cvdat)
@@ -214,8 +221,10 @@
Loading
214 221
#'
215 222
#' ## mode = "aucroc"
216 223
#' data(P10N10)
217 -
#' uauc1 <- evalmod(scores = P10N10$scores, labels = P10N10$labels,
218 -
#'                  mode="aucroc")
224 +
#' uauc1 <- evalmod(
225 +
#'   scores = P10N10$scores, labels = P10N10$labels,
226 +
#'   mode = "aucroc"
227 +
#' )
219 228
#'
220 229
#' # as.data.frame 'aucroc'
221 230
#' as.data.frame(uauc1)
@@ -223,9 +232,10 @@
Loading
223 232
#' ## mode = "aucroc"
224 233
#' samps <- create_sim_samples(10, 100, 100, "all")
225 234
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
226 -
#'                modnames = samps[["modnames"]],
227 -
#'                dsids = samps[["dsids"]])
228 -
#' uauc2 <- evalmod(mdat, mode="aucroc")
235 +
#'   modnames = samps[["modnames"]],
236 +
#'   dsids = samps[["dsids"]]
237 +
#' )
238 +
#' uauc2 <- evalmod(mdat, mode = "aucroc")
229 239
#'
230 240
#' # as.data.frame 'aucroc'
231 241
#' head(as.data.frame(uauc2))
@@ -271,51 +281,61 @@
Loading
271 281
    } else {
272 282
      x_bins <- 0
273 283
    }
274 -
275 284
  } else if (new_mode == "basic") {
276 -
    curvetype_names <- list(score = "score", label = "label", error = "err",
277 -
                            accuracy = "acc", specificity = "sp",
278 -
                            sensitivity = "sn", precision = "prec", mcc = "mcc",
279 -
                            fscore = "fscore")
285 +
    curvetype_names <- list(
286 +
      score = "score", label = "label", error = "err",
287 +
      accuracy = "acc", specificity = "sp",
288 +
      sensitivity = "sn", precision = "prec", mcc = "mcc",
289 +
      fscore = "fscore"
290 +
    )
280 291
    x_bins <- 0
281 292
  }
282 293
283 294
  # Make dsis-modname pairs
284 295
  dsid_modnames <- paste(attr(obj, "data_info")$modnames,
285 -
                         attr(obj, "data_info")$dsids, sep = ":")
296 +
    attr(obj, "data_info")$dsids,
297 +
    sep = ":"
298 +
  )
286 299
287 300
  # Create curve_df
288 301
  if (raw_curves) {
289 302
    if (use_rcpp) {
290 -
      list_df <- convert_curve_df(obj, uniq_modnames, as.character(uniq_dsids),
291 -
                                  match(modnames, uniq_modnames),
292 -
                                  match(dsids, uniq_dsids),
293 -
                                  dsid_modnames, curvetype_names, x_bins)
303 +
      list_df <- convert_curve_df(
304 +
        obj, uniq_modnames, as.character(uniq_dsids),
305 +
        match(modnames, uniq_modnames),
306 +
        match(dsids, uniq_dsids),
307 +
        dsid_modnames, curvetype_names, x_bins
308 +
      )
294 309
      .check_cpp_func_error(list_df, "convert_curve_df")
295 310
      curve_df <- list_df[["df"]]
296 311
    } else {
297 -
      curve_df <- .dataframe_curve(obj, uniq_modnames, uniq_dsids, modnames,
298 -
                                   dsids, dsid_modnames, curvetype_names)
312 +
      curve_df <- .dataframe_curve(
313 +
        obj, uniq_modnames, uniq_dsids, modnames,
314 +
        dsids, dsid_modnames, curvetype_names
315 +
      )
299 316
      warning("R version of .dataframe_common is used")
300 317
    }
301 318
  } else {
302 319
    if (use_rcpp) {
303 -
      list_df <- convert_curve_avg_df(attr(obj, "grp_avg"), uniq_modnames,
304 -
                                      match(modnames, uniq_modnames),
305 -
                                      curvetype_names, x_bins)
320 +
      list_df <- convert_curve_avg_df(
321 +
        attr(obj, "grp_avg"), uniq_modnames,
322 +
        match(modnames, uniq_modnames),
323 +
        curvetype_names, x_bins
324 +
      )
306 325
      .check_cpp_func_error(list_df, "convert_curve_avg_df")
307 326
      curve_df <- list_df[["df"]]
308 327
    } else {
309 -
      curve_df <- .dataframe_curve_avg(obj, uniq_modnames, uniq_dsids, modnames,
310 -
                                       dsids, dsid_modnames, curvetype_names)
328 +
      curve_df <- .dataframe_curve_avg(
329 +
        obj, uniq_modnames, uniq_dsids, modnames,
330 +
        dsids, dsid_modnames, curvetype_names
331 +
      )
311 332
      warning("R version of .dataframe_common is used")
312 333
    }
313 -
314 334
  }
315 335
316 336
  if (!check_ggplot) {
317 337
    if ("dsid_modname" %in% names(curve_df)) {
318 -
      curve_df <- with(curve_df, subset(curve_df, select = -dsid_modname))
338 +
      curve_df[["dsid_modname"]] <- NULL
319 339
    }
320 340
    colnum <- ncol(curve_df)
321 341
    names(curve_df) <- c(names(curve_df)[1:(colnum - 1)], "type")
@@ -329,7 +349,6 @@
Loading
329 349
#
330 350
.dataframe_curve <- function(obj, uniq_modnames, uniq_dsids, modnames, dsids,
331 351
                             dsid_modnames, curvetype_names) {
332 -
333 352
  curve_df <- NULL
334 353
  for (curvetype in names(curvetype_names)) {
335 354
    curves <- obj[[curvetype_names[[curvetype]]]]
@@ -339,15 +358,22 @@
Loading
339 358
340 359
      modname <- factor(rep(modnames[i], length(x)), levels = uniq_modnames)
341 360
      dsid <- factor(rep(dsids[i], length(x)), levels = uniq_dsids)
342 -
      dsid_modname <- factor(rep(paste(modnames[i], dsids[i], sep = ":"),
343 -
                                 length(x)),
344 -
                             levels = dsid_modnames)
361 +
      dsid_modname <- factor(
362 +
        rep(
363 +
          paste(modnames[i], dsids[i], sep = ":"),
364 +
          length(x)
365 +
        ),
366 +
        levels = dsid_modnames
367 +
      )
345 368
      curvename <- factor(rep(curvetype, length(x)),
346 -
                          levels = names(curvetype_names))
347 -
      curve_df <- rbind(curve_df, data.frame(x = x, y = y, modname = modname,
348 -
                                             dsid = dsid,
349 -
                                             dsid_modname = dsid_modname,
350 -
                                             curvetype = curvename))
369 +
        levels = names(curvetype_names)
370 +
      )
371 +
      curve_df <- rbind(curve_df, data.frame(
372 +
        x = x, y = y, modname = modname,
373 +
        dsid = dsid,
374 +
        dsid_modname = dsid_modname,
375 +
        curvetype = curvename
376 +
      ))
351 377
    }
352 378
  }
353 379
@@ -357,9 +383,8 @@
Loading
357 383
#
358 384
# Make a dataframe for plotting with average curves
359 385
#
360 -
.dataframe_curve_avg <- function(obj, uniq_modnames, uniq_dsids, modnames, dsids,
361 -
                               dsid_modnames, curvetype_names) {
362 -
386 +
.dataframe_curve_avg <- function(obj, uniq_modnames, uniq_dsids, modnames,
387 +
                                 dsids, dsid_modnames, curvetype_names) {
363 388
  grp_avg <- attr(obj, "grp_avg")
364 389
  curve_df <- NULL
365 390
  for (curvetype in names(curvetype_names)) {
@@ -372,13 +397,17 @@
Loading
372 397
      ymax <- avgcurves[[i]][["y_ci_h"]]
373 398
374 399
      modname <- factor(rep(uniq_modnames[i], length(x)),
375 -
                        levels = uniq_modnames)
400 +
        levels = uniq_modnames
401 +
      )
376 402
      curvename <- factor(rep(curvetype, length(x)),
377 -
                          levels = names(curvetype_names))
378 -
      curve_df <- rbind(curve_df, data.frame(x = x, y = y,
379 -
                                             ymin = ymin, ymax = ymax,
380 -
                                             modname = modname,
381 -
                                             curvetype = curvename))
403 +
        levels = names(curvetype_names)
404 +
      )
405 +
      curve_df <- rbind(curve_df, data.frame(
406 +
        x = x, y = y,
407 +
        ymin = ymin, ymax = ymax,
408 +
        modname = modname,
409 +
        curvetype = curvename
410 +
      ))
382 411
    }
383 412
  }
384 413
@@ -389,7 +418,6 @@
Loading
389 418
# Process ... for curve objects
390 419
#
391 420
.get_dataframe_arglist <- function(evalmod_args, def_raw_curves, ...) {
392 -
393 421
  arglist <- list(...)
394 422
395 423
  if (is.null(arglist[["raw_curves"]])) {
@@ -404,4 +432,3 @@
Loading
404 432
405 433
  arglist
406 434
}
407 -

@@ -160,8 +160,10 @@
Loading
160 160
#' autoplot(sscurves, curvetype = "PRC")
161 161
#'
162 162
#' ## Generate an sspoints object that contains basic evaluation measures
163 -
#' sspoints <- evalmod(mode = "basic", scores = P10N10$scores,
164 -
#'                     labels = P10N10$labels)
163 +
#' sspoints <- evalmod(
164 +
#'   mode = "basic", scores = P10N10$scores,
165 +
#'   labels = P10N10$labels
166 +
#' )
165 167
#'
166 168
#' ## Normalized ranks vs. basic evaluation measures
167 169
#' autoplot(sspoints)
@@ -177,7 +179,8 @@
Loading
177 179
#' ## Create sample datasets with 100 positives and 100 negatives
178 180
#' samps <- create_sim_samples(1, 100, 100, "all")
179 181
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
180 -
#'                modnames = samps[["modnames"]])
182 +
#'   modnames = samps[["modnames"]]
183 +
#' )
181 184
#'
182 185
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
183 186
#' mscurves <- evalmod(mdat)
@@ -215,8 +218,9 @@
Loading
215 218
#' ## Create sample datasets with 100 positives and 100 negatives
216 219
#' samps <- create_sim_samples(10, 100, 100, "good_er")
217 220
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
218 -
#'                modnames = samps[["modnames"]],
219 -
#'                dsids = samps[["dsids"]])
221 +
#'   modnames = samps[["modnames"]],
222 +
#'   dsids = samps[["dsids"]]
223 +
#' )
220 224
#'
221 225
#' ## Generate an smcurve object that contains ROC and Precision-Recall curves
222 226
#' smcurves <- evalmod(mdat, raw_curves = TRUE)
@@ -255,8 +259,9 @@
Loading
255 259
#' ## Create sample datasets with 100 positives and 100 negatives
256 260
#' samps <- create_sim_samples(10, 100, 100, "all")
257 261
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
258 -
#'                modnames = samps[["modnames"]],
259 -
#'                dsids = samps[["dsids"]])
262 +
#'   modnames = samps[["modnames"]],
263 +
#'   dsids = samps[["dsids"]]
264 +
#' )
260 265
#'
261 266
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
262 267
#' mmcurves <- evalmod(mdat, raw_curves = TRUE)
@@ -272,8 +277,10 @@
Loading
272 277
#'
273 278
#' ## Reduced/Full supporting points
274 279
#' sampmm <- create_sim_samples(4, 5000, 5000)
275 -
#' mdatmm <- mmdata(sampmm$scores, sampmm$labels, modnames = c("m1", "m2"),
276 -
#'                  dsids = c(1, 2), expd_first = "modnames")
280 +
#' mdatmm <- mmdata(sampmm$scores, sampmm$labels,
281 +
#'   modnames = c("m1", "m2"),
282 +
#'   dsids = c(1, 2), expd_first = "modnames"
283 +
#' )
277 284
#' evalmm <- evalmod(mdatmm, raw_curves = TRUE)
278 285
#'
279 286
#' # Reduced supporting point
@@ -297,9 +304,11 @@
Loading
297 304
#' data(M2N50F5)
298 305
#'
299 306
#' ## Speficy nessesary columns to create mdat
300 -
#' cvdat <- mmdata(nfold_df = M2N50F5, score_cols = c(1, 2),
301 -
#'                 lab_col = 3, fold_col = 4,
302 -
#'                 modnames = c("m1", "m2"), dsids = 1:5)
307 +
#' cvdat <- mmdata(
308 +
#'   nfold_df = M2N50F5, score_cols = c(1, 2),
309 +
#'   lab_col = 3, fold_col = 4,
310 +
#'   modnames = c("m1", "m2"), dsids = 1:5
311 +
#' )
303 312
#'
304 313
#' ## Generate an mmcurve object that contains ROC and Precision-Recall curves
305 314
#' cvcurves <- evalmod(cvdat)
@@ -315,7 +324,6 @@
Loading
315 324
#'
316 325
#' ## Normalized ranks vs. average basic evaluation measures
317 326
#' autoplot(cvpoints)
318 -
#'
319 327
#' }
320 328
#'
321 329
#' @name autoplot
@@ -329,7 +337,6 @@
Loading
329 337
                                  def_raw_curves, def_add_np_nn,
330 338
                                  def_show_legend, def_ret_grob,
331 339
                                  def_reduce_points, def_multiplot_lib, ...) {
332 -
333 340
  arglist <- list(...)
334 341
335 342
  if (is.null(arglist[["curvetype"]])) {
@@ -345,7 +352,8 @@
Loading
345 352
  }
346 353
  if (!evalmod_args[["calc_avg"]] && arglist[["show_cb"]]) {
347 354
    stop("Invalid show_cb. Inconsistent with calc_avg of evalmod.",
348 -
         call. = FALSE)
355 +
      call. = FALSE
356 +
    )
349 357
  }
350 358
351 359
  if (is.null(arglist[["raw_curves"]])) {
@@ -359,7 +367,8 @@
Loading
359 367
  }
360 368
  if (!evalmod_args[["raw_curves"]] && arglist[["raw_curves"]]) {
361 369
    stop("Invalid raw_curves. Inconsistent with the value of evalmod.",
362 -
         call. = FALSE)
370 +
      call. = FALSE
371 +
    )
363 372
  }
364 373
365 374
  if (is.null(arglist[["add_np_nn"]])) {
@@ -413,9 +422,13 @@
Loading
413 422
#
414 423
.load_ggplot2 <- function() {
415 424
  if (!requireNamespace("ggplot2", quietly = TRUE)) {
416 -
    stop(paste("ggplot2 is required to perform this function.",
417 -
               "Please install it."),
418 -
         call. = FALSE)
425 +
    stop(
426 +
      paste(
427 +
        "ggplot2 is required to perform this function.",
428 +
        "Please install it."
429 +
      ),
430 +
      call. = FALSE
431 +
    )
419 432
  }
420 433
}
421 434
@@ -425,17 +438,19 @@
Loading
425 438
.load_grid <- function() {
426 439
  if (!requireNamespace("grid", quietly = TRUE)) {
427 440
    stop("grid is required to perform this function. Please install it.",
428 -
         call. = FALSE)
441 +
      call. = FALSE
442 +
    )
429 443
  }
430 444
}
431 445
432 446
#
433 447
# Load gridExtra
434 448
#
435 -
.load_gridExtra <- function() {
449 +
.load_gridextra <- function() {
436 450
  if (!requireNamespace("gridExtra", quietly = TRUE)) {
437 451
    stop("gridExtra is required to perform this function. Please install it.",
438 -
         call. = FALSE)
452 +
      call. = FALSE
453 +
    )
439 454
  }
440 455
}
441 456
@@ -446,8 +461,13 @@
Loading
446 461
  if (requireNamespace("patchwork", quietly = TRUE)) {
447 462
    return(TRUE)
448 463
  } else {
449 -
    warning("patchwork is not installed. grid and gridExtra will be used instead.",
450 -
            call. = FALSE)
464 +
    warning(
465 +
      paste0(
466 +
        "patchwork is not installed. ",
467 +
        "grid and gridExtra will be used instead."
468 +
      ),
469 +
      call. = FALSE
470 +
    )
451 471
    return(FALSE)
452 472
  }
453 473
}
@@ -466,17 +486,16 @@
Loading
466 486
467 487
  show_cb <- arglist[["show_cb"]]
468 488
  if (!attr(object, "args")$calc_avg) {
469 -
    show_cb = FALSE
489 +
    show_cb <- FALSE
470 490
  }
471 491
472 492
  raw_curves <- arglist[["raw_curves"]]
473 493
  if (show_cb) {
474 -
    raw_curves = FALSE
494 +
    raw_curves <- FALSE
475 495
  }
476 496
477 497
  # === Check package availability  ===
478 498
  .load_ggplot2()
479 -
  avail_pathwork <- .load_ggplot2()
480 499
  .validate(object)
481 500
  .check_curvetype(curvetype, object)
482 501
  .check_type(type)
@@ -488,22 +507,29 @@
Loading
488 507
  .check_multiplot_lib(multiplot_lib)
489 508
490 509
  # === Create a ggplot object for ROC&PRC, ROC, or PRC ===
491 -
  curve_df <- ggplot2::fortify(object, raw_curves = raw_curves,
492 -
                               reduce_points = reduce_points)
510 +
  curve_df <- ggplot2::fortify(object,
511 +
    raw_curves = raw_curves,
512 +
    reduce_points = reduce_points
513 +
  )
493 514
494 515
  func_plot <- function(ctype) {
495 -
    .autoplot_single(object, curve_df, curvetype = ctype, type = type,
496 -
                     show_cb = show_cb, raw_curves = raw_curves,
497 -
                     reduce_points = reduce_points, show_legend = show_legend,
498 -
                     add_np_nn = add_np_nn)
516 +
    .autoplot_single(object, curve_df,
517 +
      curvetype = ctype, type = type,
518 +
      show_cb = show_cb, raw_curves = raw_curves,
519 +
      reduce_points = reduce_points, show_legend = show_legend,
520 +
      add_np_nn = add_np_nn
521 +
    )
499 522
  }
500 523
  lcurves <- lapply(curvetype, func_plot)
501 524
  names(lcurves) <- curvetype
502 525
503 526
  if (length(lcurves) > 1) {
504 -
    do.call(.combine_plots, c(lcurves, show_legend = show_legend,
505 -
                              ret_grob = ret_grob, multiplot_lib = multiplot_lib,
506 -
                              nplots = length(lcurves)))
527 +
    do.call(.combine_plots, c(lcurves,
528 +
      show_legend = show_legend,
529 +
      ret_grob = ret_grob,
530 +
      multiplot_lib = multiplot_lib,
531 +
      nplots = length(lcurves)
532 +
    ))
507 533
  } else {
508 534
    lcurves[[1]]
509 535
  }
@@ -520,7 +546,7 @@
Loading
520 546
  plots <- list(...)
521 547
522 548
  g <- ggplot2::ggplotGrob(plots[[1]]
523 -
                           + ggplot2::theme(legend.position = "bottom"))$grobs
549 +
  + ggplot2::theme(legend.position = "bottom"))$grobs
524 550
  legend <- g[[which(lapply(g, function(x) x$name) == "guide-box")]]
525 551
  lheight <- sum(legend$height)
526 552
@@ -531,7 +557,8 @@
Loading
531 557
    do.call(fncol, lapply(plots, fnolegend)),
532 558
    legend,
533 559
    heights = grid::unit.c(grid::unit(1, "npc") - lheight, lheight),
534 -
    ncol = 1)
560 +
    ncol = 1
561 +
  )
535 562
}
536 563
537 564
#
@@ -574,7 +601,7 @@
Loading
574 601
  if (show_legend) {
575 602
    p <- p + patchwork::plot_layout(guides = "collect")
576 603
    if (length(plotlist) > 2) {
577 -
      p <- p + ggplot2::theme(legend.position = 'bottom')
604 +
      p <- p + ggplot2::theme(legend.position = "bottom")
578 605
    }
579 606
  }
580 607
@@ -594,8 +621,11 @@
Loading
594 621
  }
595 622
  if (multiplot_lib == "grid") {
596 623
    .load_grid()
597 -
    .load_gridExtra()
598 -
    .combine_plots_grid(..., show_legend = show_legend, ret_grob = ret_grob, nplots = nplots)
624 +
    .load_gridextra()
625 +
    .combine_plots_grid(...,
626 +
      show_legend = show_legend,
627 +
      ret_grob = ret_grob, nplots = nplots
628 +
    )
599 629
  }
600 630
}
601 631
@@ -606,40 +636,65 @@
Loading
606 636
                             show_cb = FALSE, raw_curves = FALSE,
607 637
                             reduce_points = TRUE, show_legend = FALSE,
608 638
                             add_np_nn = TRUE, ...) {
609 -
610 -
  curve_df <- .prepare_autoplot(object, curve_df = curve_df,
611 -
                                curvetype = curvetype,
612 -
                                raw_curves = raw_curves,
613 -
                                reduce_points = reduce_points, ...)
639 +
  curve_df <- .prepare_autoplot(object,
640 +
    curve_df = curve_df,
641 +
    curvetype = curvetype,
642 +
    raw_curves = raw_curves,
643 +
    reduce_points = reduce_points, ...
644 +
  )
614 645
615 646
  # === Create a ggplot object ===
647 +
  x_col <- rlang::sym("x")
648 +
  y_col <- rlang::sym("y")
649 +
  ymin_col <- rlang::sym("ymin")
650 +
  ymax_col <- rlang::sym("ymax")
651 +
  modname_col <- rlang::sym("modname")
652 +
  dsid_modname_col <- rlang::sym("dsid_modname")
616 653
  if (show_cb) {
617 -
    p <- ggplot2::ggplot(curve_df,
618 -
                         ggplot2::aes_string(x = 'x', y = 'y',
619 -
                                             ymin = 'ymin', ymax = 'ymax'))
654 +
    p <- ggplot2::ggplot(
655 +
      curve_df,
656 +
      ggplot2::aes(
657 +
        x = !!x_col, y = !!y_col,
658 +
        ymin = !!ymin_col, ymax = !!ymax_col
659 +
      )
660 +
    )
620 661
    if (type == "l") {
621 -
      p <- p + ggplot2::geom_smooth(ggplot2::aes_string(color = 'modname'),
622 -
                                    stat = "identity", na.rm = TRUE,
623 -
                                    size = 0.5)
662 +
      p <- p + ggplot2::geom_smooth(ggplot2::aes(color = !!modname_col),
663 +
        stat = "identity", na.rm = TRUE,
664 +
        linewidth = 0.5
665 +
      )
624 666
    } else if (type == "b" || type == "p") {
625 -
      p <- p + ggplot2::geom_ribbon(ggplot2::aes_string(ymin = 'ymin',
626 -
                                                        ymax = 'ymax',
627 -
                                                        group = 'modname'),
628 -
                                    stat = "identity", alpha = 0.25,
629 -
                                    fill = "grey25", na.rm = TRUE)
667 +
      p <- p + ggplot2::geom_ribbon(
668 +
        ggplot2::aes(
669 +
          ymin = !!ymin_col,
670 +
          ymax = !!ymax_col,
671 +
          group = !!modname_col
672 +
        ),
673 +
        stat = "identity", alpha = 0.25,
674 +
        fill = "grey25", na.rm = TRUE
675 +
      )
630 676
      if (type == "b") {
631 -
        p <- p + ggplot2::geom_line(ggplot2::aes_string(color = 'modname'),
632 -
                                    alpha = 0.25, na.rm = TRUE)
677 +
        p <- p + ggplot2::geom_line(ggplot2::aes(color = !!modname_col),
678 +
          alpha = 0.25, na.rm = TRUE
679 +
        )
633 680
      }
634 -
      p <- p + ggplot2::geom_point(ggplot2::aes_string(x = 'x', y = 'y',
635 -
                                                       color = 'modname'),
636 -
                                   na.rm = TRUE)
681 +
      p <- p + ggplot2::geom_point(
682 +
        ggplot2::aes(
683 +
          x = !!x_col, y = !!y_col,
684 +
          color = !!modname_col
685 +
        ),
686 +
        na.rm = TRUE
687 +
      )
637 688
    }
638 689
  } else if (raw_curves) {
639 -
    p <- ggplot2::ggplot(curve_df,
640 -
                         ggplot2::aes_string(x = 'x', y = 'y',
641 -
                                             group = 'dsid_modname',
642 -
                                             color = 'modname'))
690 +
    p <- ggplot2::ggplot(
691 +
      curve_df,
692 +
      ggplot2::aes(
693 +
        x = !!x_col, y = !!y_col,
694 +
        group = !!dsid_modname_col,
695 +
        color = !!modname_col
696 +
      )
697 +
    )
643 698
644 699
    if (type == "l") {
645 700
      p <- p + ggplot2::geom_line(na.rm = TRUE)
@@ -649,10 +704,11 @@
Loading
649 704
      }
650 705
      p <- p + ggplot2::geom_point(na.rm = TRUE)
651 706
    }
652 -
653 707
  } else {
654 -
    p <- ggplot2::ggplot(curve_df, ggplot2::aes_string(x = 'x', y = 'y',
655 -
                                                       color = 'modname'))
708 +
    p <- ggplot2::ggplot(curve_df, ggplot2::aes(
709 +
      x = !!x_col, y = !!y_col,
710 +
      color = !!modname_col
711 +
    ))
656 712
    if (type == "l") {
657 713
      p <- p + ggplot2::geom_line(na.rm = TRUE)
658 714
    } else if (type == "b" || type == "p") {
@@ -692,13 +748,15 @@
Loading
692 748
  }
693 749
  if (curvetype == "ROC" || curvetype == "PRC") {
694 750
    if (all(xlim == ylim)) {
695 -
      ratio = 1
751 +
      ratio <- 1
696 752
    } else {
697 -
      ratio = NULL
753 +
      ratio <- NULL
698 754
    }
699 755
  }
700 -
  p <- func_g(p, object, show_legend = show_legend, add_np_nn = add_np_nn,
701 -
              curve_df = curve_df, xlim = xlim, ylim = ylim, ratio = ratio, ...)
756 +
  p <- func_g(p, object,
757 +
    show_legend = show_legend, add_np_nn = add_np_nn,
758 +
    curve_df = curve_df, xlim = xlim, ylim = ylim, ratio = ratio, ...
759 +
  )
702 760
703 761
  p
704 762
}
@@ -736,7 +794,6 @@
Loading
736 794
#
737 795
.geom_basic_roc <- function(p, object, show_legend = TRUE, add_np_nn = TRUE,
738 796
                            xlim, ylim, ratio, ...) {
739 -
740 797
  pn_info <- .get_pn_info(object)
741 798
742 799
  if (add_np_nn && pn_info$is_consistant) {
@@ -745,8 +802,10 @@
Loading
745 802
    main <- "ROC"
746 803
  }
747 804
748 -
  p <- p + ggplot2::geom_abline(intercept = 0, slope = 1, colour = "grey",
749 -
                                linetype = 3)
805 +
  p <- p + ggplot2::geom_abline(
806 +
    intercept = 0, slope = 1, colour = "grey",
807 +
    linetype = 3
808 +
  )
750 809
  p <- .set_coords(p, xlim, ylim, ratio)
751 810
  p <- .geom_basic(p, main, "1 - Specificity", "Sensitivity", show_legend)
752 811
@@ -758,7 +817,6 @@
Loading
758 817
#
759 818
.geom_basic_prc <- function(p, object, show_legend = TRUE, add_np_nn = TRUE,
760 819
                            xlim, ylim, ratio, ...) {
761 -
762 820
  pn_info <- .get_pn_info(object)
763 821
764 822
  if (add_np_nn && pn_info$is_consistant) {
@@ -767,8 +825,10 @@
Loading
767 825
    main <- "Precision-Recall"
768 826
  }
769 827
770 -
  p <- p + ggplot2::geom_hline(yintercept = pn_info$prc_base, colour = "grey",
771 -
                               linetype = 3)
828 +
  p <- p + ggplot2::geom_hline(
829 +
    yintercept = pn_info$prc_base, colour = "grey",
830 +
    linetype = 3
831 +
  )
772 832
  p <- .set_coords(p, xlim, ylim, ratio)
773 833
  p <- .geom_basic(p, main, "Recall", "Precision", show_legend)
774 834
@@ -779,8 +839,7 @@
Loading
779 839
# Set coordinates for ROC and precision-recall
780 840
#
781 841
.set_coords <- function(p, xlim, ylim, ratio) {
782 -
783 -
  if (is.null(ratio))  {
842 +
  if (is.null(ratio)) {
784 843
    p <- p + ggplot2::coord_cartesian(xlim = xlim, ylim = ylim)
785 844
  } else {
786 845
    p <- p + ggplot2::coord_fixed(ratio = ratio, xlim = xlim, ylim = ylim)
@@ -794,14 +853,13 @@
Loading
794 853
#
795 854
.geom_basic_point <- function(p, object, show_legend = TRUE,
796 855
                              curve_df = curve_df, xlim, ylim, ratio, ...) {
797 -
798 856
  s <- curve_df[["curvetype"]][1]
799 857
  if (s == "mcc") {
800 858
    main <- "MCC"
801 859
  } else if (s == "label") {
802 860
    main <- "Label (1:pos, -1:neg)"
803 861
  } else {
804 -
    main <- paste0(toupper(substring(s, 1, 1)), substring(s,2))
862 +
    main <- paste0(toupper(substring(s, 1, 1)), substring(s, 2))
805 863
  }
806 864
  p <- .set_coords(p, xlim, ylim, ratio)
807 865
  p <- .geom_basic(p, main, "normalized rank", s, show_legend)

@@ -4,7 +4,6 @@
Loading
4 4
.pl_main_basic <- function(mdat, model_type, dataset_type, class_name_pf,
5 5
                           calc_avg = TRUE, cb_alpha = 0.05,
6 6
                           raw_curves = FALSE) {
7 -
8 7
  if (dataset_type == "single") {
9 8
    calc_avg <- FALSE
10 9
    raw_curves <- TRUE
@@ -19,26 +18,35 @@
Loading
19 18
      } else {
20 19
        cl <- "negative"
21 20
      }
22 -
      err_msg <- paste0("Basic measures cannot be calculated. ",
23 -
                        "Only a single class (", cl, ") ",
24 -
                        "found in dataset (modname: ", attr(mdat[[s]], "modname"),
25 -
                        ", dsid: ",attr(mdat[[s]], "dsid"), ").")
21 +
      err_msg <- paste0(
22 +
        "Basic measures cannot be calculated. ",
23 +
        "Only a single class (", cl, ") ",
24 +
        "found in dataset (modname: ",
25 +
        attr(mdat[[s]], "modname"),
26 +
        ", dsid: ", attr(mdat[[s]], "dsid"), ")."
27 +
      )
26 28
      stop(err_msg, call. = FALSE)
27 29
    }
28 30
    cdat <- create_confmats(mdat[[s]], keep_fmdat = TRUE)
29 -
    pevals <- calc_measures(cdat)
31 +
    calc_measures(cdat)
30 32
  }
31 33
  lpoints <- lapply(seq_along(mdat), plfunc)
32 34
33 35
  # Summarize points by evaluation measure
34 36
  grpfunc <- function(m) {
35 -
    .summarize_points(lpoints, m, "pointgrp", mdat, dataset_type,
36 -
                      calc_avg, cb_alpha)
37 +
    .summarize_points(
38 +
      lpoints, m, "pointgrp", mdat, dataset_type,
39 +
      calc_avg, cb_alpha
40 +
    )
37 41
  }
38 -
  eval_names <- c("score", "label", "error", "accuracy", "specificity",
39 -
                  "sensitivity", "precision", "mcc", "fscore")
40 -
  grp_row_names <- c("score", "label", "err", "acc", "sp", "sn", "prec", "mcc",
41 -
                     "fscore")
42 +
  eval_names <- c(
43 +
    "score", "label", "error", "accuracy", "specificity",
44 +
    "sensitivity", "precision", "mcc", "fscore"
45 +
  )
46 +
  grp_row_names <- c(
47 +
    "score", "label", "err", "acc", "sp", "sn", "prec", "mcc",
48 +
    "fscore"
49 +
  )
42 50
  grp_points <- lapply(eval_names, grpfunc)
43 51
  names(grp_points) <- grp_row_names
44 52
@@ -60,8 +68,10 @@
Loading
60 68
    grp_points <- lapply(eval_names, grpfunc3)
61 69
    names(grp_points) <- grp_row_names
62 70
  }
63 -
  s3obj <- structure(grp_points, class = c(paste0(class_name_pf, "points"),
64 -
                                           "beval_info"))
71 +
  s3obj <- structure(grp_points, class = c(
72 +
    paste0(class_name_pf, "points"),
73 +
    "beval_info"
74 +
  ))
65 75
66 76
  # Set attributes
67 77
  attr(s3obj, "eval_summary") <- eval_summary
@@ -71,10 +81,12 @@
Loading
71 81
  attr(s3obj, "uniq_dsids") <- attr(mdat, "uniq_dsids")
72 82
  attr(s3obj, "model_type") <- model_type
73 83
  attr(s3obj, "dataset_type") <- dataset_type
74 -
  attr(s3obj, "args") <- list(mode = "basic",
75 -
                              calc_avg = calc_avg,
76 -
                              cb_alpha = cb_alpha,
77 -
                              raw_curves = raw_curves)
84 +
  attr(s3obj, "args") <- list(
85 +
    mode = "basic",
86 +
    calc_avg = calc_avg,
87 +
    cb_alpha = cb_alpha,
88 +
    raw_curves = raw_curves
89 +
  )
78 90
  attr(s3obj, "validated") <- FALSE
79 91
80 92
  # Call .validate.class_name()
@@ -86,12 +98,13 @@
Loading
86 98
#
87 99
.summarize_points <- function(lpoints, eval_type, class_name, mdat,
88 100
                              dataset_type, calc_avg, cb_alpha) {
89 -
90 101
  if (!is.null(lpoints)) {
91 102
    # Summarize basic evaluation measures
92 103
    grp_func <- function(s) {
93 -
      list(x = lpoints[[s]][["basic"]][["rank"]],
94 -
           y = lpoints[[s]][["basic"]][[eval_type]])
104 +
      list(
105 +
        x = lpoints[[s]][["basic"]][["rank"]],
106 +
        y = lpoints[[s]][["basic"]][[eval_type]]
107 +
      )
95 108
    }
96 109
    pevals <- lapply(seq_along(lpoints), grp_func)
97 110
@@ -130,24 +143,27 @@
Loading
130 143
# Summarize basic evaluation measures
131 144
#
132 145
.summarize_basic <- function(lpoints, mdat) {
133 -
134 146
  # Summarize AUC of ROC or PRC curves
135 147
  modnames <- attr(mdat, "data_info")[["modnames"]]
136 148
  dsids <- attr(mdat, "data_info")[["dsids"]]
137 -
  evaltypes <- c("rank", "score", "label", "error", "accuracy",
138 -
                 "specificity","sensitivity", "precision", "mcc", "fscore")
149 +
  evaltypes <- c(
150 +
    "rank", "score", "label", "error", "accuracy",
151 +
    "specificity", "sensitivity", "precision", "mcc", "fscore"
152 +
  )
139 153
  elen <- length(evaltypes)
140 154
141 -
  sbasic <- data.frame(modnames = rep(modnames, each = elen),
142 -
                       dsids = rep(dsids, each = elen),
143 -
                       evaltypes = rep(evaltypes, length(modnames)),
144 -
                       minvals = rep(NA, length(modnames) * elen),
145 -
                       q25vals = rep(NA, length(modnames) * elen),
146 -
                       medianvals = rep(NA, length(modnames) * elen),
147 -
                       meanvals = rep(NA, length(modnames) * elen),
148 -
                       q75vals = rep(NA, length(modnames) * elen),
149 -
                       maxvals = rep(NA, length(modnames) * elen),
150 -
                       stringsAsFactors = FALSE)
155 +
  sbasic <- data.frame(
156 +
    modnames = rep(modnames, each = elen),
157 +
    dsids = rep(dsids, each = elen),
158 +
    evaltypes = rep(evaltypes, length(modnames)),
159 +
    minvals = rep(NA, length(modnames) * elen),
160 +
    q25vals = rep(NA, length(modnames) * elen),
161 +
    medianvals = rep(NA, length(modnames) * elen),
162 +
    meanvals = rep(NA, length(modnames) * elen),
163 +
    q75vals = rep(NA, length(modnames) * elen),
164 +
    maxvals = rep(NA, length(modnames) * elen),
165 +
    stringsAsFactors = FALSE
166 +
  )
151 167
152 168
  for (i in seq_along(lpoints)) {
153 169
    for (j in seq_along(evaltypes)) {
@@ -169,14 +185,20 @@
Loading
169 185
  }
170 186
171 187
  # Validate class items and attributes
172 -
  item_names <- c("score", "label", "err", "acc", "sp", "sn", "prec", "mcc",
173 -
                  "fscore")
174 -
  attr_names <- c("eval_summary", "grp_avg", "data_info", "uniq_modnames",
175 -
                  "uniq_dsids", "model_type", "dataset_type", "args",
176 -
                  "validated")
188 +
  item_names <- c(
189 +
    "score", "label", "err", "acc", "sp", "sn", "prec", "mcc",
190 +
    "fscore"
191 +
  )
192 +
  attr_names <- c(
193 +
    "eval_summary", "grp_avg", "data_info", "uniq_modnames",
194 +
    "uniq_dsids", "model_type", "dataset_type", "args",
195 +
    "validated"
196 +
  )
177 197
  arg_names <- c("mode", "calc_avg", "cb_alpha", "raw_curves")
178 -
  .validate_basic(points, class_name, ".pl_main_basic", item_names, attr_names,
179 -
                  arg_names)
198 +
  .validate_basic(
199 +
    points, class_name, ".pl_main_basic", item_names, attr_names,
200 +
    arg_names
201 +
  )
180 202
181 203
  attr(points, "validated") <- TRUE
182 204
  points
@@ -221,11 +243,15 @@
Loading
221 243
222 244
  # Validate class items and attributes
223 245
  item_names <- NULL
224 -
  attr_names <- c("data_info", "eval_type", "uniq_modnames", "uniq_dsids",
225 -
                  "avgcurves", "validated")
246 +
  attr_names <- c(
247 +
    "data_info", "eval_type", "uniq_modnames", "uniq_dsids",
248 +
    "avgcurves", "validated"
249 +
  )
226 250
  arg_names <- NULL
227 -
  .validate_basic(pointgrp, "pointgrp", ".summarize_points", item_names,
228 -
                  attr_names, arg_names)
251 +
  .validate_basic(
252 +
    pointgrp, "pointgrp", ".summarize_points", item_names,
253 +
    attr_names, arg_names
254 +
  )
229 255
230 256
  attr(pointgrp, "validated") <- TRUE
231 257
  pointgrp

@@ -3,11 +3,12 @@
Loading
3 3
#
4 4
create_curves <- function(pevals, scores = NULL, labels = NULL,
5 5
                          x_bins = 1000, keep_pevals = FALSE, ...) {
6 -
7 6
  # === Validate input arguments ===
8 7
  # Create pevals from scores and labels if pevals is missing
9 -
  pevals <- .create_src_obj(pevals, "pevals", calc_measures, scores, labels,
10 -
                                 ...)
8 +
  pevals <- .create_src_obj(
9 +
    pevals, "pevals", calc_measures, scores, labels,
10 +
    ...
11 +
  )
11 12
12 13
  if (is.null(x_bins) || any(is.na(x_bins))) {
13 14
    x_bins <- 1
@@ -16,10 +17,14 @@
Loading
16 17
  .validate(pevals)
17 18
18 19
  # === Create ROC and Precision-Recall curves ===
19 -
  roc_curve <- create_roc(pevals, x_bins = x_bins,
20 -
                          keep_pevals = keep_pevals, ...)
21 -
  prc_curve <- create_prc(pevals, x_bins = x_bins,
22 -
                          keep_pevals = keep_pevals, ...)
20 +
  roc_curve <- create_roc(pevals,
21 +
    x_bins = x_bins,
22 +
    keep_pevals = keep_pevals, ...
23 +
  )
24 +
  prc_curve <- create_prc(pevals,
25 +
    x_bins = x_bins,
26 +
    keep_pevals = keep_pevals, ...
27 +
  )
23 28
24 29
  curves <- list(roc = roc_curve, prc = prc_curve)
25 30
@@ -48,11 +53,12 @@
Loading
48 53
#
49 54
create_roc <- function(pevals, scores = NULL, labels = NULL, x_bins = 1000,
50 55
                       keep_pevals = FALSE, ...) {
51 -
52 56
  # === Create a ROC curve ===
53 -
  .create_curve("specificity", "sensitivity", create_roc_curve,
54 -
                "create_roc_curve", "roc_curve", pevals, scores, labels,
55 -
                x_bins, keep_pevals, ...)
57 +
  .create_curve(
58 +
    "specificity", "sensitivity", create_roc_curve,
59 +
    "create_roc_curve", "roc_curve", pevals, scores, labels,
60 +
    x_bins, keep_pevals, ...
61 +
  )
56 62
}
57 63
58 64
#
@@ -60,11 +66,12 @@
Loading
60 66
#
61 67
create_prc <- function(pevals, scores = NULL, labels = NULL, x_bins = 1000,
62 68
                       keep_pevals = FALSE, ...) {
63 -
64 69
  # === Create a Precision-Recall curve ===
65 -
  .create_curve("sensitivity", "precision", create_prc_curve,
66 -
                "create_prc_curve", "prc_curve", pevals, scores, labels,
67 -
                x_bins, keep_pevals, ...)
70 +
  .create_curve(
71 +
    "sensitivity", "precision", create_prc_curve,
72 +
    "create_prc_curve", "prc_curve", pevals, scores, labels,
73 +
    x_bins, keep_pevals, ...
74 +
  )
68 75
}
69 76
70 77
#
@@ -73,26 +80,31 @@
Loading
73 80
.create_curve <- function(x_name, y_name, func, func_name, class_name,
74 81
                          pevals, scores = NULL, labels = NULL, x_bins = 1000,
75 82
                          keep_pevals = FALSE, ...) {
76 -
77 83
  # === Validate input arguments ===
78 84
  # Create pevals from scores and labels if pevals is missing
79 -
  pevals <- .create_src_obj(pevals, "pevals", calc_measures, scores, labels,
80 -
                            ...)
85 +
  pevals <- .create_src_obj(
86 +
    pevals, "pevals", calc_measures, scores, labels,
87 +
    ...
88 +
  )
81 89
  .validate_x_bins(x_bins, allow_zero = TRUE)
82 90
  .validate(pevals)
83 91
84 92
  # === Create a curve ===
85 93
  # Calculate a curve
86 94
  pb <- pevals[["basic"]]
87 -
  crv <- func(attr(pevals, "src")[["tp"]], attr(pevals, "src")[["fp"]],
88 -
              pb[[x_name]], pb[[y_name]], x_bins)
95 +
  crv <- func(
96 +
    attr(pevals, "src")[["tp"]], attr(pevals, "src")[["fp"]],
97 +
    pb[[x_name]], pb[[y_name]], x_bins
98 +
  )
89 99
  .check_cpp_func_error(crv, func_name)
90 100
91 101
  # Calculate AUC
92 102
  auc <- calc_auc(crv[["curve"]][["x"]], crv[["curve"]][["y"]])
93 103
  if (auc[["errmsg"]] == "invalid-x-vals") {
94 -
    warning(paste0("Invalid ", x_name,
95 -
                   " values detected. AUC can be inaccurate."))
104 +
    warning(paste0(
105 +
      "Invalid ", x_name,
106 +
      " values detected. AUC can be inaccurate."
107 +
    ))
96 108
  } else {
97 109
    .check_cpp_func_error(auc, "calc_auc")
98 110
  }
@@ -163,26 +175,32 @@
Loading
163 175
.validate_curve <- function(obj, class_name, func_name) {
164 176
  # Validate class items and attributes
165 177
  item_names <- c("x", "y", "orig_points")
166 -
  attr_names <- c("modname", "dsid", "nn", "np", "auc", "args",
167 -
                  "cpp_errmsg1", "cpp_errmsg2", "src", "validated")
168 -
  arg_names <- c("x_bins", "na_worst", "na.last", "ties_method", "ties.method",
169 -
                 "modname", "dsid", "keep_fmdat", "keep_cmats")
170 -
  .validate_basic(obj, class_name, func_name, item_names, attr_names,
171 -
                  arg_names)
178 +
  attr_names <- c(
179 +
    "modname", "dsid", "nn", "np", "auc", "args",
180 +
    "cpp_errmsg1", "cpp_errmsg2", "src", "validated"
181 +
  )
182 +
  arg_names <- c(
183 +
    "x_bins", "na_worst", "na.last", "ties_method", "ties.method",
184 +
    "modname", "dsid", "keep_fmdat", "keep_cmats"
185 +
  )
186 +
  .validate_basic(
187 +
    obj, class_name, func_name, item_names, attr_names,
188 +
    arg_names
189 +
  )
172 190
173 191
  # Check values of class items
174 -
  if ((length(obj[["x"]]) != length(obj[["y"]]))
175 -
      || (length(obj[["x"]]) != length(obj[["orig_points"]]))) {
192 +
  if ((length(obj[["x"]]) != length(obj[["y"]])) ||
193 +
    (length(obj[["x"]]) != length(obj[["orig_points"]]))) {
176 194
    stop("x, y, and orig_points must be all the same lengths", call. = FALSE)
177 195
  } else if (!(length(obj[["x"]]) > 2)) {
178 196
    stop("The minimum length of x, y, and orig_points must be 3",
179 -
         call. = FALSE)
197 +
      call. = FALSE
198 +
    )
180 199
  }
181 200
182 201
  # Check values of class attributes
183 202
  # AUC
184 203
  assertthat::assert_that((attr(obj, "auc") >= 0) && (attr(obj, "auc") <= 1))
185 -
186 204
}
187 205
188 206
#
@@ -196,12 +214,18 @@
Loading
196 214
197 215
  # Validate class items and attributes
198 216
  item_names <- c("roc", "prc")
199 -
  attr_names <- c("modname", "dsid", "nn", "np", "args", "src",
200 -
                  "validated")
201 -
  arg_names <- c("x_bins", "na_worst", "na.last", "ties_method", "ties.method",
202 -
                 "modname", "dsid", "keep_fmdat", "keep_cmats")
203 -
  .validate_basic(curves, "curves", "calc_measures", item_names, attr_names,
204 -
                  arg_names)
217 +
  attr_names <- c(
218 +
    "modname", "dsid", "nn", "np", "args", "src",
219 +
    "validated"
220 +
  )
221 +
  arg_names <- c(
222 +
    "x_bins", "na_worst", "na.last", "ties_method", "ties.method",
223 +
    "modname", "dsid", "keep_fmdat", "keep_cmats"
224 +
  )
225 +
  .validate_basic(
226 +
    curves, "curves", "calc_measures", item_names, attr_names,
227 +
    arg_names
228 +
  )
205 229
206 230
  # Check values of class items
207 231
  curves[["roc"]] <- .validate(curves[["roc"]])

@@ -5,12 +5,17 @@
Loading
5 5
  curve_df <- .prepare_autoplot(object)
6 6
7 7
  # === Create a ggplot object ===
8 -
  p <- ggplot2::ggplot(curve_df,
9 -
                       ggplot2::aes_string(x = 'x', y = 'y', color = 'x'))
8 +
  x_col <- rlang::sym("x")
9 +
  y_col <- rlang::sym("y")
10 +
  p <- ggplot2::ggplot(
11 +
    curve_df,
12 +
    ggplot2::aes(x = !!x_col, y = !!y_col, color = !!x_col)
13 +
  )
10 14
  p <- p + ggplot2::geom_jitter()
11 15
  p <- p + ggplot2::coord_flip()
12 16
  p <- .geom_basic(p, "Score distributions by rank", NULL, "rank",
13 -
                   show_legend = FALSE)
17 +
    show_legend = FALSE
18 +
  )
14 19
}
15 20
16 21
#
@@ -20,11 +25,18 @@
Loading
20 25
  curve_df <- .prepare_autoplot(object)
21 26
22 27
  # === Create a ggplot object ===
23 -
  p <- ggplot2::ggplot(curve_df,
24 -
                       ggplot2::aes_string(x = 'x', y = 'y', color = 'group'))
28 +
  x_col <- rlang::sym("x")
29 +
  y_col <- rlang::sym("y")
30 +
  group_col <- rlang::sym("group")
31 +
  p <- ggplot2::ggplot(
32 +
    curve_df,
33 +
    ggplot2::aes(x = !!x_col, y = !!y_col, color = !!group_col)
34 +
  )
25 35
  p <- p + ggplot2::geom_line()
26 36
  p <- .geom_basic(p, "TPs, FNs, FPs, and TNs by ranks",
27 -
                   "rank", "count", show_legend = TRUE)
37 +
    "rank", "count",
38 +
    show_legend = TRUE
39 +
  )
28 40
}
29 41
30 42
#
@@ -34,47 +46,52 @@
Loading
34 46
  curve_df <- .prepare_autoplot(object)
35 47
36 48
  # === Create a ggplot object ===
37 -
  p <- ggplot2::ggplot(curve_df,
38 -
                       ggplot2::aes_string(x = 'x', y = 'y'))
49 +
  x_col <- rlang::sym("x")
50 +
  y_col <- rlang::sym("y")
51 +
  p <- ggplot2::ggplot(
52 +
    curve_df,
53 +
    ggplot2::aes(x = !!x_col, y = !!y_col)
54 +
  )
39 55
  p <- p + ggplot2::geom_line()
40 -
  p <- p + ggplot2::facet_wrap(~ group, ncol = 2)
56 +
  p <- p + ggplot2::facet_wrap(~group, ncol = 2)
41 57
42 58
  p <- .geom_basic(p, "Evaluation measures by ranks",
43 -
                   "normalized rank", "evaluation value", show_legend = FALSE)
59 +
    "normalized rank", "evaluation value",
60 +
    show_legend = FALSE
61 +
  )
44 62
}
45 63
46 64
#' @rdname autoplot
47 65
#' @export
48 66
autoplot.sscurves <- function(object, curvetype = c("ROC", "PRC"), ...) {
49 -
50 67
  arglist <- .get_autoplot_arglist(attr(object, "args"),
51 -
                                   def_curvetype = curvetype, def_type = "l",
52 -
                                   def_show_cb = FALSE, def_raw_curves = TRUE,
53 -
                                   def_add_np_nn = TRUE,
54 -
                                   def_show_legend = FALSE,
55 -
                                   def_ret_grob = FALSE,
56 -
                                   def_reduce_points = TRUE,
57 -
                                   def_multiplot_lib = "patchwork", ...)
68 +
    def_curvetype = curvetype, def_type = "l",
69 +
    def_show_cb = FALSE, def_raw_curves = TRUE,
70 +
    def_add_np_nn = TRUE,
71 +
    def_show_legend = FALSE,
72 +
    def_ret_grob = FALSE,
73 +
    def_reduce_points = TRUE,
74 +
    def_multiplot_lib = "patchwork", ...
75 +
  )
58 76
  arglist[["raw_curves"]] <- TRUE
59 77
  arglist[["show_cb"]] <- FALSE
60 78
  arglist[["curvetype"]] <- .pmatch_curvetype_rocprc(arglist[["curvetype"]])
61 79
62 80
  .autoplot_multi(object, arglist)
63 -
64 81
}
65 82
66 83
#' @rdname autoplot
67 84
#' @export
68 85
autoplot.mscurves <- function(object, curvetype = c("ROC", "PRC"), ...) {
69 -
70 86
  arglist <- .get_autoplot_arglist(attr(object, "args"),
71 -
                                   def_curvetype = curvetype, def_type = "l",
72 -
                                   def_show_cb = FALSE, def_raw_curves = TRUE,
73 -
                                   def_add_np_nn = TRUE,
74 -
                                   def_show_legend = TRUE,
75 -
                                   def_ret_grob = FALSE,
76 -
                                   def_reduce_points = TRUE,
77 -
                                   def_multiplot_lib = "patchwork", ...)
87 +
    def_curvetype = curvetype, def_type = "l",
88 +
    def_show_cb = FALSE, def_raw_curves = TRUE,
89 +
    def_add_np_nn = TRUE,
90 +
    def_show_legend = TRUE,
91 +
    def_ret_grob = FALSE,
92 +
    def_reduce_points = TRUE,
93 +
    def_multiplot_lib = "patchwork", ...
94 +
  )
78 95
  arglist[["raw_curves"]] <- TRUE
79 96
  arglist[["show_cb"]] <- FALSE
80 97
  arglist[["curvetype"]] <- .pmatch_curvetype_rocprc(arglist[["curvetype"]])
@@ -85,15 +102,15 @@
Loading
85 102
#' @rdname autoplot
86 103
#' @export
87 104
autoplot.smcurves <- function(object, curvetype = c("ROC", "PRC"), ...) {
88 -
89 105
  arglist <- .get_autoplot_arglist(attr(object, "args"),
90 -
                                   def_curvetype = curvetype, def_type = "l",
91 -
                                   def_show_cb = TRUE, def_raw_curves = NULL,
92 -
                                   def_add_np_nn = TRUE,
93 -
                                   def_show_legend = FALSE,
94 -
                                   def_ret_grob = FALSE,
95 -
                                   def_reduce_points = TRUE,
96 -
                                   def_multiplot_lib = "patchwork", ...)
106 +
    def_curvetype = curvetype, def_type = "l",
107 +
    def_show_cb = TRUE, def_raw_curves = NULL,
108 +
    def_add_np_nn = TRUE,
109 +
    def_show_legend = FALSE,
110 +
    def_ret_grob = FALSE,
111 +
    def_reduce_points = TRUE,
112 +
    def_multiplot_lib = "patchwork", ...
113 +
  )
97 114
98 115
  arglist[["curvetype"]] <- .pmatch_curvetype_rocprc(arglist[["curvetype"]])
99 116
@@ -103,15 +120,15 @@
Loading
103 120
#' @rdname autoplot
104 121
#' @export
105 122
autoplot.mmcurves <- function(object, curvetype = c("ROC", "PRC"), ...) {
106 -
107 123
  arglist <- .get_autoplot_arglist(attr(object, "args"),
108 -
                                   def_curvetype = curvetype, def_type = "l",
109 -
                                   def_show_cb = FALSE, def_raw_curves = NULL,
110 -
                                   def_add_np_nn = TRUE,
111 -
                                   def_show_legend = TRUE,
112 -
                                   def_ret_grob = FALSE,
113 -
                                   def_reduce_points = TRUE,
114 -
                                   def_multiplot_lib = "patchwork", ...)
124 +
    def_curvetype = curvetype, def_type = "l",
125 +
    def_show_cb = FALSE, def_raw_curves = NULL,
126 +
    def_add_np_nn = TRUE,
127 +
    def_show_legend = TRUE,
128 +
    def_ret_grob = FALSE,
129 +
    def_reduce_points = TRUE,
130 +
    def_multiplot_lib = "patchwork", ...
131 +
  )
115 132
116 133
  arglist[["curvetype"]] <- .pmatch_curvetype_rocprc(arglist[["curvetype"]])
117 134
@@ -122,36 +139,35 @@
Loading
122 139
#' @export
123 140
autoplot.sspoints <- function(object, curvetype = .get_metric_names("basic"),
124 141
                              ...) {
125 -
126 142
  arglist <- .get_autoplot_arglist(attr(object, "args"),
127 -
                                   def_curvetype = curvetype, def_type = "p",
128 -
                                   def_show_cb = FALSE, def_raw_curves = TRUE,
129 -
                                   def_add_np_nn = TRUE,
130 -
                                   def_show_legend = FALSE,
131 -
                                   def_ret_grob = FALSE,
132 -
                                   def_reduce_points = FALSE,
133 -
                                   def_multiplot_lib = "patchwork", ...)
143 +
    def_curvetype = curvetype, def_type = "p",
144 +
    def_show_cb = FALSE, def_raw_curves = TRUE,
145 +
    def_add_np_nn = TRUE,
146 +
    def_show_legend = FALSE,
147 +
    def_ret_grob = FALSE,
148 +
    def_reduce_points = FALSE,
149 +
    def_multiplot_lib = "patchwork", ...
150 +
  )
134 151
  arglist[["raw_curves"]] <- TRUE
135 152
  arglist[["show_cb"]] <- FALSE
136 153
  arglist[["curvetype"]] <- .pmatch_curvetype_basic(arglist[["curvetype"]])
137 154
138 155
  .autoplot_multi(object, arglist)
139 -
140 156
}
141 157
142 158
#' @rdname autoplot
143 159
#' @export
144 160
autoplot.mspoints <- function(object, curvetype = .get_metric_names("basic"),
145 161
                              ...) {
146 -
147 162
  arglist <- .get_autoplot_arglist(attr(object, "args"),
148 -
                                   def_curvetype = curvetype, def_type = "p",
149 -
                                   def_show_cb = FALSE, def_raw_curves = TRUE,
150 -
                                   def_add_np_nn = TRUE,
151 -
                                   def_show_legend = TRUE,
152 -
                                   def_ret_grob = FALSE,
153 -
                                   def_reduce_points = FALSE,
154 -
                                   def_multiplot_lib = "patchwork", ...)
163 +
    def_curvetype = curvetype, def_type = "p",
164 +
    def_show_cb = FALSE, def_raw_curves = TRUE,
165 +
    def_add_np_nn = TRUE,
166 +
    def_show_legend = TRUE,
167 +
    def_ret_grob = FALSE,
168 +
    def_reduce_points = FALSE,
169 +
    def_multiplot_lib = "patchwork", ...
170 +
  )
155 171
  arglist[["raw_curves"]] <- TRUE
156 172
  arglist[["show_cb"]] <- FALSE
157 173
  arglist[["curvetype"]] <- .pmatch_curvetype_basic(arglist[["curvetype"]])
@@ -163,15 +179,15 @@
Loading
163 179
#' @export
164 180
autoplot.smpoints <- function(object, curvetype = .get_metric_names("basic"),
165 181
                              ...) {
166 -
167 182
  arglist <- .get_autoplot_arglist(attr(object, "args"),
168 -
                                   def_curvetype = curvetype, def_type = "p",
169 -
                                   def_show_cb = TRUE, def_raw_curves = NULL,
170 -
                                   def_add_np_nn = TRUE,
171 -
                                   def_show_legend = FALSE,
172 -
                                   def_ret_grob = FALSE,
173 -
                                   def_reduce_points = FALSE,
174 -
                                   def_multiplot_lib = "patchwork", ...)
183 +
    def_curvetype = curvetype, def_type = "p",
184 +
    def_show_cb = TRUE, def_raw_curves = NULL,
185 +
    def_add_np_nn = TRUE,
186 +
    def_show_legend = FALSE,
187 +
    def_ret_grob = FALSE,
188 +
    def_reduce_points = FALSE,
189 +
    def_multiplot_lib = "patchwork", ...
190 +
  )
175 191
176 192
  arglist[["curvetype"]] <- .pmatch_curvetype_basic(arglist[["curvetype"]])
177 193
@@ -182,15 +198,15 @@
Loading
182 198
#' @export
183 199
autoplot.mmpoints <- function(object, curvetype = .get_metric_names("basic"),
184 200
                              ...) {
185 -
186 201
  arglist <- .get_autoplot_arglist(attr(object, "args"),
187 -
                                   def_curvetype = curvetype, def_type = "p",
188 -
                                   def_show_cb = FALSE, def_raw_curves = NULL,
189 -
                                   def_add_np_nn = TRUE,
190 -
                                   def_show_legend = TRUE,
191 -
                                   def_ret_grob = FALSE,
192 -
                                   def_reduce_points = FALSE,
193 -
                                   def_multiplot_lib = "patchwork", ...)
202 +
    def_curvetype = curvetype, def_type = "p",
203 +
    def_show_cb = FALSE, def_raw_curves = NULL,
204 +
    def_add_np_nn = TRUE,
205 +
    def_show_legend = TRUE,
206 +
    def_ret_grob = FALSE,
207 +
    def_reduce_points = FALSE,
208 +
    def_multiplot_lib = "patchwork", ...
209 +
  )
194 210
195 211
  arglist[["curvetype"]] <- .pmatch_curvetype_basic(arglist[["curvetype"]])
196 212

@@ -43,8 +43,9 @@
Loading
43 43
#' ## Create sample datasets with 100 positives and 100 negatives
44 44
#' samps <- create_sim_samples(4, 100, 100, "good_er")
45 45
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
46 -
#'                modnames = samps[["modnames"]],
47 -
#'                dsids = samps[["dsids"]])
46 +
#'   modnames = samps[["modnames"]],
47 +
#'   dsids = samps[["dsids"]]
48 +
#' )
48 49
#'
49 50
#' ## Generate an smcurve object that contains ROC and Precision-Recall curves
50 51
#' smcurves <- evalmod(mdat)
@@ -62,8 +63,9 @@
Loading
62 63
#' ## Create sample datasets with 100 positives and 100 negatives
63 64
#' samps <- create_sim_samples(4, 100, 100, "all")
64 65
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
65 -
#'                modnames = samps[["modnames"]],
66 -
#'                dsids = samps[["dsids"]])
66 +
#'   modnames = samps[["modnames"]],
67 +
#'   dsids = samps[["dsids"]]
68 +
#' )
67 69
#'
68 70
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
69 71
#' mmcurves <- evalmod(mdat)
@@ -75,10 +77,12 @@
Loading
75 77
#' mm_auc_ci
76 78
#'
77 79
#' @export
78 -
auc_ci <- function(curves, alpha=NULL, dtype=NULL) UseMethod("auc_ci", curves)
80 +
auc_ci <- function(curves, alpha = NULL, dtype = NULL) {
81 +
  UseMethod("auc_ci", curves)
82 +
}
79 83
80 84
#' @export
81 -
auc_ci.default <- function(curves, alpha=NULL, dtype=NULL) {
85 +
auc_ci.default <- function(curves, alpha = NULL, dtype = NULL) {
82 86
  stop("An object of unknown class is specified")
83 87
}
84 88
@@ -87,13 +91,16 @@
Loading
87 91
#
88 92
#' @rdname auc_ci
89 93
#' @export
90 -
auc_ci.aucs <- function(curves, alpha=0.05, dtype="normal") {
94 +
auc_ci.aucs <- function(curves, alpha = 0.05, dtype = "normal") {
91 95
  # Validation
92 96
  .validate(curves)
93 97
  assertthat::assert_that(attr(curves, "dataset_type") == "multiple",
94 -
                          msg = "'curves' must contain multiple datasets.")
95 -
  assertthat::assert_that(assertthat::is.number(alpha),
96 -
                          alpha >= 0 && alpha <= 1)
98 +
    msg = "'curves' must contain multiple datasets."
99 +
  )
100 +
  assertthat::assert_that(
101 +
    assertthat::is.number(alpha),
102 +
    alpha >= 0 && alpha <= 1
103 +
  )
97 104
  assertthat::assert_that(assertthat::is.string(dtype))
98 105
99 106
  # Check type of distribution
@@ -102,7 +109,10 @@
Loading
102 109
  if (!is.na(dype_match)) {
103 110
    dtype <- dtype_tab[dype_match]
104 111
  }
105 -
  err_msg = paste0("'dtype' must be one of ", paste(dtype_tab, collapse = ", "))
112 +
  err_msg <- paste0(
113 +
    "'dtype' must be one of ",
114 +
    paste(dtype_tab, collapse = ", ")
115 +
  )
106 116
  assertthat::assert_that(dtype %in% dtype_tab, msg = err_msg)
107 117
108 118
  # Get AUC scores
@@ -122,14 +132,18 @@
Loading
122 132
      aucs_mean <- mean(aucs_subset$aucs)
123 133
      aucs_n <- length(aucs_subset$aucs)
124 134
      if (aucs_n < 2) {
125 -
        ci_df <- rbind(ci_df,
126 -
                       data.frame(modnames = modname,
127 -
                                  curvetypes = curvetype,
128 -
                                  mean = aucs_mean,
129 -
                                  error = 0,
130 -
                                  lower_bound = aucs_mean,
131 -
                                  upper_bound = aucs_mean,
132 -
                                  n = aucs_n))
135 +
        ci_df <- rbind(
136 +
          ci_df,
137 +
          data.frame(
138 +
            modnames = modname,
139 +
            curvetypes = curvetype,
140 +
            mean = aucs_mean,
141 +
            error = 0,
142 +
            lower_bound = aucs_mean,
143 +
            upper_bound = aucs_mean,
144 +
            n = aucs_n
145 +
          )
146 +
        )
133 147
        next
134 148
      }
135 149
      aucs_sd <- sd(aucs_subset$aucs)
@@ -145,16 +159,19 @@
Loading
145 159
      acus_lower <- max(aucs_mean - aucs_error, 0.0)
146 160
      acus_upper <- min(aucs_mean + aucs_error, 1.0)
147 161
148 -
      ci_df <- rbind(ci_df,
149 -
                     data.frame(modnames = modname,
150 -
                                curvetypes = curvetype,
151 -
                                mean = aucs_mean,
152 -
                                error = aucs_error,
153 -
                                lower_bound = acus_lower,
154 -
                                upper_bound = acus_upper,
155 -
                                n = aucs_n))
162 +
      ci_df <- rbind(
163 +
        ci_df,
164 +
        data.frame(
165 +
          modnames = modname,
166 +
          curvetypes = curvetype,
167 +
          mean = aucs_mean,
168 +
          error = aucs_error,
169 +
          lower_bound = acus_lower,
170 +
          upper_bound = acus_upper,
171 +
          n = aucs_n
172 +
        )
173 +
      )
156 174
    }
157 -
158 175
  }
159 176
160 177
  ci_df

@@ -117,7 +117,6 @@
Loading
117 117
// Solve tied scores
118 118
void solve_ties(std::vector<double>& tp, std::vector<double>& fp,
119 119
                unsigned curpos, unsigned ties) {
120 -
121 120
  const double tied_tp = (tp[curpos] - tp[curpos-ties-1]) / (ties + 1);
122 121
  const double tied_fp = (fp[curpos] - fp[curpos-ties-1]) / (ties + 1);
123 122
  for (unsigned i = 0; i < ties; ++i) {
@@ -795,7 +794,7 @@
Loading
795 794
  n_y[vec_size - 2]  = 1;
796 795
797 796
  // Treat missing values
798 -
  for (int i = 0; i < vec_size; ++i) {
797 +
  for (unsigned i = 0; i < vec_size; ++i) {
799 798
    if (n_y[i] == 0) {
800 799
      if (n_y[i-1] != 0 && n_y[i+1] != 0) {
801 800
        s_y_val[i] = (s_y_val[i-1] + s_y_val[i+1]) / 2.0;
@@ -847,17 +846,17 @@
Loading
847 846
  std::vector<double> stot_y;        // Total of squared ys
848 847
849 848
  // Create all unique x values
850 -
  for (int i = 0; i < points.size(); ++i) {
849 +
  for (unsigned i = 0; i < static_cast<unsigned>(points.size()); ++i) {
851 850
    Rcpp::List c = Rcpp::as<Rcpp::List>(points[i]);
852 851
    Rcpp::NumericVector xs = c["x"];
853 852
854 -
    for (int j = 0; j < xs.size(); ++j) {
853 +
    for (unsigned j = 0; j < static_cast<unsigned>(xs.size()); ++j) {
855 854
      all_x_vals.insert(xs[j]);
856 855
    }
857 856
  }
858 857
859 858
  // Resize vectors
860 -
  int vec_size = all_x_vals.size();
859 +
  const unsigned vec_size = static_cast<const unsigned>(all_x_vals.size());
861 860
  x_val.resize(vec_size, 0.0);
862 861
  avg_y.resize(vec_size, 0.0);
863 862
  se_y.resize(vec_size, 0.0);
@@ -878,12 +877,12 @@
Loading
878 877
879 878
  // Calculate total
880 879
  idx = 0;
881 -
  for (int i = 0; i < points.size(); ++i) {
880 +
  for (unsigned i = 0; i < static_cast<unsigned>(points.size()); ++i) {
882 881
    Rcpp::List c = Rcpp::as<Rcpp::List>(points[i]);
883 882
    Rcpp::NumericVector xs = c["x"];
884 883
    Rcpp::NumericVector ys = c["y"];
885 884
886 -
    for (int j = 0; j < ys.size(); ++j) {
885 +
    for (unsigned j = 0; j < static_cast<unsigned>(ys.size()); ++j) {
887 886
      idx = x_vals_idx[xs[j]];
888 887
889 888
      tot_y[idx] += ys[j];
@@ -896,8 +895,8 @@
Loading
896 895
  double exp2;
897 896
  double sd;
898 897
  double n;
899 -
  for (int i = 0; i < vec_size; ++i) {
900 -
    n = double(count_y[i]);
898 +
  for (unsigned i = 0; i < vec_size; ++i) {
899 +
    n = static_cast<double>(count_y[i]);
901 900
902 901
    // y
903 902
    avg_y[i] = tot_y[i] / n;
@@ -929,4 +928,3 @@
Loading
929 928
  return ret_val;
930 929
}
931 930
932 -

@@ -40,8 +40,8 @@
Loading
40 40
#'   input with calculated pAUCs and standardized pAUCs.
41 41
#'
42 42
#' @seealso \code{\link{evalmod}} for generating \code{S3} objects with
43 -
#'   performance evaluation measures. \code{\link{pauc}} for retrieving a dataset
44 -
#'   of pAUCs.
43 +
#'   performance evaluation measures. \code{\link{pauc}} for retrieving
44 +
#'   a dataset of pAUCs.
45 45
#'
46 46
#' @examples
47 47
#' \dontrun{
@@ -79,7 +79,8 @@
Loading
79 79
#' ## Create sample datasets with 100 positives and 100 negatives
80 80
#' samps <- create_sim_samples(1, 100, 100, "all")
81 81
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
82 -
#'                modnames = samps[["modnames"]])
82 +
#'   modnames = samps[["modnames"]]
83 +
#' )
83 84
#'
84 85
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
85 86
#' mscurves <- evalmod(mdat)
@@ -104,8 +105,9 @@
Loading
104 105
#' ## Create sample datasets with 100 positives and 100 negatives
105 106
#' samps <- create_sim_samples(4, 100, 100, "good_er")
106 107
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
107 -
#'                modnames = samps[["modnames"]],
108 -
#'                dsids = samps[["dsids"]])
108 +
#'   modnames = samps[["modnames"]],
109 +
#'   dsids = samps[["dsids"]]
110 +
#' )
109 111
#'
110 112
#' ## Generate an smcurve object that contains ROC and Precision-Recall curves
111 113
#' smcurves <- evalmod(mdat)
@@ -130,8 +132,9 @@
Loading
130 132
#' ## Create sample datasets with 100 positives and 100 negatives
131 133
#' samps <- create_sim_samples(4, 100, 100, "all")
132 134
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
133 -
#'                modnames = samps[["modnames"]],
134 -
#'                dsids = samps[["dsids"]])
135 +
#'   modnames = samps[["modnames"]],
136 +
#'   dsids = samps[["dsids"]]
137 +
#' )
135 138
#'
136 139
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
137 140
#' mmcurves <- evalmod(mdat, raw_curves = TRUE)
@@ -150,10 +153,12 @@
Loading
150 153
#' }
151 154
#'
152 155
#' @export
153 -
part <- function(curves, xlim=NULL, ylim=NULL, curvetype=NULL) UseMethod("part", curves)
156 +
part <- function(curves, xlim = NULL, ylim = NULL, curvetype = NULL) {
157 +
  UseMethod("part", curves)
158 +
}
154 159
155 160
#' @export
156 -
part.default <- function(curves, xlim=NULL, ylim=NULL, curvetype=NULL) {
161 +
part.default <- function(curves, xlim = NULL, ylim = NULL, curvetype = NULL) {
157 162
  stop("An object of unknown class is specified")
158 163
}
159 164
@@ -206,9 +211,13 @@
Loading
206 211
  # Calculate partial AUC scores for ROC
207 212
  if ("ROC" %in% new_curvetype) {
208 213
    if (avg_only) {
209 -
      attr(curves, "grp_avg")[["rocs"]] <- .calc_pauc(attr(curves,
210 -
                                                           "grp_avg")[["rocs"]],
211 -
                                                      xlim, ylim, avg_only)
214 +
      attr(curves, "grp_avg")[["rocs"]] <- .calc_pauc(
215 +
        attr(
216 +
          curves,
217 +
          "grp_avg"
218 +
        )[["rocs"]],
219 +
        xlim, ylim, avg_only
220 +
      )
212 221
    } else {
213 222
      curves[["rocs"]] <- .calc_pauc(curves[["rocs"]], xlim, ylim, avg_only)
214 223
    }
@@ -219,9 +228,13 @@
Loading
219 228
  # Calculate partial AUC scores for precision-recall
220 229
  if ("PRC" %in% new_curvetype) {
221 230
    if (avg_only) {
222 -
      attr(curves, "grp_avg")[["prcs"]] <- .calc_pauc(attr(curves,
223 -
                                                           "grp_avg")[["prcs"]],
224 -
                                                      xlim, ylim, avg_only)
231 +
      attr(curves, "grp_avg")[["prcs"]] <- .calc_pauc(
232 +
        attr(
233 +
          curves,
234 +
          "grp_avg"
235 +
        )[["prcs"]],
236 +
        xlim, ylim, avg_only
237 +
      )
225 238
    } else {
226 239
      curves[["prcs"]] <- .calc_pauc(curves[["prcs"]], xlim, ylim, avg_only)
227 240
    }
@@ -243,7 +256,6 @@
Loading
243 256
# Calculate partial AUC scores
244 257
#
245 258
.calc_pauc <- function(curves, xlim, ylim, avg_only) {
246 -
247 259
  for (i in seq_along(curves)) {
248 260
    # Trim x
249 261
    x <- curves[[i]][["x"]]
@@ -279,11 +291,11 @@
Loading
279 291
280 292
    # Max 1
281 293
    if (pauc > 1) {
282 -
      pauc = 1
294 +
      pauc <- 1
283 295
    }
284 296
285 297
    if (spauc > 1) {
286 -
      spauc = 1
298 +
      spauc <- 1
287 299
    }
288 300
289 301
    attr(curves[[i]], "pauc") <- pauc
@@ -300,23 +312,28 @@
Loading
300 312
# Get pAUCs
301 313
#
302 314
.gather_paucs <- function(curves) {
303 -
304 315
  # Collect AUCs of ROC or PRC curves
305 316
  ct_len <- 2
306 317
  aucs <- attr(curves, "aucs")
307 -
  paucs <- data.frame(modnames = aucs$modnames,
308 -
                      dsids = aucs$dsids,
309 -
                      curvetypes = aucs$curvetypes,
310 -
                      paucs = rep(NA, length(aucs$modnames)),
311 -
                      spaucs = rep(NA, length(aucs$modnames)),
312 -
                      stringsAsFactors = FALSE)
318 +
  paucs <- data.frame(
319 +
    modnames = aucs$modnames,
320 +
    dsids = aucs$dsids,
321 +
    curvetypes = aucs$curvetypes,
322 +
    paucs = rep(NA, length(aucs$modnames)),
323 +
    spaucs = rep(NA, length(aucs$modnames)),
324 +
    stringsAsFactors = FALSE
325 +
  )
313 326
314 327
  for (i in seq_along(curves[["rocs"]])) {
315 328
    idx <- ct_len * i - 1
316 -
    paucs[["paucs"]][idx:(idx + 1)] <- c(attr(curves[["rocs"]][[i]], "pauc"),
317 -
                                         attr(curves[["prcs"]][[i]], "pauc"))
318 -
    paucs[["spaucs"]][idx:(idx + 1)] <- c(attr(curves[["rocs"]][[i]], "spauc"),
319 -
                                          attr(curves[["prcs"]][[i]], "spauc"))
329 +
    paucs[["paucs"]][idx:(idx + 1)] <- c(
330 +
      attr(curves[["rocs"]][[i]], "pauc"),
331 +
      attr(curves[["prcs"]][[i]], "pauc")
332 +
    )
333 +
    paucs[["spaucs"]][idx:(idx + 1)] <- c(
334 +
      attr(curves[["rocs"]][[i]], "spauc"),
335 +
      attr(curves[["prcs"]][[i]], "spauc")
336 +
    )
320 337
  }
321 338
322 339
  paucs
@@ -326,25 +343,30 @@
Loading
326 343
# Get pAUCs of average curves
327 344
#
328 345
.gather_paucs_avg <- function(curves) {
329 -
330 346
  avg_crvs <- attr(curves, "grp_avg")
331 347
332 348
  # Collect AUCs of ROC or PRC curves
333 349
  ct_len <- 2
334 350
  modnames <- attr(avg_crvs[["rocs"]], "uniq_modnames")
335 -
  paucs <- data.frame(modnames = rep(modnames, each = ct_len),
336 -
                      curvetypes = rep(c("ROC", "PRC"), length(modnames)),
337 -
                      paucs = rep(NA, length(modnames) * ct_len),
338 -
                      spaucs = rep(NA, length(modnames) * ct_len),
339 -
                      stringsAsFactors = FALSE)
351 +
  paucs <- data.frame(
352 +
    modnames = rep(modnames, each = ct_len),
353 +
    curvetypes = rep(c("ROC", "PRC"), length(modnames)),
354 +
    paucs = rep(NA, length(modnames) * ct_len),
355 +
    spaucs = rep(NA, length(modnames) * ct_len),
356 +
    stringsAsFactors = FALSE
357 +
  )
340 358
341 359
  for (i in seq_along(avg_crvs[["rocs"]])) {
342 360
    idx <- ct_len * i - 1
343 361
    idx2 <- idx + 1
344 -
    paucs[["paucs"]][idx:idx2] <- c(attr(avg_crvs[["rocs"]][[i]], "pauc"),
345 -
                                    attr(avg_crvs[["prcs"]][[i]], "pauc"))
346 -
    paucs[["spaucs"]][idx:idx2] <- c(attr(avg_crvs[["rocs"]][[i]], "spauc"),
347 -
                                     attr(avg_crvs[["prcs"]][[i]], "spauc"))
362 +
    paucs[["paucs"]][idx:idx2] <- c(
363 +
      attr(avg_crvs[["rocs"]][[i]], "pauc"),
364 +
      attr(avg_crvs[["prcs"]][[i]], "pauc")
365 +
    )
366 +
    paucs[["spaucs"]][idx:idx2] <- c(
367 +
      attr(avg_crvs[["rocs"]][[i]], "spauc"),
368 +
      attr(avg_crvs[["prcs"]][[i]], "spauc")
369 +
    )
348 370
  }
349 371
350 372
  paucs

@@ -11,10 +11,14 @@
Loading
11 11
  cat("    === Input data ===\n\n")
12 12
13 13
  data_info <- attr(x, "data_info")
14 -
  rownames(data_info) <- format(rownames(data_info), width = 4,
15 -
                                justify = "right")
16 -
  colnames(data_info) <- c("Model name", "Dataset ID", "# of negatives",
17 -
                           "# of positives")
14 +
  rownames(data_info) <- format(rownames(data_info),
15 +
    width = 4,
16 +
    justify = "right"
17 +
  )
18 +
  colnames(data_info) <- c(
19 +
    "Model name", "Dataset ID", "# of negatives",
20 +
    "# of positives"
21 +
  )
18 22
19 23
  print.data.frame(data_info, print.gap = 1)
20 24
@@ -51,8 +55,10 @@
Loading
51 55
      colnames(paucs) <- c("Model name", "Curve type", "pAUC", "Standardized")
52 56
    } else {
53 57
      cat("    === partial AUCs ===\n")
54 -
      colnames(paucs) <- c("Model name", "Dataset ID", "Curve type", "pAUC",
55 -
                           "Standardized")
58 +
      colnames(paucs) <- c(
59 +
        "Model name", "Dataset ID", "Curve type", "pAUC",
60 +
        "Standardized"
61 +
      )
56 62
    }
57 63
58 64
    cat("\n")
@@ -88,12 +94,18 @@
Loading
88 94
  cat("\n\n")
89 95
90 96
  eval_summary <- attr(x, "eval_summary")
91 -
  rownames(eval_summary) <- format(rownames(eval_summary), width = 4,
92 -
                                   justify = "right")
93 -
  colnames(eval_summary) <- c("Model", "ID", "Meas.", "Min.",
94 -
                              "1st Qu.", "Median", "Mean", "3rd Qu.", "Max.")
95 -
  evaltypes <- c("rank", "score", "label", "err", "acc", "sp", "sn", "prec",
96 -
                 "mcc", "fscore")
97 +
  rownames(eval_summary) <- format(rownames(eval_summary),
98 +
    width = 4,
99 +
    justify = "right"
100 +
  )
101 +
  colnames(eval_summary) <- c(
102 +
    "Model", "ID", "Meas.", "Min.",
103 +
    "1st Qu.", "Median", "Mean", "3rd Qu.", "Max."
104 +
  )
105 +
  evaltypes <- c(
106 +
    "rank", "score", "label", "err", "acc", "sp", "sn", "prec",
107 +
    "mcc", "fscore"
108 +
  )
97 109
  eval_summary[, "Meas."] <- evaltypes
98 110
99 111
  print.data.frame(eval_summary, print.gap = 1)
@@ -115,10 +127,14 @@
Loading
115 127
  cat("    === Input data ===\n\n")
116 128
117 129
  data_info <- attr(x, "data_info")
118 -
  rownames(data_info) <- format(rownames(data_info), width = 4,
119 -
                                justify = "right")
120 -
  colnames(data_info) <- c("Model name", "Dataset ID", "# of negatives",
121 -
                           "# of positives")
130 +
  rownames(data_info) <- format(rownames(data_info),
131 +
    width = 4,
132 +
    justify = "right"
133 +
  )
134 +
  colnames(data_info) <- c(
135 +
    "Model name", "Dataset ID", "# of negatives",
136 +
    "# of positives"
137 +
  )
122 138
123 139
  print.data.frame(data_info, print.gap = 1)
124 140
  cat("\n\n")

@@ -5,7 +5,6 @@
Loading
5 5
                            calc_avg = FALSE, cb_alpha = 0.05,
6 6
                            raw_curves = FALSE, na_worst = TRUE,
7 7
                            ties_method = "equiv") {
8 -
9 8
  # === Calculate AUC ROC ===
10 9
  plfunc <- function(s) {
11 10
    # AUC with the U statistic
@@ -15,22 +14,29 @@
Loading
15 14
      } else {
16 15
        cl <- "negative"
17 16
      }
18 -
      err_msg <- paste0("AUCs with the U statistic cannot be calculated. ",
19 -
                        "Only a single class (", cl, ") ",
20 -
                        "found in dataset (modname: ", attr(mdat[[s]], "modname"),
21 -
                        ", dsid: ",attr(mdat[[s]], "dsid"), ").")
17 +
      err_msg <- paste0(
18 +
        "AUCs with the U statistic cannot be calculated. ",
19 +
        "Only a single class (", cl, ") ",
20 +
        "found in dataset (modname: ",
21 +
        attr(mdat[[s]], "modname"),
22 +
        ", dsid: ", attr(mdat[[s]], "dsid"), ")."
23 +
      )
22 24
      stop(err_msg, call. = FALSE)
23 25
    }
24 -
    uauc <- calc_auc_with_u(mdat[[s]], na_worst = na_worst,
25 -
                            ties_method = ties_method)
26 +
    calc_auc_with_u(mdat[[s]],
27 +
      na_worst = na_worst,
28 +
      ties_method = ties_method
29 +
    )
26 30
  }
27 31
  aucrocs <- lapply(seq_along(mdat), plfunc)
28 -
  auc.df <- .summarize_uauc_results(aucrocs, attr(mdat, "uniq_modnames"),
29 -
                                    attr(mdat, "uniq_dsids"), calc_avg,
30 -
                                    cb_alpha, raw_curves)
32 +
  auc_df <- .summarize_uauc_results(
33 +
    aucrocs, attr(mdat, "uniq_modnames"),
34 +
    attr(mdat, "uniq_dsids"), calc_avg,
35 +
    cb_alpha, raw_curves
36 +
  )
31 37
32 38
  # === Create an S3 object ===
33 -
  s3obj <- structure(auc.df, class = "aucroc")
39 +
  s3obj <- structure(auc_df, class = "aucroc")
34 40
35 41
  # Set attributes
36 42
  attr(s3obj, "data_info") <- attr(mdat, "data_info")
@@ -38,12 +44,14 @@
Loading
38 44
  attr(s3obj, "uniq_dsids") <- attr(mdat, "uniq_dsids")
39 45
  attr(s3obj, "model_type") <- model_type
40 46
  attr(s3obj, "dataset_type") <- dataset_type
41 -
  attr(s3obj, "args") <- list(mode = "aucroc",
42 -
                              calc_avg = calc_avg,
43 -
                              cb_alpha = cb_alpha,
44 -
                              raw_curves = raw_curves,
45 -
                              na_worst = na_worst,
46 -
                              ties_method = ties_method)
47 +
  attr(s3obj, "args") <- list(
48 +
    mode = "aucroc",
49 +
    calc_avg = calc_avg,
50 +
    cb_alpha = cb_alpha,
51 +
    raw_curves = raw_curves,
52 +
    na_worst = na_worst,
53 +
    ties_method = ties_method
54 +
  )
47 55
  attr(s3obj, "validated") <- FALSE
48 56
49 57
  # Call .validate.class_name()
@@ -61,12 +69,18 @@
Loading
61 69
62 70
  # Validate class items and attributes
63 71
  item_names <- NULL
64 -
  attr_names <- c("data_info", "uniq_modnames", "uniq_dsids",
65 -
                  "model_type", "dataset_type", "args", "validated")
66 -
  arg_names <- c("mode", "calc_avg", "cb_alpha", "raw_curves", "na_worst",
67 -
                 "ties_method")
68 -
  .validate_basic(aucroc, "aucroc", ".pl_main_aucroc", item_names,
69 -
                  attr_names, arg_names)
72 +
  attr_names <- c(
73 +
    "data_info", "uniq_modnames", "uniq_dsids",
74 +
    "model_type", "dataset_type", "args", "validated"
75 +
  )
76 +
  arg_names <- c(
77 +
    "mode", "calc_avg", "cb_alpha", "raw_curves", "na_worst",
78 +
    "ties_method"
79 +
  )
80 +
  .validate_basic(
81 +
    aucroc, "aucroc", ".pl_main_aucroc", item_names,
82 +
    attr_names, arg_names
83 +
  )
70 84
71 85
  attr(aucroc, "validated") <- TRUE
72 86
  aucroc
@@ -93,12 +107,13 @@
Loading
93 107
      vustat[i] <- aucs[[i]]$ustat
94 108
    }
95 109
96 -
    auc_df <- data.frame(modnames = vmodname,
97 -
                         dsids = vdsid,
98 -
                         aucs = vaucs,
99 -
                         ustats = vustat)
110 +
    auc_df <- data.frame(
111 +
      modnames = vmodname,
112 +
      dsids = vdsid,
113 +
      aucs = vaucs,
114 +
      ustats = vustat
115 +
    )
100 116
  }
101 117
102 118
  list(uaucs = auc_df)
103 119
}
104 -

@@ -8,12 +8,18 @@
Loading
8 8
#'
9 9
#'   \tabular{ll}{
10 10
#'     \strong{Function} \tab \strong{Description} \cr
11 -
#'     \code{\link{evalmod}}            \tab Main function to calculate evaluation measures \cr
12 -
#'     \code{\link{mmdata}}             \tab Reformat input data for performance evaluation calculation \cr
13 -
#'     \code{\link{join_scores}}        \tab Join scores of multiple models into a list \cr
14 -
#'     \code{\link{join_labels}}        \tab Join observed labels of multiple test datasets into a list \cr
15 -
#'     \code{\link{create_sim_samples}} \tab Create random samples for simulations \cr
16 -
#'     \code{\link{format_nfold}}       \tab Create n-fold cross validation dataset from data frame
11 +
#'     \code{\link{evalmod}}
12 +
#'           \tab Main function to calculate evaluation measures \cr
13 +
#'     \code{\link{mmdata}}
14 +
#'           \tab Reformat input data for performance evaluation calculation \cr
15 +
#'     \code{\link{join_scores}}
16 +
#'           \tab Join scores of multiple models into a list \cr
17 +
#'     \code{\link{join_labels}}
18 +
#'           \tab Join observed labels of multiple test datasets into a list \cr
19 +
#'     \code{\link{create_sim_samples}}
20 +
#'           \tab Create random samples for simulations \cr
21 +
#'     \code{\link{format_nfold}}
22 +
#'           \tab Create n-fold cross validation dataset from data frame
17 23
#'   }
18 24
#'
19 25
#' @section S3 generics:
@@ -21,16 +27,36 @@
Loading
21 27
#'  \code{S3} objects generated by the \code{\link{evalmod}} function.
22 28
#'
23 29
#'   \tabular{lll}{
24 -
#'     \strong{S3 generic} \tab \strong{Library} \tab \strong{Description} \cr
25 -
#'     \code{print}           \tab base      \tab Print the calculation results and the summary of the test data \cr
26 -
#'     \code{\link{as.data.frame}} \tab base \tab Convert a precrec object to a data frame \cr
27 -
#'     \code{\link{plot}}     \tab graphics  \tab Plot performance evaluation measures \cr
28 -
#'     \code{\link{autoplot}} \tab ggplot2   \tab Plot performance evaluation measures with ggplot2  \cr
29 -
#'     \code{\link{fortify}}  \tab ggplot2   \tab Prepare a data frame for ggplot2 \cr
30 -
#'     \code{\link{auc}}      \tab precrec   \tab Make a data frame with AUC scores \cr
31 -
#'     \code{\link{part}}     \tab precrec   \tab Calculate partial curves and partial AUC scores \cr
32 -
#'     \code{\link{pauc}}     \tab precrec   \tab Make a data frame with pAUC scores \cr
33 -
#'     \code{\link{auc_ci}}   \tab precrec   \tab Calculate confidence intervals of AUC scores
30 +
#'     \strong{S3 generic}
31 +
#'     \tab \strong{Library}
32 +
#'     \tab \strong{Description} \cr
33 +
#'     \code{print}
34 +
#'     \tab base
35 +
#'     \tab Print the calculation results and the summary of the test data \cr
36 +
#'     \code{\link{as.data.frame}}
37 +
#'     \tab base
38 +
#'     \tab Convert a precrec object to a data frame \cr
39 +
#'     \code{\link{plot}}
40 +
#'     \tab graphics
41 +
#'     \tab Plot performance evaluation measures \cr
42 +
#'     \code{\link{autoplot}}
43 +
#'     \tab ggplot2
44 +
#'     \tab Plot performance evaluation measures with ggplot2  \cr
45 +
#'     \code{\link{fortify}}
46 +
#'     \tab ggplot2
47 +
#'     \tab Prepare a data frame for ggplot2 \cr
48 +
#'     \code{\link{auc}}
49 +
#'     \tab precrec
50 +
#'     \tab Make a data frame with AUC scores \cr
51 +
#'     \code{\link{part}}
52 +
#'     \tab precrec
53 +
#'     \tab Calculate partial curves and partial AUC scores \cr
54 +
#'     \code{\link{pauc}}
55 +
#'     \tab precrec
56 +
#'     \tab Make a data frame with pAUC scores \cr
57 +
#'     \code{\link{auc_ci}}
58 +
#'     \tab precrec
59 +
#'     \tab Calculate confidence intervals of AUC scores
34 60
#'   }
35 61
#'
36 62
#' @section Performance measure calculations:
@@ -53,18 +79,20 @@
Loading
53 79
#'  five different performance levels.
54 80
#'
55 81
#' @section Data visualization:
56 -
#' \code{\link{plot}} takes an \code{S3} object generated by \code{\link{evalmod}} as input
57 -
#' and plot corresponding curves.
82 +
#' \code{\link{plot}} takes an \code{S3} object generated
83 +
#' by \code{\link{evalmod}} as input and plot corresponding curves.
58 84
#'
59 85
#' \code{\link{autoplot}} uses \code{ggplot} to plot curves.
60 86
#'
61 87
#' @section Result retrieval:
62 -
#' \code{\link{as.data.frame}} takes an \code{S3} object generated by \code{\link{evalmod}}
63 -
#' as input and and returns a data frame with calculated curve points.
88 +
#' \code{\link{as.data.frame}} takes an \code{S3} object generated
89 +
#' by \code{\link{evalmod}} as input and and returns a data frame
90 +
#' with calculated curve points.
64 91
#'
65 92
#' \code{\link{auc}} and \code{\link{pauc}} returns a data frame with AUC scores
66 -
#' and partial AUC scores, respectively. \code{\link{auc_ci}} returns confidence intervals
67 -
#' of AUCs for both ROC and precision-recall curves.
93 +
#' and partial AUC scores, respectively. \code{\link{auc_ci}}
94 +
#' returns confidence intervals of AUCs for both ROC
95 +
#' and precision-recall curves.
68 96
#'
69 97
#'
70 98
#' @docType package
@@ -75,8 +103,10 @@
Loading
75 103
#' @importFrom ggplot2 autoplot
76 104
#' @importFrom ggplot2 fortify
77 105
#' @importFrom grDevices col2rgb rainbow rgb
78 -
#' @importFrom graphics abline layout legend lines matplot par plot plot.new polygon
106 +
#' @importFrom graphics abline layout legend lines
107 +
#' @importFrom graphics matplot plot plot.new polygon
79 108
#' @importFrom methods is
109 +
#' @importFrom rlang sym
80 110
#' @importFrom stats qnorm rbeta rnorm sd qt
81 111
#' @importFrom data.table frank
82 112
#'

@@ -52,7 +52,8 @@
Loading
52 52
#' ## Create sample datasets with 100 positives and 100 negatives
53 53
#' samps <- create_sim_samples(1, 100, 100, "all")
54 54
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
55 -
#'                modnames = samps[["modnames"]])
55 +
#'   modnames = samps[["modnames"]]
56 +
#' )
56 57
#'
57 58
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
58 59
#' mscurves <- evalmod(mdat)
@@ -70,8 +71,9 @@
Loading
70 71
#' ## Create sample datasets with 100 positives and 100 negatives
71 72
#' samps <- create_sim_samples(4, 100, 100, "good_er")
72 73
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
73 -
#'                modnames = samps[["modnames"]],
74 -
#'                dsids = samps[["dsids"]])
74 +
#'   modnames = samps[["modnames"]],
75 +
#'   dsids = samps[["dsids"]]
76 +
#' )
75 77
#'
76 78
#' ## Generate an smcurve object that contains ROC and Precision-Recall curves
77 79
#' smcurves <- evalmod(mdat, raw_curves = TRUE)
@@ -89,8 +91,9 @@
Loading
89 91
#' ## Create sample datasets with 100 positives and 100 negatives
90 92
#' samps <- create_sim_samples(4, 100, 100, "all")
91 93
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
92 -
#'                modnames = samps[["modnames"]],
93 -
#'                dsids = samps[["dsids"]])
94 +
#'   modnames = samps[["modnames"]],
95 +
#'   dsids = samps[["dsids"]]
96 +
#' )
94 97
#'
95 98
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
96 99
#' mmcurves <- evalmod(mdat, raw_curves = TRUE)

@@ -4,11 +4,11 @@
Loading
4 4
calc_auc_with_u <- function(sdat, scores = NULL, labels = NULL, na_worst = TRUE,
5 5
                            ties_method = "equiv", keep_sdat = FALSE,
6 6
                            ustat_method = "frank", ...) {
7 -
8 7
  # === Validate input arguments ===
9 8
  # Create sdat from scores and labels if sdat is missing
10 9
  sdat <- .create_src_obj(sdat, "sdat", reformat_data, scores, labels,
11 -
                          mode = "aucroc", ...)
10 +
    mode = "aucroc", ...
11 +
  )
12 12
  .validate(sdat)
13 13
14 14
  # === Calculate AUCs (ROC) ===
@@ -18,30 +18,34 @@
Loading
18 18
    dt_loaded <- .load_data_table()
19 19
    if (dt_loaded) {
20 20
      if (na_worst) {
21 -
        na.last <- FALSE
21 +
        na_last <- FALSE
22 22
      } else {
23 -
        na.last <- TRUE
23 +
        na_last <- TRUE
24 24
      }
25 25
      if (ties_method == "random") {
26 -
        ties.method <- "random"
26 +
        ties_method <- "random"
27 27
      } else {
28 -
        ties.method <- "average"
28 +
        ties_method <- "average"
29 29
      }
30 30
31 31
      frank_func <- function(x) {
32 -
        data.table::frank(x, na.last = na.last, ties.method = ties.method)
32 +
        data.table::frank(x, na.last = na_last, ties.method = ties_method)
33 33
      }
34 34
35 -
      uauc <- calc_uauc_frank(attr(sdat, "np"), attr(sdat, "nn"),
36 -
                              sdat[["scores"]], sdat[["labels"]],
37 -
                              na.last, ties.method, frank_func)
35 +
      uauc <- calc_uauc_frank(
36 +
        attr(sdat, "np"), attr(sdat, "nn"),
37 +
        sdat[["scores"]], sdat[["labels"]],
38 +
        na_last, ties_method, frank_func
39 +
      )
38 40
      .check_cpp_func_error(uauc, "calc_uauc_fsort")
39 41
    }
40 42
  }
41 43
42 44
  if (ustat_method == "sort" || (ustat_method == "frank" && !dt_loaded)) {
43 -
    uauc <- calc_uauc(attr(sdat, "np"), attr(sdat, "nn"), sdat[["scores"]],
44 -
                      sdat[["labels"]], na_worst, ties_method)
45 +
    uauc <- calc_uauc(
46 +
      attr(sdat, "np"), attr(sdat, "nn"), sdat[["scores"]],
47 +
      sdat[["labels"]], na_worst, ties_method
48 +
    )
45 49
    .check_cpp_func_error(uauc, "calc_uauc")
46 50
  }
47 51
@@ -79,17 +83,25 @@
Loading
79 83
80 84
  # Validate class items and attributes
81 85
  item_names <- "auc"
82 -
  attr_names <- c("modname", "dsid", "nn", "np", "args", "cpp_errmsg",
83 -
                  "src", "validated")
84 -
  arg_names <- c("na_worst", "na.last", "ties_method", "ties.method",
85 -
                 "modname", "dsid", "keep_fmdat")
86 -
  .validate_basic(uauc, "uauc", "calc_auc_with_u", item_names, attr_names,
87 -
                  arg_names)
86 +
  attr_names <- c(
87 +
    "modname", "dsid", "nn", "np", "args", "cpp_errmsg",
88 +
    "src", "validated"
89 +
  )
90 +
  arg_names <- c(
91 +
    "na_worst", "na.last", "ties_method", "ties.method",
92 +
    "modname", "dsid", "keep_fmdat"
93 +
  )
94 +
  .validate_basic(
95 +
    uauc, "uauc", "calc_auc_with_u", item_names, attr_names,
96 +
    arg_names
97 +
  )
88 98
89 99
  # AUC
90 100
  auc <- uauc[["auc"]]
91 -
  assertthat::assert_that(assertthat::is.number(auc),
92 -
                          auc >= 0, auc <= 1)
101 +
  assertthat::assert_that(
102 +
    assertthat::is.number(auc),
103 +
    auc >= 0, auc <= 1
104 +
  )
93 105
94 106
  attr(uauc, "validated") <- TRUE
95 107
  uauc

@@ -66,8 +66,10 @@
Loading
66 66
#' @export
67 67
join_scores <- function(..., byrow = FALSE, chklen = TRUE) {
68 68
  # Call join datasets
69 -
  .join_datasets(..., efunc_vtype = .validate_scores, efunc_nrow = NULL,
70 -
                 byrow = byrow, chklen = chklen)
69 +
  .join_datasets(...,
70 +
    efunc_vtype = .validate_scores, efunc_nrow = NULL,
71 +
    byrow = byrow, chklen = chklen
72 +
  )
71 73
}
72 74
73 75
#' Join observed labels of multiple test datasets into a list
@@ -137,8 +139,10 @@
Loading
137 139
#' @export
138 140
join_labels <- function(..., byrow = FALSE, chklen = TRUE) {
139 141
  # Call join datasets
140 -
  .join_datasets(..., efunc_vtype = .validate_labels, efunc_nrow = NULL,
141 -
                 byrow = byrow, chklen = chklen)
142 +
  .join_datasets(...,
143 +
    efunc_vtype = .validate_labels, efunc_nrow = NULL,
144 +
    byrow = byrow, chklen = chklen
145 +
  )
142 146
}
143 147
144 148
#
@@ -146,11 +150,12 @@
Loading
146 150
#
147 151
.join_datasets <- function(..., efunc_vtype = NULL, efunc_nrow = NULL,
148 152
                           byrow = FALSE, chklen = TRUE) {
149 -
150 153
  # Validate arguments
151 -
  .validate_join_datasets_args(..., efunc_vtype = efunc_vtype,
152 -
                               efunc_nrow = efunc_nrow, byrow = byrow,
153 -
                               chklen = chklen)
154 +
  .validate_join_datasets_args(...,
155 +
    efunc_vtype = efunc_vtype,
156 +
    efunc_nrow = efunc_nrow, byrow = byrow,
157 +
    chklen = chklen
158 +
  )
154 159
155 160
  # Set a default error function for checking values
156 161
  if (is.null(efunc_vtype)) {
@@ -182,9 +187,9 @@
Loading
182 187
      cdat <- c(cdat, list(ds))
183 188
    } else if (is.matrix(ds) || is.data.frame(ds)) {
184 189
      if (byrow) {
185 -
        cdat <- c(cdat, lapply(seq(nrow(ds)), function(i) ds[i, ]))
190 +
        cdat <- c(cdat, lapply(seq_len(nrow(ds)), function(i) ds[i, ]))
186 191
      } else {
187 -
        cdat <- c(cdat, lapply(seq(ncol(ds)), function(j) ds[, j]))
192 +
        cdat <- c(cdat, lapply(seq_len(ncol(ds)), function(j) ds[, j]))
188 193
      }
189 194
    } else if (is.array(ds)) {
190 195
      if (length(dim(ds)) == 1) {
@@ -235,7 +240,6 @@
Loading
235 240
#
236 241
.validate_join_datasets_args <- function(..., efunc_vtype, efunc_nrow, byrow,
237 242
                                         chklen) {
238 -
239 243
  # Check ...
240 244
  arglist <- list(...)
241 245
  if (length(arglist) == 0) {
@@ -243,25 +247,28 @@
Loading
243 247
  }
244 248
245 249
  # Check efunc_vtype
246 -
  if (!is.null(efunc_vtype)
247 -
      && (!methods::is(efunc_vtype, "function")
248 -
          || length(as.list(formals(efunc_vtype))) != 1)) {
250 +
  if (!is.null(efunc_vtype) &&
251 +
    (!methods::is(efunc_vtype, "function") ||
252 +
      length(as.list(formals(efunc_vtype))) != 1)) {
249 253
    stop("efunc_vtype must be a function with 1 argument", call. = FALSE)
250 254
  }
251 255
252 256
  # Check efunc_nrow
253 -
  if (!is.null(efunc_nrow)
254 -
      && (!methods::is(efunc_nrow, "function")
255 -
          || length(as.list(formals(efunc_nrow))) != 2)) {
257 +
  if (!is.null(efunc_nrow) &&
258 +
    (!methods::is(efunc_nrow, "function") ||
259 +
      length(as.list(formals(efunc_nrow))) != 2)) {
256 260
    stop("efunc_nrow must be a function with 2 arguments", call. = FALSE)
257 261
  }
258 262
259 263
  # Check byrow
260 -
  assertthat::assert_that(assertthat::is.flag(byrow),
261 -
                          assertthat::noNA(byrow))
264 +
  assertthat::assert_that(
265 +
    assertthat::is.flag(byrow),
266 +
    assertthat::noNA(byrow)
267 +
  )
262 268
263 269
  # Check chklen
264 -
  assertthat::assert_that(assertthat::is.flag(chklen),
265 -
                          assertthat::noNA(chklen))
266 -
270 +
  assertthat::assert_that(
271 +
    assertthat::is.flag(chklen),
272 +
    assertthat::noNA(chklen)
273 +
  )
267 274
}

@@ -103,7 +103,6 @@
Loading
103 103
#'   with \pkg{ggplot2}.
104 104
#'
105 105
#' @examples
106 -
#'
107 106
#' \dontrun{
108 107
#' ##################################################
109 108
#' ### Single model & single test dataset
@@ -125,8 +124,10 @@
Loading
125 124
#' plot(sscurves, curvetype = "PRC")
126 125
#'
127 126
#' ## Generate an sspoints object that contains basic evaluation measures
128 -
#' sspoints <- evalmod(mode = "basic", scores = P10N10$scores,
129 -
#'                     labels = P10N10$labels)
127 +
#' sspoints <- evalmod(
128 +
#'   mode = "basic", scores = P10N10$scores,
129 +
#'   labels = P10N10$labels
130 +
#' )
130 131
#'
131 132
#' ## Plot normalized ranks vs. basic evaluation measures
132 133
#' plot(sspoints)
@@ -142,7 +143,8 @@
Loading
142 143
#' ## Create sample datasets with 100 positives and 100 negatives
143 144
#' samps <- create_sim_samples(1, 100, 100, "all")
144 145
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
145 -
#'                modnames = samps[["modnames"]])
146 +
#'   modnames = samps[["modnames"]]
147 +
#' )
146 148
#'
147 149
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
148 150
#' mscurves <- evalmod(mdat)
@@ -170,8 +172,9 @@
Loading
170 172
#' ## Create sample datasets with 100 positives and 100 negatives
171 173
#' samps <- create_sim_samples(10, 100, 100, "good_er")
172 174
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
173 -
#'                modnames = samps[["modnames"]],
174 -
#'                dsids = samps[["dsids"]])
175 +
#'   modnames = samps[["modnames"]],
176 +
#'   dsids = samps[["dsids"]]
177 +
#' )
175 178
#'
176 179
#' ## Generate an smcurve object that contains ROC and Precision-Recall curves
177 180
#' smcurves <- evalmod(mdat, raw_curves = TRUE)
@@ -199,8 +202,9 @@
Loading
199 202
#' ## Create sample datasets with 100 positives and 100 negatives
200 203
#' samps <- create_sim_samples(10, 100, 100, "all")
201 204
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
202 -
#'                modnames = samps[["modnames"]],
203 -
#'                dsids = samps[["dsids"]])
205 +
#'   modnames = samps[["modnames"]],
206 +
#'   dsids = samps[["dsids"]]
207 +
#' )
204 208
#'
205 209
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
206 210
#' mmcurves <- evalmod(mdat, raw_curves = TRUE)
@@ -229,9 +233,11 @@
Loading
229 233
#' data(M2N50F5)
230 234
#'
231 235
#' ## Speficy nessesary columns to create mdat
232 -
#' cvdat <- mmdata(nfold_df = M2N50F5, score_cols = c(1, 2),
233 -
#'                 lab_col = 3, fold_col = 4,
234 -
#'                 modnames = c("m1", "m2"), dsids = 1:5)
236 +
#' cvdat <- mmdata(
237 +
#'   nfold_df = M2N50F5, score_cols = c(1, 2),
238 +
#'   lab_col = 3, fold_col = 4,
239 +
#'   modnames = c("m1", "m2"), dsids = 1:5
240 +
#' )
235 241
#'
236 242
#' ## Generate an mmcurve object that contains ROC and Precision-Recall curves
237 243
#' cvcurves <- evalmod(cvdat)
@@ -247,8 +253,7 @@
Loading
247 253
#'
248 254
#' ## Normalized ranks vs. average basic evaluation measures
249 255
#' plot(cvpoints)
250 -
#'
251 -
#'}
256 +
#' }
252 257
#' @name plot
253 258
NULL
254 259
@@ -279,7 +284,6 @@
Loading
279 284
# Check partial match - Basic evaluation measures
280 285
#
281 286
.pmatch_curvetype_basic <- function(vals) {
282 -
283 287
  pfunc <- function(val) {
284 288
    if (assertthat::is.string(val)) {
285 289
      sval <- tolower(val)
@@ -296,8 +300,8 @@
Loading
296 300
        return("specificity")
297 301
      }
298 302
299 -
      if (!is.na(pmatch(sval, "sensitivity"))
300 -
          || !is.na(pmatch(sval, "recall")) || sval == "tpr" || sval == "sn") {
303 +
      if (!is.na(pmatch(sval, "sensitivity")) ||
304 +
        !is.na(pmatch(sval, "recall")) || sval == "tpr" || sval == "sn") {
301 305
        return("sensitivity")
302 306
      }
303 307
@@ -305,8 +309,8 @@
Loading
305 309
        return("precision")
306 310
      }
307 311
308 -
      if (!is.na(pmatch(sval, "matthews correlation coefficient"))
309 -
          || sval == "mcc") {
312 +
      if (!is.na(pmatch(sval, "matthews correlation coefficient")) ||
313 +
        sval == "mcc") {
310 314
        return("mcc")
311 315
      }
312 316
@@ -355,7 +359,8 @@
Loading
355 359
  }
356 360
  if (!evalmod_args[["calc_avg"]] && arglist[["show_cb"]]) {
357 361
    stop("Invalid show_cb. Inconsistent with calc_avg of evalmod.",
358 -
         call. = FALSE)
362 +
      call. = FALSE
363 +
    )
359 364
  }
360 365
361 366
  if (is.null(arglist[["raw_curves"]])) {
@@ -369,7 +374,8 @@
Loading
369 374
  }
370 375
  if (!evalmod_args[["raw_curves"]] && arglist[["raw_curves"]]) {
371 376
    stop("Invalid raw_curves. Inconsistent with the value of evalmod.",
372 -
         call. = FALSE)
377 +
      call. = FALSE
378 +
    )
373 379
  }
374 380
375 381
  if (is.null(arglist[["add_np_nn"]])) {
@@ -381,7 +387,6 @@
Loading
381 387
  }
382 388
383 389
  arglist
384 -
385 390
}
386 391
387 392
#
@@ -396,12 +401,12 @@
Loading
396 401
397 402
  show_cb <- arglist[["show_cb"]]
398 403
  if (!attr(x, "args")$calc_avg) {
399 -
    show_cb = FALSE
404 +
    show_cb <- FALSE
400 405
  }
401 406
402 407
  raw_curves <- arglist[["raw_curves"]]
403 408
  if (show_cb) {
404 -
    raw_curves = FALSE
409 +
    raw_curves <- FALSE
405 410
  }
406 411
407 412
  # === Validate input arguments ===
@@ -422,9 +427,11 @@
Loading
422 427
  }
423 428
424 429
  for (ct in curvetype) {
425 -
    .plot_single(x, ct, type = type, show_cb = show_cb,
426 -
                 raw_curves = raw_curves, add_np_nn = add_np_nn,
427 -
                 show_legend = show_legend2)
430 +
    .plot_single(x, ct,
431 +
      type = type, show_cb = show_cb,
432 +
      raw_curves = raw_curves, add_np_nn = add_np_nn,
433 +
      show_legend = show_legend2
434 +
    )
428 435
  }
429 436
  if (length(curvetype) > 4 && length(curvetype) %% 3 == 2) {
430 437
    graphics::plot(1, type = "n", axes = FALSE, xlab = "", ylab = "")
@@ -443,14 +450,13 @@
Loading
443 450
# Set layout
444 451
#
445 452
.set_layout <- function(ctype_len, show_legend) {
446 -
447 453
  if (ctype_len == 1) {
448 454
    nrow1 <- 2
449 455
    ncol1 <- 1
450 456
    mat1 <- c(1, 2)
451 457
    mat2 <- 1
452 458
    heights <- c(0.85, 0.15)
453 -
  } else  if (ctype_len == 2) {
459 +
  } else if (ctype_len == 2) {
454 460
    nrow1 <- 2
455 461
    ncol1 <- 2
456 462
    mat1 <- c(1, 2, 3, 3)
@@ -495,7 +501,6 @@
Loading
495 501
# matplot wrapper
496 502
#
497 503
.matplot_wrapper <- function(obj, type, curvetype, main, xlab, ylab) {
498 -
499 504
  # === Validate input arguments ===
500 505
  .validate(obj[[curvetype]])
501 506
@@ -512,9 +517,11 @@
Loading
512 517
  xlim <- .get_xlim(obj, curvetype)
513 518
  ylim <- .get_ylim(obj, curvetype)
514 519
  mats <- .make_matplot_mats(obj[[curvetype]])
515 -
  graphics::matplot(mats[["x"]], mats[["y"]], type = type, lty = 1, pch = 19,
516 -
                    col = line_col, main = main, xlab = xlab, ylab = ylab,
517 -
                    ylim = ylim, xlim = xlim)
520 +
  graphics::matplot(mats[["x"]], mats[["y"]],
521 +
    type = type, lty = 1, pch = 19,
522 +
    col = line_col, main = main, xlab = xlab, ylab = ylab,
523 +
    ylim = ylim, xlim = xlim
524 +
  )
518 525
}
519 526
520 527
#
@@ -558,8 +565,10 @@
Loading
558 565
559 566
  xlim <- .get_xlim(obj, curvetype)
560 567
  ylim <- .get_ylim(obj, curvetype)
561 -
  graphics::plot(1, type = "n", main = main, xlab = xlab, ylab = ylab,
562 -
                 ylim = ylim, xlim = xlim)
568 +
  graphics::plot(1,
569 +
    type = "n", main = main, xlab = xlab, ylab = ylab,
570 +
    ylim = ylim, xlim = xlim
571 +
  )
563 572
564 573
  if (length(avgcurves) == 1) {
565 574
    lcols <- "blue"
@@ -594,15 +603,21 @@
Loading
594 603
    }
595 604
596 605
    g <- grDevices::col2rgb(pcol)
597 -
    graphics::polygon(c(x, rev(x)), c(ymin, rev(ymax)), border = FALSE,
598 -
                      col = grDevices::rgb(g[1], g[2], g[3], 180,
599 -
                                           maxColorValue = 255))
606 +
    graphics::polygon(c(x, rev(x)), c(ymin, rev(ymax)),
607 +
      border = FALSE,
608 +
      col = grDevices::rgb(g[1], g[2], g[3], 180,
609 +
        maxColorValue = 255
610 +
      )
611 +
    )
600 612
  }
601 613
602 614
  b <- grDevices::col2rgb(lcol)
603 -
  graphics::lines(x, y, type = type, lty = 1, pch = 19,
604 -
                  col = grDevices::rgb(b[1], b[2], b[3], 200,
605 -
                                       maxColorValue = 255))
615 +
  graphics::lines(x, y,
616 +
    type = type, lty = 1, pch = 19,
617 +
    col = grDevices::rgb(b[1], b[2], b[3], 200,
618 +
      maxColorValue = 255
619 +
    )
620 +
  )
606 621
}
607 622
608 623
#
@@ -611,7 +626,6 @@
Loading
611 626
.plot_single <- function(x, curvetype, type = type, show_cb = FALSE,
612 627
                         raw_curves = FALSE, add_np_nn = TRUE,
613 628
                         show_legend = TRUE) {
614 -
615 629
  tlist <- .get_titiles(curvetype)
616 630
  main <- tlist[["main"]]
617 631
@@ -630,19 +644,24 @@
Loading
630 644
631 645
  # === Create a plot ===
632 646
  if (show_cb) {
633 -
    .plot_avg(x, type, tlist[["ctype"]], main, tlist[["xlab"]],
634 -
              tlist[["ylab"]], show_cb)
647 +
    .plot_avg(
648 +
      x, type, tlist[["ctype"]], main, tlist[["xlab"]],
649 +
      tlist[["ylab"]], show_cb
650 +
    )
635 651
  } else if (raw_curves) {
636 -
    .matplot_wrapper(x, type, tlist[["ctype"]], main, tlist[["xlab"]],
637 -
                     tlist[["ylab"]])
652 +
    .matplot_wrapper(
653 +
      x, type, tlist[["ctype"]], main, tlist[["xlab"]],
654 +
      tlist[["ylab"]]
655 +
    )
638 656
  } else {
639 -
    .plot_avg(x, type, tlist[["ctype"]], main, tlist[["xlab"]],
640 -
              tlist[["ylab"]], show_cb)
657 +
    .plot_avg(
658 +
      x, type, tlist[["ctype"]], main, tlist[["xlab"]],
659 +
      tlist[["ylab"]], show_cb
660 +
    )
641 661
  }
642 662
643 663
  if (curvetype == "ROC") {
644 664
    graphics::abline(a = 0, b = 1, col = "grey", lty = 3)
645 -
646 665
  } else if (curvetype == "PRC") {
647 666
    graphics::abline(h = pn_info$prc_base, col = "grey", lty = 3)
648 667
  }
@@ -654,7 +673,7 @@
Loading
654 673
# Get title and subtitles
655 674
#
656 675
.get_titiles <- function(curvetype) {
657 -
  tlist = list()
676 +
  tlist <- list()
658 677
659 678
  if (curvetype == "ROC") {
660 679
    tlist[["main"]] <- "ROC"
@@ -667,15 +686,20 @@
Loading
667 686
    tlist[["ylab"]] <- "Precision"
668 687
    tlist[["ctype"]] <- "prcs"
669 688
  } else {
670 -
    mnames <- list(score = "score", label = "label", error = "err",
671 -
                   accuracy = "acc", specificity = "sp", sensitivity = "sn",
672 -
                   precision = "prec", mcc = "mcc", fscore = "fscore")
689 +
    mnames <- list(
690 +
      score = "score", label = "label", error = "err",
691 +
      accuracy = "acc", specificity = "sp", sensitivity = "sn",
692 +
      precision = "prec", mcc = "mcc", fscore = "fscore"
693 +
    )
673 694
    if (curvetype == "mcc") {
674 695
      main <- "MCC"
675 696
    } else if (curvetype == "label") {
676 697
      main <- "Label (1:pos, -1:neg)"
677 698
    } else {
678 -
      main <- paste0(toupper(substring(curvetype, 1, 1)), substring(curvetype, 2))
699 +
      main <- paste0(
700 +
        toupper(substring(curvetype, 1, 1)),
701 +
        substring(curvetype, 2)
702 +
      )
679 703
    }
680 704
    tlist[["main"]] <- main
681 705
    tlist[["xlab"]] <- "normalized rank"
@@ -694,10 +718,12 @@
Loading
694 718
    withr::local_par(list(mar = c(0, 0, 0, 0), pty = "m"))
695 719
    gnames <- attr(obj, paste0("uniq_", gnames))
696 720
    graphics::plot(1, type = "n", axes = FALSE, xlab = "", ylab = "")
697 -
    graphics::legend(x = "top", lty = 1,
698 -
                     legend = gnames,
699 -
                     col = grDevices::rainbow(length(gnames), alpha = 1),
700 -
                     horiz = TRUE)
721 +
    graphics::legend(
722 +
      x = "top", lty = 1,
723 +
      legend = gnames,
724 +
      col = grDevices::rainbow(length(gnames), alpha = 1),
725 +
      horiz = TRUE
726 +
    )
701 727
  }
702 728
}
703 729
@@ -715,16 +741,20 @@
Loading
715 741
  if (!all(is.na(avgcurves))) {
716 742
    for (i in seq_len(length(avgcurves))) {
717 743
      max_score <- max(max_score, max(avgcurves[[i]][["y_ci_h"]], na.rm = TRUE),
718 -
                       na.rm = TRUE)
744 +
        na.rm = TRUE
745 +
      )
719 746
      min_score <- min(min_score, min(avgcurves[[i]][["y_ci_l"]], na.rm = TRUE),
720 -
                       na.rm = TRUE)
747 +
        na.rm = TRUE
748 +
      )
721 749
    }
722 750
  } else {
723 751
    for (i in seq_len(length(curves))) {
724 752
      max_score <- max(max_score, max(curves[[i]][["y"]], na.rm = TRUE),
725 -
                       na.rm = TRUE)
753 +
        na.rm = TRUE
754 +
      )
726 755
      min_score <- min(min_score, min(curves[[i]][["y"]], na.rm = TRUE),
727 -
                       na.rm = TRUE)
756 +
        na.rm = TRUE
757 +
      )
728 758
    }
729 759
  }
730 760
@@ -744,6 +774,8 @@
Loading
744 774
  } else {
745 775
    xlim <- c(0, 1)
746 776
  }
777 +
778 +
  xlim
747 779
}
748 780
749 781
#
@@ -759,4 +791,6 @@
Loading
759 791
  } else {
760 792
    ylim <- c(0, 1)
761 793
  }
794 +
795 +
  ylim
762 796
}

@@ -91,8 +91,10 @@
Loading
91 91
#' p_prc
92 92
#'
93 93
#' ## Generate an sspoints object that contains basic evaluation measures
94 -
#' sspoints <- evalmod(mode = "basic", scores = P10N10$scores,
95 -
#'                     labels = P10N10$labels)
94 +
#' sspoints <- evalmod(
95 +
#'   mode = "basic", scores = P10N10$scores,
96 +
#'   labels = P10N10$labels
97 +
#' )
96 98
#' ## Fortify sspoints
97 99
#' ssdf <- fortify(sspoints)
98 100
#'
@@ -109,7 +111,8 @@
Loading
109 111
#' ## Create sample datasets with 10 positives and 10 negatives
110 112
#' samps <- create_sim_samples(1, 10, 10, "all")
111 113
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
112 -
#'                modnames = samps[["modnames"]])
114 +
#'   modnames = samps[["modnames"]]
115 +
#' )
113 116
#'
114 117
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
115 118
#' mscurves <- evalmod(mdat)
@@ -155,8 +158,9 @@
Loading
155 158
#' ## Create sample datasets with 10 positives and 10 negatives
156 159
#' samps <- create_sim_samples(5, 10, 10, "good_er")
157 160
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
158 -
#'                modnames = samps[["modnames"]],
159 -
#'                dsids = samps[["dsids"]])
161 +
#'   modnames = samps[["modnames"]],
162 +
#'   dsids = samps[["dsids"]]
163 +
#' )
160 164
#'
161 165
#' ## Generate an smcurve object that contains ROC and Precision-Recall curves
162 166
#' smcurves <- evalmod(mdat, raw_curves = TRUE)
@@ -192,8 +196,9 @@
Loading
192 196
#' df_prec <- subset(smdf, curvetype == "precision")
193 197
#' p_prec <- ggplot(df_prec, aes(x = x, y = y, ymin = ymin, ymax = ymax))
194 198
#' p_prec <- p_prec + geom_ribbon(aes(min = ymin, ymax = ymax),
195 -
#'                                stat = "identity", alpha = 0.25,
196 -
#'                                fill = "grey25")
199 +
#'   stat = "identity", alpha = 0.25,
200 +
#'   fill = "grey25"
201 +
#' )
197 202
#' p_prec <- p_prec + geom_point(aes(x = x, y = y))
198 203
#' p_prec
199 204
#'
@@ -205,8 +210,9 @@
Loading
205 210
#' ## Create sample datasets with 10 positives and 10 negatives
206 211
#' samps <- create_sim_samples(5, 10, 10, "all")
207 212
#' mdat <- mmdata(samps[["scores"]], samps[["labels"]],
208 -
#'                modnames = samps[["modnames"]],
209 -
#'                dsids = samps[["dsids"]])
213 +
#'   modnames = samps[["modnames"]],
214 +
#'   dsids = samps[["dsids"]]
215 +
#' )
210 216
#'
211 217
#' ## Generate an mscurve object that contains ROC and Precision-Recall curves
212 218
#' mmcurves <- evalmod(mdat, raw_curves = TRUE)
@@ -242,8 +248,9 @@
Loading
242 248
#' df_prec <- subset(mmdf, curvetype == "precision")
243 249
#' p_prec <- ggplot(df_prec, aes(x = x, y = y, ymin = ymin, ymax = ymax))
244 250
#' p_prec <- p_prec + geom_ribbon(aes(min = ymin, ymax = ymax, group = modname),
245 -
#'                                stat = "identity", alpha = 0.25,
246 -
#'                                fill = "grey25")
251 +
#'   stat = "identity", alpha = 0.25,
252 +
#'   fill = "grey25"
253 +
#' )
247 254
#' p_prec <- p_prec + geom_point(aes(x = x, y = y, color = modname))
248 255
#' p_prec
249 256
#' }
@@ -256,7 +263,6 @@
Loading
256 263
# Process ... for curve objects
257 264
#
258 265
.get_fortify_arglist <- function(evalmod_args, def_raw_curves, ...) {
259 -
260 266
  arglist <- list(...)
261 267
262 268
  if (!is.null(def_raw_curves)) {

@@ -5,15 +5,16 @@
Loading
5 5
                          modname = as.character(NA), dsid = 1L,
6 6
                          posclass = NULL, na_worst = TRUE,
7 7
                          ties_method = "equiv", mode = "rocprc", ...) {
8 -
9 8
  # === Validate input arguments ===
10 9
  new_ties_method <- .pmatch_tiesmethod(ties_method, ...)
11 10
  new_na_worst <- .get_new_naworst(na_worst, ...)
12 11
  new_mode <- .pmatch_mode(mode)
13 -
  .validate_reformat_data_args(scores, labels, modname = modname, dsid = dsid,
14 -
                               posclass = posclass, na_worst = new_na_worst,
15 -
                               ties_method = new_ties_method, mode = new_mode,
16 -
                               ...)
12 +
  .validate_reformat_data_args(scores, labels,
13 +
    modname = modname, dsid = dsid,
14 +
    posclass = posclass, na_worst = new_na_worst,
15 +
    ties_method = new_ties_method, mode = new_mode,
16 +
    ...
17 +
  )
17 18
18 19
  # === Reformat input data ===
19 20
  # Get a factor with "positive" and "negative"
@@ -21,22 +22,31 @@
Loading
21 22
22 23
  if (mode == "aucroc") {
23 24
    # === Create an S3 object ===
24 -
    s3obj <- structure(list(scores = scores,
25 -
                            labels = fmtlabs[["labels"]]),
26 -
                       class = "sdat")
25 +
    s3obj <- structure(
26 +
      list(
27 +
        scores = scores,
28 +
        labels = fmtlabs[["labels"]]
29 +
      ),
30 +
      class = "sdat"
31 +
    )
27 32
  } else {
28 33
    # Get score ranks and sorted indices
29 34
    sranks <- .rank_scores(scores, new_na_worst, new_ties_method,
30 -
                           validate = FALSE)
35 +
      validate = FALSE
36 +
    )
31 37
    ranks <- sranks[["ranks"]]
32 38
    rank_idx <- sranks[["rank_idx"]]
33 39
34 40
    # === Create an S3 object ===
35 -
    s3obj <- structure(list(scores = scores,
36 -
                            labels = fmtlabs[["labels"]],
37 -
                            ranks = ranks,
38 -
                            rank_idx = rank_idx),
39 -
                       class = "fmdat")
41 +
    s3obj <- structure(
42 +
      list(
43 +
        scores = scores,
44 +
        labels = fmtlabs[["labels"]],
45 +
        ranks = ranks,
46 +
        rank_idx = rank_idx
47 +
      ),
48 +
      class = "fmdat"
49 +
    )
40 50
  }
41 51
42 52
  # Set attributes
@@ -44,9 +54,11 @@
Loading
44 54
  attr(s3obj, "dsid") <- dsid
45 55
  attr(s3obj, "nn") <- fmtlabs[["nn"]]
46 56
  attr(s3obj, "np") <- fmtlabs[["np"]]
47 -
  attr(s3obj, "args") <- list(posclass = posclass, na_worst = new_na_worst,
48 -
                              ties_method = new_ties_method,
49 -
                              modname = modname, dsid = dsid)
57 +
  attr(s3obj, "args") <- list(
58 +
    posclass = posclass, na_worst = new_na_worst,
59 +
    ties_method = new_ties_method,
60 +
    modname = modname, dsid = dsid
61 +
  )
50 62
  attr(s3obj, "validated") <- FALSE
51 63
52 64
  # Call .validate.fmdat() / .validate.sdat()
@@ -88,7 +100,6 @@
Loading
88 100
#
89 101
.rank_scores <- function(scores, na_worst = TRUE, ties_method = "equiv",
90 102
                         validate = TRUE) {
91 -
92 103
  # === Validate input arguments ===
93 104
  if (validate) {
94 105
    .validate_scores(scores)
@@ -97,7 +108,6 @@
Loading
97 108
  }
98 109
99 110
  # === Create ranks ===
100 -
  #   ranks <- rank(scores, na_worst, ties_method)
101 111
  sranks <- get_score_ranks(scores, na_worst, ties_method)
102 112
  .check_cpp_func_error(sranks, "get_score_ranks")
103 113
@@ -110,12 +120,12 @@
Loading
110 120
.validate_reformat_data_args <- function(scores, labels, modname, dsid,
111 121
                                         posclass, na_worst, ties_method,
112 122
                                         mode, ...) {
113 -
114 123
  # Check '...'
115 124
  arglist <- list(...)
116 125
  if (!is.null(names(arglist))) {
117 126
    stop(paste0("Invalid arguments: ", paste(names(arglist), collapse = ", ")),
118 -
         call. = FALSE)
127 +
      call. = FALSE
128 +
    )
119 129
  }
120 130
121 131
  # Check scores and labels
@@ -138,7 +148,6 @@
Loading
138 148
139 149
  # Check mode
140 150
  .validate_mode(mode)
141 -
142 151
}
143 152
144 153
#
@@ -154,30 +163,38 @@
Loading
154 163
  item_names <- c("scores", "labels", "ranks", "rank_idx")
155 164
  attr_names <- c("modname", "dsid", "nn", "np", "args", "validated")
156 165
  arg_names <- c("posclass", "na_worst", "ties_method", "modname", "dsid")
157 -
  .validate_basic(fmdat, "fmdat", "reformat_data", item_names, attr_names,
158 -
                  arg_names)
166 +
  .validate_basic(
167 +
    fmdat, "fmdat", "reformat_data", item_names, attr_names,
168 +
    arg_names
169 +
  )
159 170
160 171
  # Check values of class items
161 -
  if (length(fmdat[["labels"]]) == 0
162 -
      || length(fmdat[["labels"]]) != length(fmdat[["ranks"]])
163 -
      || length(fmdat[["labels"]]) != length(fmdat[["rank_idx"]])) {
172 +
  if (length(fmdat[["labels"]]) == 0 ||
173 +
    length(fmdat[["labels"]]) != length(fmdat[["ranks"]]) ||
174 +
    length(fmdat[["labels"]]) != length(fmdat[["rank_idx"]])) {
164 175
    stop("List items in fmdat must be all the same lengths", call. = FALSE)
165 176
  }
166 177
167 178
  # Labels
168 -
  assertthat::assert_that(is.atomic(fmdat[["labels"]]),
169 -
                          is.vector(fmdat[["labels"]]),
170 -
                          is.numeric(fmdat[["labels"]]))
179 +
  assertthat::assert_that(
180 +
    is.atomic(fmdat[["labels"]]),
181 +
    is.vector(fmdat[["labels"]]),
182 +
    is.numeric(fmdat[["labels"]])
183 +
  )
171 184
172 185
  # Ranks
173 -
  assertthat::assert_that(is.atomic(fmdat[["ranks"]]),
174 -
                          is.vector(fmdat[["ranks"]]),
175 -
                          is.numeric(fmdat[["ranks"]]))
186 +
  assertthat::assert_that(
187 +
    is.atomic(fmdat[["ranks"]]),
188 +
    is.vector(fmdat[["ranks"]]),
189 +
    is.numeric(fmdat[["ranks"]])
190 +
  )
176 191
177 192
  # Rank index
178 -
  assertthat::assert_that(is.atomic(fmdat[["rank_idx"]]),
179 -
                          is.vector(fmdat[["rank_idx"]]),
180 -
                          is.integer(fmdat[["rank_idx"]]))
193 +
  assertthat::assert_that(
194 +
    is.atomic(fmdat[["rank_idx"]]),
195 +
    is.vector(fmdat[["rank_idx"]]),
196 +
    is.integer(fmdat[["rank_idx"]])
197 +
  )
181 198