🚀
Documentation
RepositoryBugreport
  • Introduction
  • Applications
    • Example
  • Reference
    • Classification metrics
      • Accuracy
        • accuracy.cmatrix.md
        • accuracy.factor.md
        • accuracy.md
        • weighted.accuracy.factor.md
      • Area under the Precision Recall Curve
        • auc.pr.curve.factor.md
        • auc.pr.curve.md
        • pr.curve
          • auc.pr.curve.factor.md
          • auc.pr.curve.md
          • pr.curve.factor.md
          • pr.curve.md
          • weighted.auc.pr.curve.factor.md
          • weighted.pr.curve.factor.md
        • weighted.auc.pr.curve.factor.md
      • Area under the Receiver Operator Characteristics Curve
        • auc.roc.curve.factor.md
        • auc.roc.curve.md
        • roc.curve
          • auc.roc.curve.factor.md
          • auc.roc.curve.md
          • roc.curve.factor.md
          • roc.curve.md
          • weighted.auc.roc.curve.factor.md
          • weighted.roc.curve.factor.md
        • weighted.auc.roc.curve.factor.md
      • Balanced Accuracy
        • baccuracy.cmatrix.md
        • baccuracy.factor.md
        • baccuracy.md
        • weighted.baccuracy.factor.md
      • Brier Score
        • brier.score.matrix.md
        • brier.score.md
        • weighted.brier.score.matrix.md
      • Cohen's kappa-Statistic
        • ckappa.cmatrix.md
        • ckappa.factor.md
        • ckappa.md
        • weighted.ckappa.factor.md
      • Cross Entropy
        • cross.entropy.matrix.md
        • cross.entropy.md
      • Diagnostic Odds Ratio
        • dor.cmatrix.md
        • dor.factor.md
        • dor.md
        • weighted.dor.factor.md
      • False Discovery Rate
        • fdr.cmatrix.md
        • fdr.factor.md
        • fdr.md
        • weighted.fdr.factor.md
      • False Omission Rate
        • fer.cmatrix.md
        • fer.factor.md
        • fer.md
        • weighted.fer.factor.md
      • False Positive Rate
        • fpr.cmatrix.md
        • fpr.factor.md
        • fpr.md
        • weighted.fpr.factor.md
      • Fowlkes Mallows Index
        • fmi.cmatrix.md
        • fmi.factor.md
        • fmi.md
        • weighted.fmi.factor.md
      • Hamming Loss
        • hammingloss.cmatrix.md
        • hammingloss.factor.md
        • hammingloss.md
        • weighted.hammingloss.factor.md
      • Jaccard Index
        • jaccard.cmatrix.md
        • jaccard.factor.md
        • jaccard.md
        • weighted.jaccard.factor.md
      • Logarithmic Loss
        • logloss.factor.md
        • logloss.integer
        • logloss.integer.md
          • logloss.integer.md
          • weighted.logloss.integer.md
        • logloss.md
        • weighted.logloss.factor.md
        • weighted.logloss.integer.md
      • Matthews Correlation Coefficient
        • mcc.cmatrix.md
        • mcc.factor.md
        • mcc.md
        • weighted.mcc.factor.md
      • Negative Likelihood Ratio
        • nlr.cmatrix.md
        • nlr.factor.md
        • nlr.md
        • weighted.nlr.factor.md
      • Negative Predictive Value
        • npv.cmatrix.md
        • npv.factor.md
        • npv.md
        • weighted.npv.factor.md
      • Positive Likelihood Ratio
        • plr.cmatrix.md
        • plr.factor.md
        • plr.md
        • weighted.plr.factor.md
      • Precision
        • precision.cmatrix.md
        • precision.factor.md
        • precision.md
        • weighted.precision.factor.md
      • Recall
        • accuracy.cmatrix.md
        • baccuracy.cmatrix.md
        • ckappa.cmatrix.md
        • cmatrix.factor.md
        • cmatrix.md
        • dor.cmatrix.md
        • fbeta.cmatrix.md
        • fdr.cmatrix.md
        • fer.cmatrix.md
        • fmi.cmatrix.md
        • fpr.cmatrix.md
        • hammingloss.cmatrix.md
        • jaccard.cmatrix.md
        • mcc.cmatrix.md
        • nlr.cmatrix.md
        • npv.cmatrix.md
        • plr.cmatrix.md
        • precision.cmatrix.md
        • recall
        • recall.cmatrix.md
          • recall.cmatrix.md
          • recall.factor.md
          • recall.md
          • weighted.recall.factor.md
        • specificity.cmatrix.md
        • weighted.cmatrix.factor.md
        • zerooneloss.cmatrix.md
      • Relative Entropy
        • relative.entropy.matrix.md
        • relative.entropy.md
      • Shannon Entropy
        • shannon.entropy.matrix.md
        • shannon.entropy.md
      • Specificity
        • specificity.cmatrix.md
        • specificity.factor.md
        • specificity.md
        • weighted.specificity.factor.md
      • Zero-One Loss
        • weighted.zerooneloss.factor.md
        • zerooneloss.cmatrix.md
        • zerooneloss.factor.md
        • zerooneloss.md
      • f{beta}
        • fbeta.cmatrix.md
        • fbeta.factor.md
        • fbeta.md
        • weighted.fbeta.factor.md
    • Regression metrics
      • Concordance Correlation Coefficient
        • ccc.md
        • ccc.numeric.md
        • weighted.ccc.numeric.md
      • Gamma Deviance
        • deviance.gamma.md
        • deviance.gamma.numeric.md
        • weighted.deviance.gamma.numeric.md
      • Geometric Mean Squared Error
        • gmse.md
        • gmse.numeric.md
        • weighted.gmse.numeric.md
      • Huber Loss
        • huberloss.md
        • huberloss.numeric.md
        • weighted.huberloss.numeric.md
      • Mean Absolute Error
        • mae.md
        • mae.numeric.md
        • weighted.mae.numeric.md
      • Mean Absolute Percentage Error
        • mape.md
        • mape.numeric.md
        • weighted.mape.numeric.md
      • Mean Arctangent Absolute Percentage Error
        • maape.md
        • maape.numeric.md
        • weighted.maape.numeric.md
      • Mean Percentage Error
        • mpe.md
        • mpe.numeric.md
        • weighted.mpe.numeric.md
      • Mean Squared Error
        • mse.md
        • mse.numeric.md
        • weighted.mse.numeric.md
      • Pinball Loss
        • pinball.md
        • pinball.numeric.md
        • weighted.pinball.numeric.md
      • Poisson Deviance
        • deviance.poisson.md
        • deviance.poisson.numeric.md
        • weighted.deviance.poisson.numeric.md
      • Relative Absolute Error
        • rae.md
        • rae.numeric.md
        • weighted.rae.numeric.md
      • Relative Root Mean Squared Error
        • rrmse.md
        • rrmse.numeric.md
        • weighted.rrmse.numeric.md
      • Root Mean Squared Error
        • rmse.md
        • rmse.numeric.md
        • weighted.rmse.numeric.md
      • Root Mean Squared Logarithmic Error
        • rmsle.md
        • rmsle.numeric.md
        • weighted.rmsle.numeric.md
      • Root Relative Squared Error
        • rrse.md
        • rrse.numeric.md
        • weighted.rrse.numeric.md
      • Symmetric Mean Absolutte Percentage Error
        • smape.md
        • smape.numeric.md
        • weighted.smape.numeric.md
      • Tweedie Deviance
        • deviance.tweedie.md
        • deviance.tweedie.numeric.md
        • weighted.deviance.tweedie.numeric.md
      • r^2
        • rsq.md
        • rsq.numeric.md
        • weighted.rsq.numeric.md
  • Changelog
    • Changelog
