append_class_pred: Add a 'class_pred' column

Description Usage Arguments Value Examples

View source: R/make_class_pred.R

Description

This function is similar to make_class_pred(), but is useful when you have a large number of class probability columns and want to use tidyselect helpers. It appends the new class_pred vector as a column on the original data frame.

Usage

1
2
3
4
5
6
7
8
append_class_pred(
  .data,
  ...,
  levels,
  ordered = FALSE,
  min_prob = 1/length(levels),
  name = ".class_pred"
)

Arguments

.data

A data frame or tibble.

...

One or more unquoted expressions separated by commas to capture the columns of .data containing the class probabilities. You can treat variable names like they are positions, so you can use expressions like x:y to select ranges of variables or use selector functions to choose which columns. For make_class_pred, the columns for all class probabilities should be selected (in the same order as the levels object). For two_class_pred, a vector of class probabilities should be selected.

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.

name

A single character value for the name of the appended class_pred column.

Value

.data with an extra class_pred column appended onto it.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# The following two examples are equivalent and demonstrate
# the helper, append_class_pred()

library(dplyr)

species_probs %>%
  mutate(
    .class_pred = make_class_pred(
      .pred_bobcat, .pred_coyote, .pred_gray_fox,
      levels = levels(Species),
      min_prob = .5
    )
  )

lvls <- levels(species_probs$Species)

append_class_pred(
  .data = species_probs,
  contains(".pred_"),
  levels = lvls,
  min_prob = .5
)

probably documentation built on July 1, 2020, 7:08 p.m.