View source: R/helper_functions.R
| calculate_pred_metrics_cv | R Documentation |
Computes Root Mean Squared Error (RMSE) and R-squared.
calculate_pred_metrics_cv(predictions, actual)
predictions |
A numeric vector of model predictions. |
actual |
A numeric vector of the true outcome values. |
The function is robust to missing values (NA) within the input vectors, as they
are removed prior to calculation (na.rm = TRUE). It also handles multiple
edge cases, returning NA for both metrics if inputs are NULL, empty, contain only
NAs, or are of unequal length. If the variance of actual values is
near zero, R_squared is handled safely.
A list containing the following numeric elements:
RMSE: Root Mean Squared Error.
R_squared: R-squared (Coefficient of Determination).
Inputs are numeric vectors of equal length. NAs are removed via
na.rm = TRUE; if inputs are NULL/empty/all-NA/length-mismatch, both
metrics are returned as NA_real_.
# --- Example for an internal function ---
# Example predicted values from a model
predicted_values <- c(2.5, 3.8, 6.1, 7.9)
# The corresponding actual, true values
actual_values <- c(2.2, 4.1, 5.9, 8.3)
# Calculate the prediction metrics
metrics <- calculate_pred_metrics_cv(
predictions = predicted_values,
actual = actual_values
)
print(metrics)
#> Expected output:
#> $RMSE
#> [1] 0.3082207
#> $R_squared
#> [1] 0.981269
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.