Calculation of fit metrics for observed and expected numeric variables (e.g. \(R^2\), \(RMSE\), \(MAE\), \(MAPE\)).
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
)A list with two elements:
fit_metricsA list containing the computed fit metrics:
SQR, SAR, SQT, R2, MSE, RMSE,
MAE, and MAPE.
observed_expectedA data.frame containing observed values, expected values,
residuals, and derived residual measures.
Numeric vector of observed values.
Numeric vector of expected or predicted values.
Logical. If TRUE, diagnostic plots for observed vs. expected values
and relative residual distributions are created.
Character string. Title of the observed vs. expected plot.
Character string. Label of the x-axis.
Character string. Label of the y-axis.
Color of points in the observed vs. expected plot.
Plotting character used for points.
Color of the identity line (\(y = x\)).
Character string. Title of the residuals bar plot.
Numeric. Character expansion factor for legends.
Thomas Wieland
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.
Boehmke B, Greenwell B (2020). Hands-On Machine Learning with R (1st ed.). Taylor & Francis, New York, NY.
metrics
obs <- c(10, 12, 15, 18, 20)
exp <- c(11, 13, 14, 17, 21)
metrics(
observed = obs,
expected = exp
)
Run the code above in your browser using DataLab