Learn R Programming

{SLmetrics}: Machine learning performance evaluation on steroids

{SLmetrics} is a lightweight R package written in C++ and {Rcpp} for memory-efficient and lightning-fast machine learning performance evaluation; it’s like using a supercharged {yardstick} but without the risk of soft to super-hard deprecations. {SLmetrics} covers both regression and classification metrics and provides (almost) the same array of metrics as {scikit-learn} and {PyTorch} all without {reticulate} and the Python compile-run-(crash)-debug cycle.

Depending on the mood and alignment of planets {SLmetrics} stands for Supervised Learning metrics, or Statistical Learning metrics. If {SLmetrics} catches on, the latter will be the core philosophy and include unsupervised learning metrics. If not, then it will remain a {pkg} for Supervised Learning metrics, and a sandbox for me to develop my C++ skills.

:rocket: Gettting Started

Below you’ll find instructions to install {SLmetrics} and get started with your first metric, the Root Mean Squared Error (RMSE).

:package: CRAN version

## install latest CRAN build
install.packages("SLmetrics")

:books: Basic Usage

Below is a minimal example demonstrating how to compute both unweighted and weighted RMSE.

library(SLmetrics)

actual    <- c(10.2, 12.5, 14.1)
predicted <- c(9.8, 11.5, 14.2)
weights   <- c(0.2, 0.5, 0.3)

cat(
  "Root Mean Squared Error", rmse(
    actual    = actual,
    predicted = predicted,
  ),
  "Root Mean Squared Error (weighted)", weighted.rmse(
    actual    = actual,
    predicted = predicted,
    w         = weights
  ),
  sep = "\n"
)
#> Root Mean Squared Error
#> 0.6244998
#> Root Mean Squared Error (weighted)
#> 0.7314369

That’s all! Now you can explore the rest of this README for in-depth usage, performance comparisons, and more details about {SLmetrics}.

:information_source: Why?

Machine learning can be a complicated task; the steps from feature engineering to model deployment require carefully measured actions and decisions. One low-hanging fruit to simplify this process is performance evaluation.

At its core, performance evaluation is essentially just comparing two vectors - a programmatically and, at times, mathematically trivial step in the machine learning pipeline, but one that can become complicated due to:

  1. Dependencies and potential deprecations
  2. Needlessly complex or repetitive arguments
  3. Performance and memory bottlenecks at scale

{SLmetrics} solves these issues by being:

  1. Fast: Powered by C++ and {Rcpp}
  2. Memory-efficient: Everything is structured around pointers and references
  3. Lightweight: Only depends on {Rcpp} and {lattice}
  4. Simple: S3-based, minimal overhead, and flexible inputs

Performance evaluation should be plug-and-play and “just work” out of the box - there’s no need to worry about quasiquations, dependencies, deprecations, or variations of the same functions relative to their arguments when using {SLmetrics}.

:zap: Performance Comparison

One, obviously, can’t build an R-package on C++ and {Rcpp} without a proper pissing contest at the urinals - below is a comparison in execution time and memory efficiency of two simple cases that any {pkg} should be able to handle gracefully; computing a 2 x 2 confusion matrix and computing the RMSE[^1].

:fast_forward: Speed comparison

As shown in the chart, {SLmetrics} maintains consistently low(er) execution times across different sample sizes.

:floppy_disk: Memory-efficiency

Below are the results for garbage collections and total memory allocations when computing a 2×2 confusion matrix (N = 1e7) and RMSE (N = 1e7) [^2]. Notice that {SLmetrics} requires no GC calls for these operations.

IterationsGarbage Collections [gc()]gc() pr. secondMemory Allocation (MB)
{SLmetrics}10000.000
{yardstick}1001904.44381
{MLmetrics}1001864.50381
{mlr3measures}1003713.93916

2 x 2 Confusion Matrix (N = 1e7)

IterationsGarbage Collections [gc()]gc() pr. secondMemory Allocation (MB)
{SLmetrics}10000.000
{yardstick}1001494.30420
{MLmetrics}100152.0076
{mlr3measures}100121.2976

RMSE (N = 1e7)

In both tasks, {SLmetrics} remains extremely memory-efficient, even at large sample sizes.

