| 1 |
#' @templateVar class roc |
| 2 |
#' @template title_desc_tidy |
| 3 |
#' |
| 4 |
#' @param x An `roc` object returned from a call to [AUC::roc()]. |
| 5 |
#' @template param_unused_dots |
| 6 |
#' |
| 7 |
#' @evalRd return_tidy("cutoff", "tpr", "fpr") |
| 8 |
#' |
| 9 |
#' @examplesIf rlang::is_installed(c("AUC", "ggplot2")) |
| 10 |
#' |
| 11 |
#' # load libraries for models and data |
| 12 |
#' library(AUC) |
| 13 |
#' |
| 14 |
#' # load data |
| 15 |
#' data(churn) |
| 16 |
#' |
| 17 |
#' # fit model |
| 18 |
#' r <- roc(churn$predictions, churn$labels) |
| 19 |
#' |
| 20 |
#' # summarize with tidiers + visualization |
| 21 |
#' td <- tidy(r) |
| 22 |
#' td |
| 23 |
#' |
| 24 |
#' library(ggplot2) |
| 25 |
#' |
| 26 |
#' ggplot(td, aes(fpr, tpr)) + |
| 27 |
#' geom_line() |
| 28 |
#' |
| 29 |
#' # compare the ROC curves for two prediction algorithms |
| 30 |
#' library(dplyr) |
| 31 |
#' library(tidyr) |
| 32 |
#' |
| 33 |
#' rocs <- churn |> |
| 34 |
#' pivot_longer(contains("predictions"), |
| 35 |
#' names_to = "algorithm", |
| 36 |
#' values_to = "value" |
| 37 |
#' ) |> |
| 38 |
#' nest(data = -algorithm) |> |
| 39 |
#' mutate(tidy_roc = purrr::map(data, \(x) tidy(roc(x$value, x$labels)))) |> |
| 40 |
#' unnest(tidy_roc) |
| 41 |
#' |
| 42 |
#' ggplot(rocs, aes(fpr, tpr, color = algorithm)) + |
| 43 |
#' geom_line() |
| 44 |
#' |
| 45 |
#' @export |
| 46 |
#' @aliases auc_tidiers roc_tidiers |
| 47 |
#' @seealso [tidy()], [AUC::roc()] |
| 48 |
tidy.roc <- function(x, ...) { |
| 49 |
rename2(as_tibble(unclass(x)), cutoff = cutoffs) |
| 50 |
} |