```{=html}

```r
knitr::opts_chunk$set(
    echo = FALSE,
    message = FALSE,
    warning = FALSE
)
library(rtichoke)
library(dplyr)

Performance Metrics Cheat Sheet


tibble::tribble(
  ~"type", ~"predicted_positive", ~"predicted_negative",
  "Real Positive", "TP", "FN",
  "Real Negative", "FP", "TN"
) %>% 
  reactable::reactable(sortable = FALSE,
            fullWidth = FALSE,
            borderless = FALSE,
            defaultColDef = reactable::colDef(
              align = "center",
              headerStyle  = list(fontWeight = 100)
            ),
            columns = list(
              type = reactable::colDef(
                name = "",
                align = "left",
                style = list(fontWeight = 100),
                minWidth = 110
              ),
              predicted_positive = reactable::colDef(
                name = "Predicted Positive",
                style = function(value) {
                  color <- if (value == "TP") {
                    "lightgreen"
                  } else if (value == "FP") {
                    "pink"
                  }
                  list(fontWeight = 600, background = color)
                }
            ),
            predicted_negative = reactable::colDef(
              name = "Predicted Negative",
              style = function(value) {
                color <- if (value == "TN") {
                  "lightgreen"
                } else if (value == "FN") {
                  "pink"
                }
                list(fontWeight = 600, background = color)
              }
            )
            )
  )



$\begin{aligned} {\text{Prevalence}} = \frac{\text{TP + FN}}{\text{TP + FP + TN + FN}} \end{aligned}$


$\begin{aligned} {\text{PPCR (Predicted Positives Condition Rate)}} = \frac{\text{TP + FP}}{\text{TP + FP + TN + FN}} \end{aligned}$


$\begin{aligned} \text{Sensitivity (Recall, True Positive Rate)} = \frac{\text{TP}}{\text{TP + FN}} = \frac{\text{TP}}{\text{Real Positives}} = \text{Prob( Predicted Positive | Real Positive )} \end{aligned}$


$\begin{aligned} \text{Specificity (True Negative Rate)} = \frac{\text{TN}}{\text{TN + FP}} = \frac{\text{TN}}{\text{Real Negatives}} = \text{Prob( Predicted Negative | Real Negative )} \end{aligned}$


$\begin{aligned} \text{PPV (Precision)} = \frac{\text{TP}}{\text{TP + FP}} = \frac{\text{TP}}{\text{Predicted Positives}} = \text{Prob( Real Positive | Predicted Positive )} \end{aligned}$


$\begin{aligned} \text{NPV} = \frac{\text{TN}}{\text{TN + FN}} = \frac{\text{TN}}{\text{Predicted Negatives}} = \text{Prob( Real Negative | Predicted Negative )} \end{aligned}$


$\begin{aligned} \text{Lift} = \frac{\text{PPV}}{\text{Prevalence}} = \frac{\cfrac{\text{TP}}{\text{TP + FP}}}{\cfrac{\text{TP + FN}}{\text{TP + FP + TN + FN}}} \end{aligned}$


$\begin{aligned} \text{Net Benefit} = \frac{\text{TP}}{\text{TP + FP + TN + FN}} - \frac{\text{FP}}{\text{TP + FP + TN + FN}} * {\frac{{p_{t}}}{{1 - p_{t}}}} \end{aligned}$




performance_dat <- prepare_performance_data(probs = params$probs, 
                         reals = params$reals)
create_table_for_prevalence(performance_dat)

Calibration {.tabset}

Smooth

create_calibration_curve(probs = params$probs, 
                         reals = params$reals, 
                         interactive = params$interactive, 
                         type = "smooth", size = 550) 

Discrete

create_calibration_curve(probs = params$probs, 
                         reals = params$reals, 
                         interactive = params$interactive, 
                         size = 550) 

Discrimination {.tabset}

create_table_for_auc(probs = params$probs, 
                     reals = params$reals)

By Probability Threshold

Performance Metrics Curves {.tabset}

