Forescast Strategy - Rolling Window


library(zoo)
library(forecast)
library(tidyverse)
library(dplyr)
data <- read.csv("M4_Forecast uncertainty/Thailand_M.csv")
data$date <- as.Date(data$dateid01)
inflation <- zoo(data$p, order.by = data$date)


Parameters


length of out-of-sample forecast = 6
number of rolling samples = 54
last date in the first estimation period = December, 2008
rolling forecast window = 36

h <- 6
num_rolling <- 54
window_length <- 36
last_est_date <- as.Date("2008-12-01")
end_index <- which(index(inflation) == last_est_date)
start_index <- end_index - window_length + 1


Loop for number of forecasts assigned for AR(1)


results_rolling_ar1 <- data.frame(
  step = 1:num_rolling,
  est_start = as.Date(NA),
  est_end = as.Date(NA),
  forecast_target = as.Date(NA),
  forecast_value = NA,
  actual_value = NA
)

# Rolling forecast
for (i in 0:(num_rolling - 1)) {
  est_start_index <- start_index + i
  est_end_index <- est_start_index + window_length - 1
  forecast_index <- est_end_index + h
  
  if (forecast_index > length(inflation)) break
  
  est_start_date <- index(inflation)[est_start_index]
  est_end_date <- index(inflation)[est_end_index]
  forecast_target_date <- index(inflation)[forecast_index]
  
  # Extract training data
  train_series <- window(inflation, start = est_start_date, end = est_end_date)
  train_ts <- ts(as.numeric(train_series), start = c(2003, 1), frequency = 12)
  
  # Fit AR(1)
  fit <- Arima(train_ts, order = c(1, 0, 0))
  fc <- forecast(fit, h = h)
  
  forecast_value <- fc$mean[h]
  actual_value <- as.numeric(inflation[forecast_index])
  
  results_rolling_ar1[i + 1, ] <- list(
    step = i + 1,
    est_start = est_start_date,
    est_end = est_end_date,
    forecast_target = forecast_target_date,
    forecast_value = forecast_value,
    actual_value = actual_value
  )
}

print(head(results_rolling_ar1, 10))
##    step  est_start    est_end forecast_target forecast_value actual_value
## 1     1 2006-01-01 2008-12-01      2009-06-01    1.836109321        -3.96
## 2     2 2006-02-01 2009-01-01      2009-07-01    0.864879189        -4.32
## 3     3 2006-03-01 2009-02-01      2009-08-01    1.115594948        -1.06
## 4     4 2006-04-01 2009-03-01      2009-09-01    0.933843253        -0.91
## 5     5 2006-05-01 2009-04-01      2009-10-01    0.219297290         0.39
## 6     6 2006-06-01 2009-05-01      2009-11-01   -2.075145301         1.94
## 7     7 2006-07-01 2009-06-01      2009-12-01   -2.869888824         3.46
## 8     8 2006-08-01 2009-07-01      2010-01-01   -3.311305217         3.98
## 9     9 2006-09-01 2009-08-01      2010-02-01    0.001299113         3.55
## 10   10 2006-10-01 2009-09-01      2010-03-01    0.089269628         3.33


Calculate RMSE

results_rolling_ar1$error <- results_rolling_ar1$forecast_value - results_rolling_ar1$actual_value
results_rolling_ar1$sq_error <- results_rolling_ar1$error^2
rmse_rolling_ar1 <- sqrt(mean(results_rolling_ar1$sq_error, na.rm = TRUE))
cat("RMSE for AR(1), Rolling Window, Horizon = 6:", round(rmse_rolling_ar1, 4), "\n")
## RMSE for AR(1), Rolling Window, Horizon = 6: 2.1004