yardstick (version 0.0.3)

pr_curve: Precision recall curve

Description

pr_curve() constructs the full precision recall curve and returns a tibble. See pr_auc() for the area under the precision recall curve.

Usage

pr_curve(data, ...)

# S3 method for data.frame pr_curve(data, truth, ..., na_rm = TRUE)

# S3 method for pr_df autoplot(object, ...)

Arguments

data

A data.frame containing the truth and estimate columns.

...

A set of unquoted column names or one or more dplyr selector functions to choose which variables contain the class probabilities. If truth is binary, only 1 column should be selected. Otherwise, there should be as many columns as factor levels of truth.

truth

The column identifier for the true class results (that is a factor). This should be an unquoted column name although this argument is passed by expression and supports quasiquotation (you can unquote column names). For _vec() functions, a factor vector.

na_rm

A logical value indicating whether NA values should be stripped before the computation proceeds.

object

The pr_df data frame returned from pr_curve().

Value

A tibble with class pr_df or pr_grouped_df having columns .threshold, recall, and precision.

Multiclass

If a multiclass truth column is provided, a one-vs-all approach will be taken to calculate multiple curves, one per level. In this case, there will be an additional column, .level, identifying the "one" column in the one-vs-all calculation.

Relevant level

There is no common convention on which factor level should automatically be considered the "event" or "positive" result. In yardstick, the default is to use the first level. To change this, a global option called yardstick.event_first is set to TRUE when the package is loaded. This can be changed to FALSE if the last level of the factor is considered the level of interest. For multiclass extensions involving one-vs-all comparisons (such as macro averaging), this option is ignored and the "one" level is always the relevant result.

Details

pr_curve() computes the precision at every unique value of the probability column (in addition to infinity).

There is a ggplot2::autoplot() method for quickly visualizing the curve. This works for binary and multiclass output, and also works with grouped data (i.e. from resamples). See the examples.

See Also

Compute the area under the precision recall curve with pr_auc().

Other curve metrics: gain_curve, lift_curve, roc_curve

Examples

Run this code
# NOT RUN {
library(ggplot2)
library(dplyr)

# Two class - a tibble is returned
pr_curve(two_class_example, truth, Class1)

# Visualize the curve using ggplot2 manually
pr_curve(two_class_example, truth, Class1) %>%
  ggplot(aes(x = recall, y = precision)) +
  geom_path() +
  coord_equal() +
  theme_bw()

# Or use autoplot
autoplot(pr_curve(two_class_example, truth, Class1))

# Multiclass one-vs-all approach
# One curve per level
hpc_cv %>%
  filter(Resample == "Fold01") %>%
  pr_curve(obs, VF:L) %>%
  autoplot()

# Same as above, but will all of the resamples
hpc_cv %>%
  group_by(Resample) %>%
  pr_curve(obs, VF:L) %>%
  autoplot()

# }

Run the code above in your browser using DataCamp Workspace