https://github.com/cran/cutpointr
Tip revision: 4408233eb8624dea85ecf18e86d50c296165c3f2 authored by Christian Thiele on 13 April 2022, 17:12:29 UTC
version 1.1.2
version 1.1.2
Tip revision: 4408233
summary.multi_cutpointr.R
#' @export
summary.multi_cutpointr <- function(object, ...) {
x_summary <- vector("list", nrow(object))
names(x_summary) <- suppressWarnings(object$subgroup)
for (r in 1:nrow(object)) {
temprow <- object[r, ]
if (has_column(object, "subgroup")) {
x_summary[[r]]$subgroup <- temprow$subgroup
}
x_summary[[r]]$predictor <- temprow$predictor
x_summary[[r]]$cutpointr <- temprow
x_summary[[r]]$desc <- dplyr::bind_cols(
tibble::tibble(Data = "Overall"),
summary_sd_df(temprow$data[[1]] %>%
dplyr::select(temprow$predictor))
)
temprow_split <- temprow$data[[1]] %>%
split(temprow$data[[1]][, temprow$outcome])
x_summary[[r]]$desc_byclass <- temprow_split %>%
purrr::map_df(function(x) summary_sd_df(x %>% dplyr::select(temprow$predictor)))
x_summary[[r]]$desc_byclass <- dplyr::bind_cols(
data.frame(Data = names(temprow_split), stringsAsFactors = FALSE),
x_summary[[r]]$desc_byclass
)
x_summary[[r]]$n_obs <- nrow(temprow$data[[1]])
x_summary[[r]]$n_pos <- temprow$data[[1]] %>%
dplyr::select(!!as.name(temprow$outcome)) %>%
unlist %>% (function(x) sum(x == temprow$pos_class))
x_summary[[r]]$n_neg <- x_summary[[r]]$n_obs - x_summary[[r]]$n_pos
# Confusion Matrix
oi <- get_opt_ind(temprow$roc_curve[[1]],
oc = unlist(temprow$optimal_cutpoint),
direction = temprow$direction)
x_summary[[r]]$confusion_matrix <- data.frame(
cutpoint = unlist(temprow$optimal_cutpoint),
temprow$roc_curve[[1]][oi, c("tp", "fn", "fp", "tn")]
)
if (has_boot_results(temprow)) {
x_summary[[r]][["boot"]] <- purrr::map(temprow[["boot"]][[1]][, 1:13], function(x) {
summary_sd(x)
})
x_summary[[r]][["boot"]] <- do.call(rbind, x_summary[[r]][["boot"]])
x_summary[[r]][["boot"]] <- as.data.frame(x_summary[[r]][["boot"]])
x_summary[[r]][["boot"]] <- tibble::rownames_to_column(x_summary[[r]][["boot"]],
var = "Variable")
x_summary[[r]]$boot_runs <- nrow(temprow[["boot"]][[1]])
}
}
x_summary <- suppressMessages(purrr::map_df(x_summary, tidy_summary_multi))
class(x_summary) <- c("summary_multi_cutpointr", class(x_summary))
return(x_summary)
}
# Convert the list output of summary.cutpointr to a data.frame
# x is a single element of the resulting list from summary.cutpointr.
tidy_summary_multi <- function(x) {
res <- tibble::tibble(
predictor = x$predictor,
cutpointr = list(x$cutpointr),
desc = list(x$desc),
desc_by_class = list(x$desc_byclass),
n_obs = x$n_obs,
n_pos = x$n_pos,
n_neg = x$n_neg,
confusion_matrix = list(x$confusion_matrix)
)
if (has_boot_results(x)) {
res <- dplyr::bind_cols(res,
# tidyr::nest(x[["boot"]], .key = "boot"),
tibble::tibble(boot = list(x[["boot"]])),
boot_runs = x$boot_runs)
}
if (has_column(x, "subgroup")) {
res <- dplyr::bind_cols(subgroup = x$subgroup, res)
}
return(res)
}