Learn R Programming

valueprhr (version 0.1.0)

rolling_window_cv: Rolling Window Cross-Validation

Description

Performs time-series cross-validation using rolling windows, comparing fixed effects and Mundlak models.

Usage

rolling_window_cv(
  panel_data,
  window_sizes = c(20L, 30L),
  step_size = 2L,
  test_horizon = 3L,
  verbose = TRUE
)

Value

A data frame with validation results for each window, including:

window_size

Training window size

window_start

Start year of training window

window_end

End year of training window

rmse_fe_all

RMSE for FE model on all test observations

rmse_m_all

RMSE for Mundlak model on all test observations

n_test_common

Number of test obs from sectors in training

n_test_new

Number of test obs from new sectors

Arguments

panel_data

Data frame in panel format.

window_sizes

Integer vector of training window sizes. Default c(20, 30).

step_size

Integer step between windows. Default 2.

test_horizon

Integer number of periods to forecast. Default 3.

verbose

Logical. Print progress. Default TRUE.

Details

For each rolling window, the function fits both a fixed effects model (by sector) and a Mundlak CRE model, then evaluates predictions on the test period. Results are separated by whether test sectors were seen during training (common) or not (new).

Examples

Run this code
# \donttest{
if (requireNamespace("plm", quietly = TRUE)) {
  set.seed(123)
  panel <- data.frame(
    year = rep(2000:2029, 5),
    sector = rep(LETTERS[1:5], each = 30),
    log_direct = rnorm(150, 5, 0.5),
    log_production = rnorm(150, 5, 0.5)
  )
  panel$log_production <- panel$log_direct * 0.95 + rnorm(150, 0, 0.1)

  cv_results <- rolling_window_cv(panel, window_sizes = c(15, 20))
  print(head(cv_results))
}
# }

Run the code above in your browser using DataLab