Given a function for computing a metric in metric_func
, these functions
smooth the function of metric value per cutpoint using generalized additive
models (as implemented in mgcv), then
maximize or minimize the metric by selecting an optimal cutpoint. For further details
on the GAM smoothing see ?mgcv::gam
.
The metric
function should accept the following inputs:
tp
: vector of number of true positives
fp
: vector of number of false positives
tn
: vector of number of true negatives
fn
: vector of number of false negatives
maximize_gam_metric(
data,
x,
class,
metric_func = youden,
pos_class = NULL,
neg_class = NULL,
direction,
formula = m ~ s(x.sorted),
optimizer = c("outer", "newton"),
tol_metric,
use_midpoints,
...
)minimize_gam_metric(
data,
x,
class,
metric_func = youden,
pos_class = NULL,
neg_class = NULL,
direction,
formula = m ~ s(x.sorted),
optimizer = c("outer", "newton"),
tol_metric,
use_midpoints,
...
)
A data frame or tibble in which the columns that are given in x and class can be found.
(character) The variable name to be used for classification, e.g. predictions or test values.
(character) The variable name indicating class membership.
(function) A function that computes a metric to be maximized. See description.
The value of class that indicates the positive class.
The value of class that indicates the negative class.
(character) Use ">=" or "<=" to select whether an x value >= or <= the cutoff predicts the positive class.
A GAM formula. See help("gam", package = "mgcv")
for
details.
An array specifying the numerical optimization method to
use to optimize the smoothing parameter estimation criterion (given by method).
See help("gam", package = "mgcv")
for details.
All cutpoints will be returned that lead to a metric value in the interval [m_max - tol_metric, m_max + tol_metric] where m_max is the maximum achievable metric value. This can be used to return multiple decent cutpoints and to avoid floating-point problems.
(logical) If TRUE (default FALSE) the returned optimal cutpoint will be the mean of the optimal cutpoint and the next highest observation (for direction = ">") or the next lowest observation (for direction = "<") which avoids biasing the optimal cutpoint.
Further arguments that will be passed to metric_func or the GAM smoother.
A tibble with the columns optimal_cutpoint
, the corresponding metric
value and roc_curve
, a nested tibble that includes all possible cutoffs
and the corresponding numbers of true and false positives / negatives and
all corresponding metric values.
The above inputs are arrived at by using all unique values in x
, Inf, and
-Inf as possible cutpoints for classifying the variable in class.
Other method functions:
maximize_boot_metric()
,
maximize_loess_metric()
,
maximize_metric()
,
maximize_spline_metric()
,
oc_manual()
,
oc_mean()
,
oc_median()
,
oc_youden_kernel()
,
oc_youden_normal()
# NOT RUN {
oc <- cutpointr(suicide, dsi, suicide, gender, method = maximize_gam_metric,
metric = accuracy)
plot_metric(oc)
oc <- cutpointr(suicide, dsi, suicide, gender, method = minimize_gam_metric,
metric = abs_d_sens_spec)
plot_metric(oc)
# }
Run the code above in your browser using DataLab