yardstick R Package

repository·main·Indexed 19 days ago

https://github.com/tidymodels/yardstick

An R package for estimating model performance using tidy data principles. It provides metrics for binary and multiclass classification, regression, and other evaluation tasks, including support for macro and micro averaging, resample-based calculations, and visualization of performance curves via autoplot().

Tokens
589
Snippets
5
Records
5
Agent score
14%

What's inside yardstick

  1. Visualize curves with autoplot()

    main

    Curve-based methods such as roc_curve(), pr_curve(), and gain_curve() support ggplot2::autoplot(). This allows for easy visualization of performance curves, which can be faceted by resample or class when using grouped data.

    library(ggplot2)
    
    hpc_cv |> 
      group_by(Resample) |> 
      roc_curve(obs, VF:L) |> 
      autoplot()
  2. Calculate metrics across resamples

    main

    If your data is grouped by resamples (e.g., from cross-validation), you can use dplyr::group_by() in conjunction with yardstick metric functions to calculate metrics for each resample simultaneously.

    hpc_cv |> 
      group_by(Resample) |> 
      roc_auc(obs, VF:L)
  3. Compute metrics for two-class classification

    main

    For binary classification, you can use metrics() to compute multiple performance characteristics at once, or call specific metric functions like roc_auc() directly. The results are returned as tibbles following tidy data principles.

    library(yardstick)
    library(dplyr)
    
    # Compute multiple metrics
    metrics(two_class_example, truth, predicted)
    
    # Compute a specific metric (e.g., ROC AUC)
    two_class_example |> 
      roc_auc(truth, Class1)
  4. Compute multiclass metrics with macro and micro averaging

    main

    All classification metrics in yardstick have multiclass extensions. You can control the aggregation method using the estimator argument. Common options include:

    • macro: Macro-averaged metrics.
    • micro: Micro-averaged metrics.
    # Macro averaged multiclass precision
    precision(hpc_cv, obs, pred)
    
    # Micro averaged multiclass precision
    precision(hpc_cv, obs, pred, estimator = "micro")