metric_constrain {cutpointr}R Documentation

Metrics that are constrained by another metric

Description

For example, calculate sensitivity where a lower bound (minimal desired value) for specificty can be defined. All returned metric values for cutpoints that lead to values of the constraining metric below the specified minimum will be zero. The inputs must be vectors of equal length.

Usage

metric_constrain(
  tp,
  fp,
  tn,
  fn,
  main_metric = sensitivity,
  constrain_metric = specificity,
  min_constrain = 0.5,
  suffix = "_constrain",
  ...
)

sens_constrain(
  tp,
  fp,
  tn,
  fn,
  constrain_metric = specificity,
  min_constrain = 0.5,
  ...
)

spec_constrain(
  tp,
  fp,
  tn,
  fn,
  constrain_metric = sensitivity,
  min_constrain = 0.5,
  ...
)

acc_constrain(
  tp,
  fp,
  tn,
  fn,
  constrain_metric = sensitivity,
  min_constrain = 0.5,
  ...
)

Arguments

tp

(numeric) number of true positives.

fp

(numeric) number of false positives.

tn

(numeric) number of true negatives.

fn

(numeric) number of false negatives.

main_metric

Metric to be optimized.

constrain_metric

Metric for constraint.

min_constrain

Minimum desired value of constrain_metric.

suffix

Character string to be added to the name of main_metric.

...

for capturing additional arguments passed by method.

See Also

Other metric functions: F1_score(), Jaccard(), abs_d_ppv_npv(), abs_d_sens_spec(), accuracy(), cohens_kappa(), cutpoint(), false_omission_rate(), misclassification_cost(), npv(), odds_ratio(), p_chisquared(), plr(), ppv(), precision(), prod_ppv_npv(), prod_sens_spec(), recall(), risk_ratio(), roc01(), sensitivity(), specificity(), sum_ppv_npv(), sum_sens_spec(), total_utility(), tpr(), tp(), youden()

Examples

## Maximum sensitivity when Positive Predictive Value (PPV) is at least 75%
library(dplyr)
library(purrr)
library(cutpointr)
cp <- cutpointr(data = suicide, x = dsi, class = suicide,
method = maximize_metric,
metric = sens_constrain,
constrain_metric = ppv,
min_constrain = 0.75)
## All metric values (m) where PPV < 0.75 are zero
plot_metric(cp)
cp$roc_curve
## We can confirm that PPV is indeed >= 0.75
cp %>%
    add_metric(list(ppv))
## We can also do so for the complete ROC curve(s)
cp %>%
    pull(roc_curve) %>%
    map(~ add_metric(., list(sensitivity, ppv)))

## Use the metric_constrain function for a combination of any two metrics
## Estimate optimal cutpoint for precision given a recall of at least 70%
cp <- cutpointr(data = suicide, x = dsi, class = suicide,
                subgroup = gender,
                method = maximize_metric,
                metric = metric_constrain,
                main_metric = precision,
                suffix = "_constrained",
                constrain_metric = recall,
                min_constrain = 0.70)
## All metric values (m) where recall < 0.7 are zero
plot_metric(cp)
## We can confirm that recall is indeed >= 0.70 and that precision_constrain
## is identical to precision for the estimated cutpoint
cp %>%
    add_metric(list(recall, precision))
## We can also do so for the complete ROC curve(s)
cp %>%
    pull(roc_curve) %>%
    map(~ add_metric(., list(recall, precision)))

[Package cutpointr version 1.1.2 Index]