make_class_pred: Create a 'class_pred' vector from class probabilities In probably: Tools for Post-Processing Predicted Values

 make_class_pred R Documentation

Create a class_pred vector from class probabilities

Description

These functions can be used to convert class probability estimates to class_pred objects with an optional equivocal zone.

Usage

make_class_pred(..., levels, ordered = FALSE, min_prob = 1/length(levels))

make_two_class_pred(
estimate,
levels,
threshold = 0.5,
ordered = FALSE,
buffer = NULL
)

Arguments

 ... Numeric vectors corresponding to class probabilities. There should be one for each level in levels, and it is assumed that the vectors are in the same order as levels. levels A character vector of class levels. The length should be the same as the number of selections made through ..., or length 2 for make_two_class_pred(). ordered A single logical to determine if the levels should be regarded as ordered (in the order given). This results in a class_pred object that is flagged as ordered. min_prob A single numeric value. If any probabilities are less than this value (by row), the row is marked as equivocal. estimate A single numeric vector corresponding to the class probabilities of the first level in levels. threshold A single numeric value for the threshold to call a row to be labeled as the first value of levels. buffer A numeric vector of length 1 or 2 for the buffer around threshold that defines the equivocal zone (i.e., threshold - buffer[1] to threshold + buffer[2]). A length 1 vector is recycled to length 2. The default, NULL, is interpreted as no equivocal zone.

Value

A vector of class class_pred.

Examples

library(dplyr)

good <- segment_logistic\$.pred_good
lvls <- levels(segment_logistic\$Class)

# Equivocal zone of .5 +/- .15
make_two_class_pred(good, lvls, buffer = 0.15)

# Equivocal zone of c(.5 - .05, .5 + .15)
make_two_class_pred(good, lvls, buffer = c(0.05, 0.15))

# These functions are useful alongside dplyr::mutate()
segment_logistic %>%
mutate(
.class_pred = make_two_class_pred(
estimate = .pred_good,
levels = levels(Class),
buffer = 0.15
)
)

# Multi-class example
# Note that we provide class probability columns in the same
# order as the levels
species_probs %>%
mutate(
.class_pred = make_class_pred(
.pred_bobcat, .pred_coyote, .pred_gray_fox,
levels = levels(Species),
min_prob = .5
)
)

probably documentation built on May 29, 2024, 4:44 a.m.