Powered by GitBook
On this page
  1. Reference
  2. Classification metrics
  3. Area under the Precision Recall Curve
  4. pr.curve

auc.pr.curve.factor.md

auc.pr.curve.factor

R Documentation

Area under the Precision Recall Curve

Description

A generic S3 function to compute the area under the precision recall curve score for a classification model. This function dispatches to S3 methods in auc.pr.curve() and performs no input validation. If you supply NA values or vectors of unequal length (e.g.length(x) != length(y)), the underlying C++ code may trigger undefined behavior and crash your R session.

Defensive measures

Because auc.pr.curve() operates on raw pointers, pointer-level faults (e.g. from NA or mismatched length) occur before any R-level error handling. Wrapping calls in try() or tryCatch() will not preventR-session crashes.

To guard against this, wrap auc.pr.curve() in a "safe" validator that checks for NA values and matching length, for example:

safe_auc.pr.curve <- function(x, y, ...) {
  stopifnot(
    !anyNA(x), !anyNA(y),
    length(x) == length(y)
  )
  auc.pr.curve(x, y, ...)
}

Apply the same pattern to any custom metric functions to ensure input sanity before calling the underlying C++ code.

Visualizing area under the precision recall curve

Use pr.curve() to construct the data.frame and use plot to visualize the area under the curve.

Efficient multi-metric evaluation

To avoid sorting the same probability matrix multiple times (once per class or curve), you can precompute a single set of sort indices and pass it via the indices argument. This reduces the overall cost from O(K·N log N) to O(N log N + K·N).

## presort response
## probabilities
indices <- preorder(response, decreasing = TRUE)

## evaluate area under the precision recall curve
auc.pr.curve(actual, response, indices = indices)

Usage

## S3 method for class 'factor'
auc.pr.curve(
  actual,
  response,
  estimator = 0L,
  method = 0L,
  indices = NULL,
  ...
)

Arguments

actual

A vector length n, and k levels. Can be of integer or factor.

response

A n \times k <double>-matrix of predicted probabilities. The i-th row should sum to 1 (i.e., a valid probability distribution over the k classes). The first column corresponds to the first factor level in actual, the second column to the second factor level, and so on.

estimator

An <integer>-value of length 1 (default: 0).

  • 0 - a named <double>-vector of length k (class-wise)

  • 1 - a <double> value (Micro averaged metric)

  • 2 - a <double> value (Macro averaged metric)

method

A <double> value (default: 0). Defines the underlying method of calculating the area under the curve. If 0 it is calculated using thetrapezoid-method, if 1 it is calculated using the step-method.

indices

An optional n \times k matrix of <integer> values of sorted response probability indices.

...

Arguments passed into other methods.

Value

If estimator is given as

  • 0: a named <double>-vector of length k

  • 1: a <double> value (Micro averaged metric)

  • 2: a <double> value (Macro averaged metric)

References

James, Gareth, et al. An introduction to statistical learning. Vol. 112. No. 1. New York: springer, 2013.

Hastie, Trevor. "The elements of statistical learning: data mining, inference, and prediction." (2009).

Pedregosa, Fabian, et al. "Scikit-learn: Machine learning in Python." the Journal of machine Learning research 12 (2011): 2825-2830.

Examples

## Classes and
## seed
set.seed(1903)
classes <- c("Kebab", "Falafel")

## Generate actual classes
## and response probabilities
actual_classes <- factor(
x = sample(
  x = classes, 
  size = 1e2, 
  replace = TRUE, 
  prob = c(0.7, 0.3)
)
)

response_probabilities <- ifelse(
actual_classes == "Kebab", 
rbeta(sum(actual_classes == "Kebab"), 2, 5), 
rbeta(sum(actual_classes == "Falafel"), 5, 2)
)

## Construct response
## matrix
probability_matrix <- cbind(
response_probabilities,
1 - response_probabilities
)


## Evaluate performance

SLmetrics::auc.pr.curve(
actual   = actual_classes, 
response = probability_matrix
)

</div>
Previouspr.curveNextauc.pr.curve.md

Last updated 1 month ago