[!IMPORTANT]

From {bench} documentation: Total amount of memory allocated by R while running the expression. Memory allocated outside the R heap, e.g. by malloc() or new directly is not tracked, take care to avoid misinterpreting the results if running code that may do this.

:information_source: Basic usage

In its simplest form, {SLmetrics}-functions work directly with pairs of <numeric> vectors (for regression) or <factor> vectors (for classification). Below we demonstrate this on two well-known datasets, mtcars (regression) and iris (classification).

:books: Regression

We first fit a linear model to predict mpg in the mtcars dataset, then compute the in-sample RMSE:

## Evaluate a linear model on mpg (mtcars)
model <- lm(mpg ~ ., data = mtcars)
rmse(mtcars$mpg, fitted(model))
#> [1] 2.146905

:books: Classification

Now we recode the iris dataset into a binary problem (“virginica” vs. “others”) and fit a logistic regression. Then we generate predicted classes, compute the confusion matrix and summarize it.

## 1) recode iris
## to binary problem
iris$species_num <- as.numeric(
  iris$Species == "virginica"
)

## 2) fit the logistic
## regression
model <- glm(
  formula = species_num ~ Sepal.Length + Sepal.Width,
  data    = iris,
  family  = binomial(
    link = "logit"
  )
)

## 3) generate predicted
## classes
predicted <- factor(
  as.numeric(
    predict(model, type = "response") > 0.5
  ),
  levels = c(1,0),
  labels = c("Virginica", "Others")
)

## 4) generate actual
## values as factor
actual <- factor(
  x = iris$species_num,
  levels = c(1,0),
  labels = c("Virginica", "Others")
)
## 4) generate
## confusion matrix
summary(
  confusion_matrix <- cmatrix(
    actual    = actual,
    predicted = predicted
  )
)
#> Confusion Matrix (2 x 2) 
#> ================================================================================
#>           Virginica Others
#> Virginica        35     15
#> Others           14     86
#> ================================================================================
#> Overall Statistics (micro average)
#>  - Accuracy:          0.81
#>  - Balanced Accuracy: 0.78
#>  - Sensitivity:       0.81
#>  - Specificity:       0.81
#>  - Precision:         0.81

:information_source: Enable OpenMP

[!IMPORTANT]

OpenMP support in {SLmetrics} is experimental. Use it with caution, as performance gains and stability may vary based on your system configuration and workload.

You can control OpenMP usage within {SLmetrics} using openmp.on() and openmp.off() . Below are examples demonstrating how to enable and disable OpenMP:

## enable OpenMP
SLmetrics::openmp.on()
#> OpenMP enabled!

## disable OpenMP
SLmetrics::openmp.off()
#> OpenMP disabled!

To illustrate the impact of OpenMP on performance, consider the following benchmarks for calculating entropy on a 1,000,000 x 200 matrix over 100 iterations[^3].

:books: Entropy without OpenMP

IterationsRuntime (sec)Garbage Collections [gc()]gc() pr. secondMemory Allocation (MB)
1000.86000

1e6 x 200 matrix without OpenMP

:books: Entropy with OpenMP

IterationsRuntime (sec)Garbage Collections [gc()]gc() pr. secondMemory Allocation (MB)
1000.15000

1e6 x 200 matrix with OpenMP

:package: Install from source

Github release

## install github release
pak::pak(
    pkg = "serkor1/SLmetrics@*release",
    ask = FALSE
)

Nightly build

Clone repository with submodules

git clone --recurse-submodules https://github.com/serkor1/SLmetrics.git

Installing with build tools

make build

Installing with {pak}

## install nightly build
pak::pak(
    pkg = ".",
    ask = FALSE
)

:information_source: Code of Conduct

Please note that the {SLmetrics} project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

[^1]: The source code is available here and here.

[^2]: The source code is available here.

[^3]: The source code is available here.

Copy Link

Version

Install

install.packages('SLmetrics')

Monthly Downloads

137

Version

0.3-4

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Serkan Korkmaz

Last Published

June 21st, 2025

Functions in SLmetrics (0.3-4)

auc.roc.curve.factor

