#' Add evaluation metrics to a tibble of models
#'
#' @param data A data frame containing columns "prediction" and "test"
#' @param metrics Which metrics to add. Options: "auc", "cbi"
#' @param occ_col Name of the response column in the test dataset
#'
#' @return Vector of evaluation metric scores
#' @export
score_models <- function(data, metrics, occ_col = "occ") {
out <- data
if ("auc" %in% metrics) {
out <- mutate(out, auc = purrr::map2_dbl(prediction, test, function(prediction, test) {
pred_obs <- setNames(data.frame(as.data.frame(prediction)[[1]]), "pred")
pred_obs$obs <- dplyr::pull(as.data.frame(test), occ_col)
auc <- yardstick::roc_auc(pred_obs, obs, pred) %>% pull(.estimate)
}))
}
if ("cbi" %in% metrics) {
out <- mutate(out, cbi = purrr::map2_dbl(prediction, test, function(prediction, test) {
pred_obs <- setNames(data.frame(as.data.frame(prediction)[[1]]), "pred")
pred_obs$obs <- dplyr::pull(as.data.frame(test), occ_col)
ecospat::ecospat.boyce(pred_obs %>% pull(pred), pred_obs %>% filter(obs == 1) %>% pull(pred), PEplot = FALSE)$Spearman.cor
}))
}
out
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.