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

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)
#> [1] poor poor good good poor [EQ] poor good poor poor good poor poor good #> [15] poor good poor poor [EQ] poor poor good poor good poor poor good [EQ] #> [29] good good poor poor [EQ] good good poor poor poor poor poor [EQ] [EQ] #> [43] poor poor poor poor poor good good poor [EQ] poor poor [EQ] poor [EQ] #> [57] poor poor [EQ] poor poor good good poor poor [EQ] good poor [EQ] poor #> [71] good poor [EQ] poor good poor good [EQ] poor [EQ] good poor poor poor #> [85] good good poor good poor poor poor poor [EQ] good poor [EQ] good [EQ] #> [99] [EQ] poor good [EQ] poor poor good good good [EQ] good poor poor poor #> [113] good poor [EQ] poor [EQ] poor good good poor good poor [EQ] poor poor #> [127] good good poor poor [EQ] [EQ] poor good good poor [EQ] poor [EQ] [EQ] #> [141] poor [EQ] [EQ] poor [EQ] poor good [EQ] poor poor poor good good poor #> [155] poor [EQ] good poor poor [EQ] good poor good good poor poor poor [EQ] #> [169] [EQ] poor good good poor poor [EQ] good poor poor poor poor [EQ] good #> [183] poor poor poor [EQ] poor good good poor good [EQ] poor good poor poor #> [197] good poor poor poor good good [EQ] poor poor poor poor poor [EQ] poor #> [211] good good poor [EQ] poor poor poor good poor good [EQ] poor good good #> [225] good poor [EQ] poor poor good poor poor poor good good good poor poor #> [239] poor [EQ] [EQ] poor poor poor [EQ] poor [EQ] good poor poor poor good #> [253] poor poor good [EQ] good good poor [EQ] poor good good [EQ] [EQ] good #> [267] poor poor poor poor [EQ] poor poor poor poor [EQ] good poor [EQ] [EQ] #> [281] poor poor poor good [EQ] poor [EQ] good poor [EQ] poor good [EQ] good #> [295] poor poor poor good poor good [EQ] poor poor poor good poor poor poor #> [309] [EQ] good poor poor good poor good poor [EQ] poor poor [EQ] [EQ] poor #> [323] poor poor poor good [EQ] poor poor [EQ] poor poor poor poor poor [EQ] #> [337] good [EQ] poor good poor good [EQ] good poor [EQ] poor poor poor poor #> [351] poor good good [EQ] [EQ] poor good poor good poor poor poor poor good #> [365] poor poor poor poor poor poor poor [EQ] poor poor poor poor poor poor #> [379] poor good poor poor poor poor poor poor [EQ] good poor poor poor [EQ] #> [393] [EQ] good poor poor poor poor poor poor good [EQ] [EQ] poor poor poor #> [407] poor poor poor poor good good poor poor poor poor poor [EQ] poor poor #> [421] poor good [EQ] good good poor poor poor good good good good poor good #> [435] poor poor poor poor poor poor good [EQ] [EQ] poor good good [EQ] [EQ] #> [449] poor poor good poor poor good poor good poor poor poor good poor poor #> [463] poor poor good poor poor good poor good good good poor good poor good #> [477] good good poor poor good poor poor poor poor poor poor good [EQ] poor #> [491] [EQ] poor poor poor good poor [EQ] poor [EQ] poor poor poor poor poor #> [505] poor good good poor [EQ] [EQ] [EQ] poor poor poor poor good [EQ] good #> [519] poor poor good good poor [EQ] poor poor [EQ] poor good poor poor good #> [533] poor poor poor poor poor good [EQ] poor good good poor poor good poor #> [547] good good poor poor good poor good poor [EQ] poor poor poor poor [EQ] #> [561] good poor good good poor poor poor good good poor poor good [EQ] [EQ] #> [575] poor [EQ] poor poor poor [EQ] poor good poor good good poor poor poor #> [589] poor good [EQ] good poor good [EQ] [EQ] poor poor [EQ] [EQ] poor good #> [603] good good [EQ] good poor poor poor [EQ] poor good poor good [EQ] poor #> [617] poor poor good good poor good poor poor poor poor poor good poor [EQ] #> [631] good [EQ] good poor good poor good poor poor [EQ] [EQ] poor poor poor #> [645] poor poor good good poor poor poor poor poor poor good good poor good #> [659] poor good poor good poor poor poor [EQ] poor poor good poor poor good #> [673] good good poor poor poor [EQ] poor good good [EQ] good poor good poor #> [687] poor poor [EQ] poor poor [EQ] [EQ] good [EQ] poor good poor poor good #> [701] good poor [EQ] poor good poor poor [EQ] [EQ] [EQ] poor good poor good #> [715] good good good poor poor poor good poor good poor poor [EQ] poor poor #> [729] poor poor poor poor [EQ] good good good poor [EQ] poor poor poor good #> [743] poor good good [EQ] poor good poor [EQ] poor poor poor [EQ] good good #> [757] poor poor poor good poor good poor good [EQ] poor good [EQ] poor [EQ] #> [771] good poor good [EQ] poor good poor poor good poor poor good good good #> [785] poor poor poor poor poor good poor [EQ] poor poor poor good [EQ] poor #> [799] good [EQ] [EQ] good poor good poor poor poor poor poor poor poor [EQ] #> [813] poor good poor poor poor poor good poor good good poor poor poor poor #> [827] poor good [EQ] poor good poor poor poor poor poor poor poor [EQ] poor #> [841] poor poor poor poor good good good poor poor poor poor poor poor poor #> [855] poor good [EQ] [EQ] [EQ] poor good [EQ] poor poor poor [EQ] good poor #> [869] good good poor good poor poor good [EQ] [EQ] [EQ] poor poor poor poor #> [883] [EQ] good poor good poor good poor poor poor poor good poor poor poor #> [897] poor poor poor poor poor [EQ] poor poor [EQ] good [EQ] good poor poor #> [911] poor good [EQ] poor good poor poor poor poor good poor poor good good #> [925] poor poor good poor [EQ] poor good poor good good good poor poor good #> [939] good poor poor [EQ] [EQ] poor good poor poor good [EQ] [EQ] poor [EQ] #> [953] poor good [EQ] [EQ] poor good poor poor poor good poor poor poor poor #> [967] good poor poor [EQ] poor poor poor good good poor [EQ] poor poor poor #> [981] good poor poor good poor [EQ] good good good good poor [EQ] poor good #> [995] poor poor poor poor good good good good good poor good [EQ] good poor #> [1009] poor good #> Levels: good poor #> Reportable: 83.2%
# Equivocal zone of c(.5 - .05, .5 + .15) make_two_class_pred(good, lvls, buffer = c(0.05, 0.15))
#> [1] poor poor good good poor poor poor good poor poor good poor poor good #> [15] poor good poor poor [EQ] poor poor good poor good poor poor good poor #> [29] good good poor poor [EQ] good good poor poor poor poor poor [EQ] poor #> [43] poor poor poor poor poor good good poor [EQ] poor poor poor poor poor #> [57] poor poor [EQ] poor poor good good poor poor poor good poor [EQ] poor #> [71] good poor [EQ] poor good poor good poor poor [EQ] good poor poor poor #> [85] good good poor good poor poor poor poor [EQ] good poor [EQ] good [EQ] #> [99] poor poor good [EQ] poor poor good good good poor good poor poor poor #> [113] good poor poor poor [EQ] poor good good poor good poor [EQ] poor poor #> [127] good good poor poor [EQ] poor poor good good poor [EQ] poor [EQ] [EQ] #> [141] poor poor [EQ] poor [EQ] poor good poor poor poor poor good good poor #> [155] poor poor good poor poor [EQ] good poor good good poor poor poor [EQ] #> [169] poor poor good good poor poor [EQ] good poor poor poor poor [EQ] good #> [183] poor poor poor [EQ] poor good good poor good poor poor good poor poor #> [197] good poor poor poor good good poor poor poor poor poor poor poor poor #> [211] good good poor [EQ] poor poor poor good poor good [EQ] poor good good #> [225] good poor [EQ] poor poor good poor poor poor good good good poor poor #> [239] poor [EQ] poor poor poor poor [EQ] poor poor good poor poor poor good #> [253] poor poor good [EQ] good good poor [EQ] poor good good [EQ] poor good #> [267] poor poor poor poor [EQ] poor poor poor poor [EQ] good poor poor [EQ] #> [281] poor poor poor good [EQ] poor [EQ] good poor poor poor good [EQ] good #> [295] poor poor poor good poor good [EQ] poor poor poor good poor poor poor #> [309] [EQ] good poor poor good poor good poor poor poor poor [EQ] [EQ] poor #> [323] poor poor poor good poor poor poor [EQ] poor poor poor poor poor [EQ] #> [337] good [EQ] poor good poor good poor good poor [EQ] poor poor poor poor #> [351] poor good good poor [EQ] poor good poor good poor poor poor poor good #> [365] poor poor poor poor poor poor poor poor poor poor poor poor poor poor #> [379] poor good poor poor poor poor poor poor [EQ] good poor poor poor [EQ] #> [393] [EQ] good poor poor poor poor poor poor good [EQ] [EQ] poor poor poor #> [407] poor poor poor poor good good poor poor poor poor poor [EQ] poor poor #> [421] poor good [EQ] good good poor poor poor good good good good poor good #> [435] poor poor poor poor poor poor good [EQ] poor poor good good [EQ] [EQ] #> [449] poor poor good poor poor good poor good poor poor poor good poor poor #> [463] poor poor good poor poor good poor good good good poor good poor good #> [477] good good poor poor good poor poor poor poor poor poor good poor poor #> [491] [EQ] poor poor poor good poor poor poor [EQ] poor poor poor poor poor #> [505] poor good good poor [EQ] [EQ] [EQ] poor poor poor poor good poor good #> [519] poor poor good good poor poor poor poor [EQ] poor good poor poor good #> [533] poor poor poor poor poor good poor poor good good poor poor good poor #> [547] good good poor poor good poor good poor poor poor poor poor poor poor #> [561] good poor good good poor poor poor good good poor poor good [EQ] poor #> [575] poor poor poor poor poor poor poor good poor good good poor poor poor #> [589] poor good [EQ] good poor good poor [EQ] poor poor poor [EQ] poor good #> [603] good good [EQ] good poor poor poor poor poor good poor good poor poor #> [617] poor poor good good poor good poor poor poor poor poor good poor poor #> [631] good poor good poor good poor good poor poor [EQ] [EQ] poor poor poor #> [645] poor poor good good poor poor poor poor poor poor good good poor good #> [659] poor good poor good poor poor poor [EQ] poor poor good poor poor good #> [673] good good poor poor poor [EQ] poor good good [EQ] good poor good poor #> [687] poor poor poor poor poor poor [EQ] good [EQ] poor good poor poor good #> [701] good poor poor poor good poor poor [EQ] [EQ] poor poor good poor good #> [715] good good good poor poor poor good poor good poor poor [EQ] poor poor #> [729] poor poor poor poor [EQ] good good good poor poor poor poor poor good #> [743] poor good good poor poor good poor [EQ] poor poor poor [EQ] good good #> [757] poor poor poor good poor good poor good [EQ] poor good [EQ] poor [EQ] #> [771] good poor good [EQ] poor good poor poor good poor poor good good good #> [785] poor poor poor poor poor good poor [EQ] poor poor poor good [EQ] poor #> [799] good poor poor good poor good poor poor poor poor poor poor poor poor #> [813] poor good poor poor poor poor good poor good good poor poor poor poor #> [827] poor good [EQ] poor good poor poor poor poor poor poor poor poor poor #> [841] poor poor poor poor good good good poor poor poor poor poor poor poor #> [855] poor good [EQ] poor [EQ] poor good poor poor poor poor [EQ] good poor #> [869] good good poor good poor poor good [EQ] poor [EQ] poor poor poor poor #> [883] [EQ] good poor good poor good poor poor poor poor good poor poor poor #> [897] poor poor poor poor poor poor poor poor poor good poor good poor poor #> [911] poor good [EQ] poor good poor poor poor poor good poor poor good good #> [925] poor poor good poor [EQ] poor good poor good good good poor poor good #> [939] good poor poor poor poor poor good poor poor good [EQ] poor poor [EQ] #> [953] poor good [EQ] poor poor good poor poor poor good poor poor poor poor #> [967] good poor poor [EQ] poor poor poor good good poor poor poor poor poor #> [981] good poor poor good poor poor good good good good poor [EQ] poor good #> [995] poor poor poor poor good good good good good poor good [EQ] good poor #> [1009] poor good #> Levels: good poor #> Reportable: 89.8%
# 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 ) )
#> # A tibble: 1,010 x 4 #> .pred_poor .pred_good Class .class_pred #> <dbl> <dbl> <fct> <clss_prd> #> 1 0.986 0.0142 poor poor #> 2 0.897 0.103 poor poor #> 3 0.118 0.882 good good #> 4 0.102 0.898 good good #> 5 0.991 0.00914 poor poor #> 6 0.633 0.367 good [EQ] #> 7 0.770 0.230 good poor #> 8 0.00842 0.992 good good #> 9 0.995 0.00458 poor poor #> 10 0.765 0.235 poor poor #> # … with 1,000 more rows
# 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 ) )
#> # A tibble: 110 x 5 #> Species .pred_bobcat .pred_coyote .pred_gray_fox .class_pred #> <fct> <dbl> <dbl> <dbl> <clss_prd> #> 1 gray_fox 0.0976 0.0530 0.849 gray_fox #> 2 gray_fox 0.155 0.139 0.706 gray_fox #> 3 bobcat 0.501 0.0880 0.411 bobcat #> 4 gray_fox 0.256 0 0.744 gray_fox #> 5 gray_fox 0.463 0.287 0.250 [EQ] #> 6 bobcat 0.811 0 0.189 bobcat #> 7 bobcat 0.911 0.0888 0 bobcat #> 8 bobcat 0.898 0.0517 0.0500 bobcat #> 9 bobcat 0.771 0.229 0 bobcat #> 10 bobcat 0.623 0.325 0.0517 bobcat #> # … with 100 more rows