Skip to contents

Computes average precision (AP) for binary probabilistic predictions.

Usage

averagePrecision(x, ref = NULL)

Arguments

x

either a numeric vector of predicted probabilities or scores when ref is supplied, or a fitted model object from which both response and predictions are extracted

ref

numeric vector of observed binary outcomes (0/1), the reference the predictions are scored against. Required when x is a vector of predictions; ignored when x is a model object.

Value

A numeric scalar containing the average precision score.

Details

Average precision summarizes the precision-recall curve as

$$AP = \sum_n (R_n - R_{n-1}) P_n,$$

where \(P_n\) and \(R_n\) are precision and recall at the \(n\)-th distinct prediction threshold. Unlike metrics based on predicted class labels, average precision does not require a classification cutoff. Prediction values therefore need only be numeric scores; they are not restricted to the interval [0, 1].

See also

Examples

resp <- c(0, 0, 1, 1)
pred <- c(0.1, 0.4, 0.35, 0.8)
averagePrecision(pred, resp)
#> [1] 0.8333333