calculate_pred_metrics_cv: Calculate Prediction Metrics

View source: R/helper_functions.R

calculate_pred_metrics_cvR Documentation

Calculate Prediction Metrics

Description

Computes Root Mean Squared Error (RMSE) and R-squared.

Usage

calculate_pred_metrics_cv(predictions, actual)

Arguments

predictions

A numeric vector of model predictions.

actual

A numeric vector of the true outcome values.

Details

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.

Value

A list containing the following numeric elements:

  • RMSE: Root Mean Squared Error.

  • R_squared: R-squared (Coefficient of Determination).

Note

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_.

Examples

# --- 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

TemporalForest documentation built on Dec. 23, 2025, 1:06 a.m.