Area under the Receiver Operator Characteristics Curve
SLmetrics-package

SLmetrics: Machine Learning Performance Evaluation on Steroids
baccuracy

Balanced Accuracy
auc.pr.curve.factor

Area under the Precision Recall Curve
accuracy.cmatrix

Accuracy
baccuracy.cmatrix

Balanced Accuracy
accuracy

Accuracy
accuracy.factor

Accuracy
auc.pr.curve

Area under the Precision Recall Curve
auc.roc.curve

Area under the Receiver Operator Characteristics Curve
brier.score

Brier Score
cmatrix.factor

Confusion Matrix
brier.score.matrix

Brier Score
cross.entropy

Cross Entropy
cross.entropy.matrix

Cross Entropy
ckappa

Cohen's \(\kappa\)-Statistic
cmatrix

Confusion Matrix
ckappa.factor

Cohen's \(\kappa\)-Statistic
ckappa.cmatrix

Cohen's \(\kappa\)-Statistic
baccuracy.factor

Balanced Accuracy
dor

Diagnostic Odds Ratio
fbeta.cmatrix

\(f_{\beta}\)
fdr.cmatrix

False Discovery Rate
dor.cmatrix

Diagnostic Odds Ratio
dor.factor

Diagnostic Odds Ratio
fer

False Omission Rate
fdr

False Discovery Rate
fbeta

\(f_{\beta}\)
fdr.factor

False Discovery Rate
fbeta.factor

\(f_{\beta}\)
fer.cmatrix

False Omission Rate
fpr.cmatrix

False Positive Rate
hammingloss

Hamming Loss
fpr.factor

False Positive Rate
fmi.factor

Fowlkes Mallows Index
fmi

Fowlkes Mallows Index
hammingloss.cmatrix

Hamming Loss
fpr

False Positive Rate
fer.factor

False Omission Rate
fmi.cmatrix

Fowlkes Mallows Index
hammingloss.factor

Hamming Loss
jaccard

Jaccard Index
mcc.cmatrix

Matthews Correlation Coefficient
jaccard.factor

Jaccard Index
mcc

Matthews Correlation Coefficient
logloss.factor

Logarithmic Loss
jaccard.cmatrix

Jaccard Index
logloss.integer

Logarithmic Loss
mcc.factor

Matthews Correlation Coefficient
logloss

Logarithmic Loss
npv.factor

Negative Predictive Value
nlr.cmatrix

Negative Likelihood Ratio
plr.factor

Positive Likelihood Ratio
plr.cmatrix

Positive Likelihood Ratio
pr.curve

Precision Recall Curve
plr

Positive Likelihood Ratio
nlr

Negative Likelihood Ratio
nlr.factor

Negative Likelihood Ratio
npv.cmatrix

Negative Predictive Value
npv

Negative Predictive Value
relative.entropy.matrix

Relative Entropy
precision.cmatrix

Precision
precision.factor

Precision
roc.curve

Reciever Operator Characteristics
recall.factor

Recall
precision

Precision
recall

Recall
recall.cmatrix

Recall
pr.curve.factor

Precision Recall Curve
relative.entropy

Relative Entropy
weighted.baccuracy.factor

Balanced Accuracy
weighted.auc.roc.curve.factor

Area under the Receiver Operator Characteristics Curve
roc.curve.factor

Reciever Operator Characteristics
shannon.entropy.matrix

Shannon Entropy
specificity

Specificity
weighted.accuracy.factor

Accuracy
specificity.cmatrix

Specificity
specificity.factor

Specificity
weighted.auc.pr.curve.factor

Area under the Precision Recall Curve
shannon.entropy

Shannon Entropy
weighted.brier.score.matrix

Brier Score
weighted.dor.factor

Positive Likelihood Ratio
weighted.ckappa.factor

Cohen's \(\kappa\)-Statistic
weighted.fbeta.factor

\(f_{\beta}\)
weighted.fer.factor

False Omission Rate
weighted.fpr.factor

False Positive Rate
weighted.hammingloss.factor

Hamming Loss
weighted.fmi.factor

Fowlkes Mallows Index
weighted.fdr.factor

