cutpointr is an R package for tidy calculation of “optimal” cutpoints. It supports several methods for calculating cutpoints and includes several metrics that can be maximized or minimized by selecting a cutpoint. Some of these methods are designed to be more robust than the simple empirical optimization of a metric. Additionally, cutpointr can automatically bootstrap the variability of the optimal cutpoints and return out-of-bag estimates of various performance metrics.
You can install cutpointr from CRAN using the menu in RStudio or simply:
For example, the optimal cutpoint for the included data set is 2 when maximizing the sum of sensitivity and specificity.
## age gender dsi suicide
## 1 29 female 1 no
## 2 26 male 0 no
## 3 26 female 0 no
## 4 27 female 0 no
## 5 28 female 0 no
## 6 53 male 2 no
## Assuming the positive class is yes
## Assuming the positive class has higher x values
## Method: maximize_metric
## Predictor: dsi
## Outcome: suicide
## Direction: >=
##
## AUC n n_pos n_neg
## 0.9238 532 36 496
##
## optimal_cutpoint sum_sens_spec acc sensitivity specificity tp fn fp tn
## 2 1.7518 0.8647 0.8889 0.8629 32 4 68 428
##
## Predictor summary:
## Data Min. 5% 1st Qu. Median Mean 3rd Qu. 95% Max. SD NAs
## Overall 0 0.00 0 0 0.9210526 1 5.00 11 1.852714 0
## no 0 0.00 0 0 0.6330645 0 4.00 10 1.412225 0
## yes 0 0.75 4 5 4.8888889 6 9.25 11 2.549821 0
When considering the optimality of a cutpoint, we can only make a judgement based on the sample at hand. Thus, the estimated cutpoint may not be optimal within the population or on unseen data, which is why we sometimes put the “optimal” in quotation marks.
cutpointr makes assumptions about the direction of the
dependency between class and x, if
direction and / or pos_class or
neg_class are not specified. The same result as above can
be achieved by manually defining direction and the positive
/ negative classes which is slightly faster, since the classes and
direction don’t have to be determined:
opt_cut <- cutpointr(suicide, dsi, suicide, direction = ">=", pos_class = "yes",
neg_class = "no", method = maximize_metric, metric = youden)opt_cut is a data frame that returns the input data and
the ROC curve (and optionally the bootstrap results) in a nested tibble.
Methods for summarizing and plotting the data and results are included
(e.g. summary, plot, plot_roc,
plot_metric)
To inspect the optimization, the function of metric values per
cutpoint can be plotted using plot_metric, if an
optimization function was used that returns a metric column in the
roc_curve column. For example, the
maximize_metric and minimize_metric functions
do so: