🚀
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. Recall

precision.cmatrix.md

precision.cmatrix

R Documentation

Precision

Description

A generic S3 function to compute the precision score for a classification model. This function dispatches to S3 methods inprecision() 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 precision() 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 precision() in a "safe" validator that checks for NA values and matching length, for example:

safe_precision <- function(x, y, ...) {
  stopifnot(
    !anyNA(x), !anyNA(y),
    length(x) == length(y)
  )
  precision(x, y, ...)
}

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

Efficient multi-metric evaluation

For multiple performance evaluations of a classification model, first compute the confusion matrix once via cmatrix(). All other performance metrics can then be derived from this one object via S3 dispatching:

## compute confusion matrix
confusion_matrix <- cmatrix(actual, predicted)

## evaluate precision
## via S3 dispatching
precision(confusion_matrix)

## additional performance metrics
## below

The precision.factor() method calls cmatrix() internally, so explicitly invoking precision.cmatrix() yourself avoids duplicate computation, yielding significant speed and memory effciency gains when you need multiple evaluation metrics.

Usage

## S3 method for class 'cmatrix'
precision(x, estimator = 0L, na.rm = TRUE, ...)

Arguments

x

A confusion matrix created cmatrix().

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)

na.rm

A <logical> value of length 1 (default: TRUE). If TRUE, NA values are removed from the computation. This argument is only relevant when micro != NULL. Whenna.rm = TRUE, the computation corresponds tosum(c(1, 2, NA), na.rm = TRUE) / length(na.omit(c(1, 2, NA))). When na.rm = FALSE, the computation corresponds tosum(c(1, 2, NA), na.rm = TRUE) / length(c(1, 2, NA)).

...

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)

Other names

The precision has other names depending on research field:

  • Positive Predictive Value, ppv()

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
## and predicted classes
actual_classes <- factor(
x = sample(x = classes, size = 1e3, replace = TRUE),
levels = c("Kebab", "Falafel")
)

predicted_classes <- factor(
x = sample(x = classes, size = 1e3, replace = TRUE),
levels = c("Kebab", "Falafel")
)


## Construct confusion
## matrix
confusion_matrix <- SLmetrics::cmatrix(
actual    = actual_classes,
predicted = predicted_classes
)

## Evaluate performance
SLmetrics::precision(confusion_matrix)

</div>
Previousplr.cmatrix.mdNextrecall

Last updated 1 month ago