n-step ahead forecasting using predict_rolling in R

167 views
Skip to first unread message

ec.o...@gmail.com

unread,
Mar 9, 2021, 11:41:25 AM3/9/21
to tsdyn
Hi Mathieu, 

first of all, thank you very much for writting this package. It has been great so far.

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 

Matthieu S

unread,
Mar 16, 2021, 12:48:48 AM3/16/21
to ts...@googlegroups.com
Hi Eric

I agree, the result is not as expected.  I've entered it as an issue on github: https://github.com/MatthieuStigler/tsDyn/issues/31

But it will take some time to have a look at it, I am afraid you will be better off with your for loop in the meanwhile!

Best,
Matthieu

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

Eric Offner

unread,
Mar 16, 2021, 10:39:46 AM3/16/21
to ts...@googlegroups.com
Hi Matthieu,

no problem at all. Thanks for looking into it. 

I would just very much appreciate it if you could let me know when the issue is fixed.

Best,
Eric

Matthieu S

unread,
Mar 16, 2021, 11:28:55 AM3/16/21
to ts...@googlegroups.com
Hi Eric

The best way to be updated on the bug is for you to subscribe to the github issue itself: https://github.com/MatthieuStigler/tsDyn/issues/31

Best

Eric Offner

unread,
Mar 16, 2021, 2:23:54 PM3/16/21
to ts...@googlegroups.com
Hi Matthieu,

Thanks for the hint.

Best,
Eric

Reply all
Reply to author
Forward
0 new messages