False Discovery Rate
weighted.cmatrix.factor

Confusion Matrix
weighted.plr.factor

Positive Likelihood Ratio
weighted.precision.factor

Precision
weighted.recall.factor

Recall
weighted.jaccard.factor

Jaccard Index
weighted.logloss.integer

Logarithmic Loss
weighted.mcc.factor

Matthews Correlation Coefficient
weighted.specificity.factor

Specificity
zerooneloss

Zero-One Loss
weighted.roc.curve.factor

Reciever Operator Characteristics
zerooneloss.factor

Zero-One Loss
huberloss

Huber Loss
ccc.numeric

Concordance Correlation Coefficient
deviance.tweedie

Tweedie Deviance
gmse.numeric

Geometric Mean Squared Error
gmse

Geometric Mean Squared Error
deviance.gamma.numeric

Gamma Deviance
mae.numeric

Mean Absolute Error
mape.numeric

Mean Absolute Percentage Error
mpe.numeric

Mean Percentage Error
pinball

Pinball Loss
rmsle.numeric

Root Mean Squared Logarithmic Error
rmse.numeric

Root Mean Squared Error
weighted.ccc.numeric

Concordance Correlation Coefficient
rsq

\(r^2\)
weighted.deviance.gamma.numeric

Gamma Deviance
rrse

Root Relative Squared Error
rrmse.numeric

Relative Root Mean Squared Error
weighted.deviance.poisson.numeric

Poisson Deviance
smape.numeric

Symmetric Mean Absolutte Percentage Error
weighted.pr.curve.factor

Precision Recall Curve
weighted.logloss.factor

Logarithmic Loss
wine.quality

Wine quality dataset
zerooneloss.cmatrix

Zero-One Loss
banknote

Banknote authentication dataset
deviance.poisson.numeric

Poisson Deviance
deviance.gamma

Gamma Deviance
mae

Mean Absolute Error
mse

Mean Squared Error
mpe

Mean Percentage Error
maape.numeric

Mean Arctangent Absolute Percentage Error
mape

Mean Absolute Percentage Error
rrmse

Relative Root Mean Squared Error
rae

Relative Absolute Error
mse.numeric

Mean Squared Error
rsq.numeric

\(r^2\)
weighted.mpe.numeric

Mean Percentage Error
weighted.pinball.numeric

Pinball Loss
weighted.mae.numeric

Mean Absolute Error
weighted.rae.numeric

Relative Absolute Error
weighted.maape.numeric

Mean Arctangent Absolute Percentage Error
weighted.gmse.numeric

Geometric Mean Squared Error
OpenMP

Control OpenMP
weighted.rmsle.numeric

Root Mean Squared Logarithmic Error
weighted.rmse.numeric

Root Mean Squared Error
weighted.rrse.numeric

Root Relative Squared Error
presort.matrix

Presort Matrices
weighted.nlr.factor

Negative Likelihood Ratio
weighted.npv.factor

Negative Predictive Value
obesity

Obesity levels dataset
ccc

Concordance Correlation Coefficient
weighted.zerooneloss.factor

Zero-One Loss
deviance.poisson

Poisson Deviance
maape

Mean Arctangent Absolute Percentage Error
pinball.numeric

Pinball Loss
rmse

Root Mean Squared Error
rmsle

Root Mean Squared Logarithmic Error
rrse.numeric

Root Relative Squared Error
weighted.huberloss.numeric

Huber Loss
weighted.mse.numeric

Mean Squared Error
auc.xy.numeric

Area under the curve
weighted.rrmse.numeric

Relative Root Mean Squared Error
preorder

Preorder Matrices
weighted.rsq.numeric

\(r^2\)
deviance.tweedie.numeric

Tweedie Deviance
huberloss.numeric

Huber Loss
weighted.deviance.tweedie.numeric

Tweedie Deviance
auc.xy

Area under the curve
weighted.smape.numeric

Symmetric Mean Absolutte Percentage Error
preorder.matrix

Preorder Matrices
presort

Presort Matrices
rae.numeric

Relative Absolute Error
smape

Symmetric Mean Absolutte Percentage Error
weighted.mape.numeric

Mean Absolute Percentage Error