metrics: Fit metrics of observed and expected numeric variables

View source: R/swash.R

metricsR Documentation

Fit metrics of observed and expected numeric variables

Description

Calculation of fit metrics for observed and expected numeric variables (e.g. R^2, RMSE, MAE, MAPE).

Usage

metrics(
  observed,
  expected,
  plot = TRUE,
  plot.main = "Observed vs. expected",
  xlab = "Observed",
  ylab = "Expected",
  point.col = "blue",
  point.pch = 19,
  line.col = "red",
  plot_residuals.main = "Residuals",
  legend.cex = 0.7
)

Arguments

observed

Numeric vector of observed values.

expected

Numeric vector of expected or predicted values.

plot

Logical. If TRUE, diagnostic plots for observed vs. expected values and relative residual distributions are created.

plot.main

Character string. Title of the observed vs. expected plot.

xlab

Character string. Label of the x-axis.

ylab

Character string. Label of the y-axis.

point.col

Color of points in the observed vs. expected plot.

point.pch

Plotting character used for points.

line.col

Color of the identity line (y = x).

plot_residuals.main

Character string. Title of the residuals bar plot.

legend.cex

Numeric. Character expansion factor for legends.

Details

The function computes several goodness-of-fit metrics comparing observed and expected numeric values. In addition to classical error measures such as mean squared error (MSE), root mean squared error (RMSE), mean absolute error (MAE), and mean absolute percentage error (MAPE), the coefficient of determination (R^2) is calculated.

If plot = TRUE, the function produces:

  • a scatter plot of observed versus expected values including the identity line,

  • a bar plot of relative residual frequencies.

Value

A list with two elements:

fit_metrics

A list containing the computed fit metrics: SQR, SAR, SQT, R2, MSE, RMSE, MAE, and MAPE.

observed_expected

A data.frame containing observed values, expected values, residuals, and derived residual measures.

Author(s)

Thomas Wieland

References

Boehmke B, Greenwell B (2020). Hands-On Machine Learning with R (1st ed.). Taylor & Francis, New York, NY.

See Also

metrics

Examples

obs <- c(10, 12, 15, 18, 20)
exp <- c(11, 13, 14, 17, 21)

metrics(
  observed = obs,
  expected = exp
)

swash documentation built on Feb. 15, 2026, 5:07 p.m.