ROC

plot_roc_curve(performance_dat, interactive = params$interactive, 
               size = 500) 

Lift

plot_lift_curve(performance_dat, interactive = params$interactive, 
                size = 500) 

Precision Recall

plot_precision_recall_curve(performance_dat, interactive = params$interactive,
                            size = 500) 

Gains

plot_gains_curve(performance_dat, interactive = params$interactive, 
                 size = 500) 

By Predicted Positives Condition Rate (PPCR)

Performance Metrics Curves {.tabset}

ROC

performance_dat_ppcr <- prepare_performance_data(probs = params$probs, 
                         reals = params$reals, 
                         stratified_by = "ppcr")
plot_roc_curve(performance_dat_ppcr, interactive = params$interactive,
               size = 500) 

Lift

plot_lift_curve(performance_dat_ppcr, interactive = params$interactive,
               size = 500)

Precision Recall

plot_precision_recall_curve(performance_dat_ppcr, interactive = params$interactive,
               size = 500) 

Gains

plot_gains_curve(performance_dat_ppcr, 
                 interactive = params$interactive,
               size = 500) 

Utility (Decision Curve)

plot_decision_curve(performance_dat, 
                    interactive = params$interactive, 
                    type = "combined",
                    size = 500) 

Performance Table {.tabset}

By Probability Threshold

render_performance_table(performance_dat) 

By Predicted Positives Condition Rate (PPCR)

render_performance_table(performance_dat_ppcr)

```{css echo=FALSE}

.form-control { font-family: system-ui, sans-serif; font-size: 2rem; font-weight: bold; line-height: 1.1; display: grid; grid-template-columns: 1em auto; gap: 0.5em; place-content: center; }

.form-control + .form-control { margin-top: 1em; }

input[type="checkbox"] { -webkit-appearance: none; appearance: none; background-color: #fff; margin: 0; font: inherit; width: 1.15em; height: 1.15em; border: 0.15em solid currentColor; border-radius: 0.15em; transform: translateY(-0.075em); display: grid; place-content: center; }

input[type="checkbox"][value="1"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #1b9e77; background-color: CanvasText; }

input[type="checkbox"][value="2"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #d95f02; background-color: CanvasText; }

input[type="checkbox"][value="3"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #7570b3; background-color: CanvasText; }

input[type="checkbox"][value="4"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #e7298a; background-color: CanvasText; }

input[type="checkbox"][value="5"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #07004D; background-color: CanvasText; }

input[type="checkbox"][value="6"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #E6AB02; background-color: CanvasText; }

input[type="checkbox"][value="7"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #FE5F55; background-color: CanvasText; }

input[type="checkbox"][value="8"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #54494B; background-color: CanvasText; }

input[type="checkbox"][value="9"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #006E90; background-color: CanvasText; }

input[type="checkbox"][value="10"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #BC96E6; background-color: CanvasText; }

input[type="checkbox"][value="11"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #52050A; background-color: CanvasText; }

input[type="checkbox"][value="12"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #1F271B; background-color: CanvasText; }

input[type="checkbox"][value="13"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #BE7C4D; background-color: CanvasText; }

input[type="checkbox"][value="14"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #63768D; background-color: CanvasText; }

input[type="checkbox"][value="15"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #08A045; background-color: CanvasText; }

input[type="checkbox"][value="16"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #320A28; background-color: CanvasText; }

input[type="checkbox"][value="17"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #82FF9E; background-color: CanvasText; }

input[type="checkbox"][value="18"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #2176FF; background-color: CanvasText; }

input[type="checkbox"][value="19"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #D1603D; background-color: CanvasText; }

input[type="checkbox"][value="20"]::before { content: ""; width: 0.65em; height: 0.65em; transform: scale(0); box-shadow: inset 1em 1em #585123; background-color: CanvasText; }

input[type="checkbox"]:checked::before { transform: scale(1); }

```



uriahf/rtichoke documentation built on Nov. 22, 2023, 1:30 a.m.