Learn R Programming

EmpiricalDynamics (version 0.1.2)

cross_validate: Cross-Validate Discovered Equation

Description

Performs k-fold or block cross-validation to assess out-of-sample predictive performance of the discovered equation.

Usage

cross_validate(
  equation,
  data,
  response = NULL,
  derivative_col = NULL,
  k = 5,
  method = c("block", "random", "rolling"),
  block_size = NULL,
  horizon = 1,
  refit_derivative = FALSE,
  diff_method = "tvr",
  verbose = TRUE
)

Value

Object of class "cv_result" containing:

rmse

Root mean squared error per fold

mae

Mean absolute error per fold

r_squared

R-squared per fold

mean_rmse

Average RMSE across folds

sd_rmse

Standard deviation of RMSE

predictions

List of predicted vs actual per fold

fold_indices

Indices used for each fold

Arguments

equation

Fitted equation object from fit_specified_equation or symbolic_search. Can also be an object of class lm or nls.

data

Data frame containing all variables.

response

Name of the response column (derivative).

derivative_col

Alias for response (for compatibility).

k

Number of folds for cross-validation.

method

CV method: "random", "block", "rolling".

block_size

For block methods, size of contiguous blocks.

horizon

For rolling CV, forecast horizon.

refit_derivative

Logical; whether to recompute derivatives for each fold (currently unused).

diff_method

Differentiation method if refitting (currently unused).

verbose

Print progress.

Examples

Run this code
# \donttest{
# Toy example using lm
data <- data.frame(
  time = 1:50,
  y = seq(1, 10, length.out = 50) + stats::rnorm(50, sd = 0.1)
)
# Simple linear model as a proxy for a discovered equation
model <- stats::lm(y ~ time, data = data)

# Run cross-validation
cv_res <- cross_validate(
  equation = model,
  data = data,
  response = "y",
  k = 3,
  method = "random"
)
print(cv_res)
# }

Run the code above in your browser using DataLab