Learn R Programming

Precrec

precrec calculates and plots ROC and precision-recall curves for binary classifiers. It is built for the case where the two curves disagree: on an imbalanced dataset a ROC curve can look excellent while the precision-recall curve shows the classifier is not usable. All the main calculations are implemented in C++ through Rcpp.

Quick example

library(precrec)

# Load a test dataset
data(P10N10)

# Calculate ROC and Precision-Recall curves
sscurves <- evalmod(scores = P10N10$scores, labels = P10N10$labels)
# The ggplot2 package is required
library(ggplot2)

# Show ROC and Precision-Recall plots
autoplot(sscurves)

auc(sscurves) gives the areas, and as.data.frame(sscurves) gives the curve points.

Documentation

Everything is on the package website, in short pages:

  • Get started – the five-minute tour. Also available offline with vignette("introduction", package = "precrec").
  • How-to – one page per task: preparing data, several test sets, cross-validation, more than two classes, large datasets.
  • Compare and decide – which model to ship, at which threshold, and how sure you can be.
  • Metrics – what each available metric means and when it misleads.
  • Plots – every plot the package draws, and how to change it.
  • Reference – the help page of every function, each with examples. Also help(package = "precrec").

Why precrec

Accurate curves. Non-linear interpolation, elongation to the y axis where the first point is undefined, and score-wise thresholds instead of fixed bins. Joining raw precision-recall points with straight lines – what most tools do – overestimates the area.

Fast. Curves over a large dataset take seconds. mode = "aucroc" computes the ROC area from the U statistic without building the curve at all.

Many metrics. Fourteen per-cutoff metrics by default and seventeen more on request, plus AUC, partial AUC, the precision-recall break-even point, and the probability-based Brier score, RMSE and log loss. See the metrics overview.

Several models and several test sets. Averaged curves with confidence bands, cross-validation folds, and confidence intervals of the AUC.

More than two classes. One-vs-rest decomposition, with per-class and macro-averaged areas.

Partial curves. Partial AUCs for any x or y range, standardized so ranges of different widths can be compared.

Installation

Install the release version from CRAN:

install.packages("precrec")

Or the development version from GitHub, which needs a working compiler (Rtools on Windows, Xcode on macOS, the usual build tools on Linux):

# install.packages("devtools")
devtools::install_github("evalclass/precrec")

Functions

FunctionDescription
evalmodMain function to calculate evaluation metrics
mmdataReformat input data for performance evaluation calculation
join_scoresJoin scores of multiple models into a list
join_labelsJoin observed labels of multiple test datasets into a list
create_sim_samplesCreate random samples for simulations
format_nfoldCreate n-fold cross validation dataset from data frame
prob_metricsCalculate the Brier score, the RMSE and the log loss
prob_metrics_ciCalculate CIs of the Brier score, the RMSE and the log loss
metric_curveDraw one evaluation metric against another
metric_tableGet every evaluation metric at every cutoff
best_cutoffPick the cutoff that optimizes one evaluation metric
prbeFind the precision-recall break-even point
average_precisionCalculate the step estimator of the PRC area
classification_reportReport precision, recall and F-score per class at a threshold
auc_bootBootstrap the AUC of a single test set
auc_delongAnalytic standard error of the ROC AUC (DeLong)
auc_diffCompare the AUCs of two models on the same resamples

Ten S3 generics work on the objects evalmod and metric_curve return.

S3 genericPackageDescription
printbasePrint the calculation results and the summary of the test data
as.data.framebaseConvert a precrec object to a data frame
as.data.tabledata.tableConvert a precrec object to a data.table
plotgraphicsPlot performance evaluation metrics
autoplotggplot2Plot performance evaluation metrics with ggplot2
fortifyggplot2Prepare a data frame for ggplot2
aucprecrecMake a data frame with AUC scores
partprecrecCalculate partial curves and partial AUC scores
paucprecrecMake a data frame with pAUC scores
auc_ciprecrecCalculate confidence intervals of AUC scores

Citation

Precrec: fast and accurate precision-recall and ROC curve calculations in R

Takaya Saito; Marc Rehmsmeier

Bioinformatics 2017; 33 (1): 145-147.

doi: 10.1093/bioinformatics/btw570

External links

Copy Link

Version

Install

install.packages('precrec')

Monthly Downloads

3,407

Version

0.24.0

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Takaya Saito

Last Published

September 15th, 2026

Functions in precrec (0.24.0)

best_cutoff

Choose an operating point
auc_ci

Calculate CIs of ROC and precision-recall AUCs
auc_delong

DeLong's standard error for the ROC AUC
evalmod

Evaluate models and calculate performance evaluation metrics
average_precision

Calculate the average precision
auc_boot

Bootstrap AUCs from one test set
auc_diff

Compare AUCs between models
autoplot

Plot performance evaluation metrics with ggplot2
classification_report

Classification report
create_sim_samples

Create random samples for simulations
join_scores

Join scores of multiple models into a list
metric_table

Get every evaluation metric at every cutoff
pauc

Retrieve a data frame of pAUC scores
format_points

Reconstruct per-instance data from a table of curve points
part

Calculate partial AUCs
format_nfold

Create n-fold cross validation dataset from data frame
metric_curve

Draw one evaluation metric against another
mmdata

Reformat input data for performance evaluation calculation
fortify

Convert a curves and points object to a data frame for ggplot2
join_labels

Join observed labels of multiple test datasets into a list
plot

Plot performance evaluation metrics
prob_metrics

Calculate the Brier score, the RMSE and the log loss of predicted probabilities
prob_metrics_ci

Calculate CIs of the Brier score and the log loss
print

Print the summary of a precrec object
prbe

Calculate the precision-recall break-even point
precrec

precrec: A package for computing accurate ROC and Precision-Recall curves
B500

Balanced data with 500 positives and 500 negatives.
as.data.table

Convert a curves and points object to a data.table
as.data.frame

Convert a curves and points object to a data frame
auc

Retrieve a data frame of AUC scores
C3N150

Multiclass sample with three classes.
IB500

Imbalanced data with 500 positives and 5000 negatives.
B1000

Balanced data with 1000 positives and 1000 negatives.
M2N50F5

5-fold cross validation sample.
IB1000

Imbalanced data with 1000 positives and 10000 negatives.
P10N10

A small example dataset with several tied scores.