I am currently trying to do a rolling forecast with predict_rolling.
Specifically, I want to do, for example, a 2-step ahead rolling forecasting and want to re-estimate the VAR at every step. I already have a functioning code, that does it for me, but I want to avoid the for loop.
When I try to replicate the code using predict_rolling with n.ahead = 1, I get the correct results, however they differ for n.ahead > 1. I’m not quite sure what the function is doing when I increase the horizon. Do you know what it is causing this difference?
Thank you very much for your help.
Best,
Eric
library(tsDyn)
library(vars)
data <- structure(list(beta_1 = c(6.35815059908977, 6.26392397241388,
5.75584304551968, 5.79468441571414, 5.79866753735267, 5.93702141823254,
5.9802644025435, 5.86669864820461, 6.34140417225121, 6.22403010817456,
6.04156005554047, 5.99436622944328, 6.21876461132087, 6.12521892832898,
6.28785501504558, 6.58416859326718, 6.60860111153987, 6.56413440110398,
6.48242438403672, 6.37777673701131, 6.66598764007499, 6.40439103841177,
6.68523656665178, 6.84779996196864, 6.65828185836342, 6.59294917836496,
6.58247770373732, 6.58151962670905, 6.54178934287671, 6.63472984194905
), beta_2 = c(-0.693981338882855, -0.490176628667609, 0.273659053491197,
0.239555376236275, 0.207269178550827, 0.251612610758922, 0.407559789903953,
0.569886012132651, 0.203137745309771, 0.409355931195391, 0.52231134837746,
0.0989628040415202, -1.12460228733712, -1.1741087534917, -1.89827153876211,
-2.60891196464883, -3.07816904157986, -3.02987212289999, -3.02564037697751,
-3.05186295573602, -4.31703470296141, -4.46324605358408, -5.23741582126552,
-5.53763792734524, -5.21652056632509, -5.18677802920265, -5.14017527231271,
-5.1638258167503, -5.15084057539836, -5.26464530948111), beta_3 = c(2.07909176259473,
1.76313003375624, 1.53657531433667, 2.14690291607906, 2.40466018945562,
0.654662517991894, 0.0325387409714942, -0.53746641768277, -2.10960016880694,
-2.1786830671857, -2.8803651841676, -3.45821461372104, -3.09352055010635,
-3.38860120189217, -3.24589594863622, -2.24785473097812, -1.52644445065669,
-1.35977678028773, -2.6903096555247, -2.80397737054071, -3.75658639513462,
-3.89616413931621, -1.87965311921739, -0.955127483103369, -0.754754537400041,
-1.06798338362736, 1.25463230677968, -0.289233763457811, -0.585463719717287,
-1.73110982341087)), row.names = c(NA, 30L), class = "data.frame")
# 1- step ahead forecast:
horizon <- 1
# Old code I want to get rid of:
fore_factors <- matrix(NA, 20, 3)
for(j in 1 : 20)
{
# 1. Data preparation
fit_factors <- data[1 : (9 + j), ]
# 2. State equation
# 2.1 Estimation
fitting <- VAR(fit_factors, p = 1, type = "const")
# 2.2 Factor Forecasting
x_t1_mu <- as.numeric(fit_factors[nrow(fit_factors), ])
pred <- predict(fitting, n.ahead = horizon, newdata = x_t1_mu)
fore_factors[j,] <- c(pred$fcst$beta_1[horizon], pred$fcst$beta_2[horizon], pred$fcst$beta_3[horizon])
}
# New code:
fit_var <- lineVar(data, lag = 1, include = "const", model = "VAR")
predicted_values <- predict_rolling(fit_var, nroll = 20, refit = 1, n.ahead = horizon) $ pred
# Compare:
predicted_values
fore_factors
# 2 horizon forecasting:
horizon <- 2
# Old code I want to get rid of:
fore_factors <- matrix(NA, 20, 3)
for(j in 1 : 20)
{
# 1. Data preparation
fit_factors <- data[1 : (9 + j), ]
# 2. State equation
# 2.1 Estimation
fitting <- VAR(fit_factors, p = 1, type = "const")
# 2.2 Factor Forecasting
x_t1_mu <- as.numeric(fit_factors[nrow(fit_factors), ])
pred <- predict(fitting, n.ahead = horizon, newdata = x_t1_mu)
fore_factors[j,] <- c(pred$fcst$beta_1[horizon], pred$fcst$beta_2[horizon], pred$fcst$beta_3[horizon])
}
# New code:
fit_var <- lineVar(data, lag = 1, include = "const", model = "VAR")
predicted_values <- predict_rolling(fit_var, nroll = 20, refit = 1, n.ahead = horizon) $ pred
# Compare:
predicted_values
fore_factors
--
You received this message because you are subscribed to the Google Groups "tsdyn" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tsdyn+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tsdyn/cbf572c3-ee07-46cc-a481-6b56b3d9ff32n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tsdyn/CAEYvigJUbXtKHhykjB8czfSgqcoDQcTC_Cufo7zAr%3DLtN22dKw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tsdyn/CAO5oc6V5j_3L2JxvmfMF3pipbS5NR4-p-dEo_efCuReuo1zS7A%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tsdyn/CAEYvigLkWCT1fhseOWKEcPS%3DtuQ2cXNBQgMNshD6A1_OND73GQ%40mail.gmail.com.