Skip to content

Commit

Permalink
feat: add no information rate
Browse files Browse the repository at this point in the history
  • Loading branch information
overdodactyl committed Jan 5, 2024
1 parent ab95efe commit af56ea2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export(dx_lrt_pos)
export(dx_markedness)
export(dx_mcc)
export(dx_mcnemars)
export(dx_nir)
export(dx_npv)
export(dx_odds_ratio)
export(dx_plot_calibration)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Fowlkes-Mallows Index
* Brier Score
* Detection Prevalence
* No Information Rate (NIR)


*Also added aliases for metrics with the same name (e.g., recall and true positive rate for sensitivity)*
Expand Down
39 changes: 39 additions & 0 deletions R/dx_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -1421,5 +1421,44 @@ calculate_brier <- function(truth, predprob) {
mean((predprob - truth) ^ 2)
}

#' Calculate No Information Rate (NIR)
#'
#' @description
#' The No Information Rate is the proportion of the largest class in the actual outcomes.
#' It represents the accuracy that a naive model would achieve by always predicting
#' the most frequent class. It's a baseline measure for classification performance.
#'
#' @inheritParams metrics-params
#' @examples
#' cm <- dx_cm(dx_heart_failure$predicted, dx_heart_failure$truth, threshold = 0.5, poslabel = 1)
#' nir <- dx_nir(cm)
#' print(nir)
#' @export
#' @concept metrics
dx_nir <- function(cm, detail = "full") {
# Calculate the total number of actual positives and negatives
dispos <- cm$dispos # Number of actual positives
disneg <- cm$disneg # Number of actual negatives

# The NIR is the proportion of the largest class
num <- max(dispos, disneg)
den <- dispos + disneg
nir <- num / den

if (detail == "simple") {
return(nir)
} else if (detail == "full") {
return(measure_df(
measure = "No Information Rate",
estimate = format(nir, digits = 2),
estimate_raw = nir,
fraction = paste0(comma(num), "/", comma(den))
))
}


return(nir)
}



26 changes: 26 additions & 0 deletions man/dx_nir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit af56ea2

Please sign in to comment.