0

I want to compute a series of out of sample forecasts. I need to run a quantile regression that uses the first 20 quarters of data and produces a forecast, stores the last observation from the series, which is the forecast and then re-runs the quantile regression using 21 quarters of data to produce a new forecast for the next quarter, then 22 quarters of data and so on.

I tried using a loop to expand the 'window' for ts data but I can't get the loop to work.

I then tried using xts data but I'm having the same problem


quarters1 <- c(0,3,2,1,0,3,2,1)
years1 <-    c(2,1,1,1,1,0,0,0)
dates1 <- cbind(years1, quarters1)

for (i in 1:nrow(dates1)) {

window(flows, start(flows), (end(flows) - dates1[i,1:2]))
window(rhsflows, start(rhsflows), (end(rhsflows) - dates1[i,1:2]))
window(bbb, start(bbb), (end(bbb) - dates1[i,1:2]))
window(yield, start(yield), (end(yield) - dates1[i,1:2]))
window(usd, start(usd), (end(usd) - dates1[i,1:2]))
window(gdp, start(gdp), (end(gdp) - dates1[i,1:2]))


vg_qt <- dynrq(flows ~ L(rhsflows, nlag) + bbb + yield + usd + gdp, tau = tau_vec, method = "br")

dyhat_qt <- predict(vg_qt)
colnames(dyhat_qt) <- tau_vec


dyhat_qt <- ts(data = dyhat_qt, start = forecastflowsbegin, frequency = ts_freq)

dyhat_qt1 <- tail(dyhat_qt,1)

dyhat_qt_oos <- rbind(dyhat_qt_oos, c(dyhat_qt1))
}

I expect the rows of dyhat_qt_oos to be different but they are all the same

Tess K
  • 1
  • 2
    Welcome to StackOverflow. Your code is not reproducible, you use datasets `flows`, `rhsflows`, etc that are not defined. And function `dynrq` comes from where? In order to ask a better question please read [How to ask a good question](https://stackoverflow.com/help/how-to-ask) and [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve) and [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Rui Barradas May 31 '19 at 07:07
  • `dynrq` comes from the `quantreg` package. Is it necessary for the code to be reproducible? It's a small section of a long piece of code that would take quite a while to make reproducible. – Tess K Jun 02 '19 at 23:28
  • Are you aware that the 6 `window` calls are not necessary to the post? They are basically a waste of time since their return values are not assigned. Maybe that's where the error is, the loop runs the very same `dynrq` regression `nrow(dates1)` times. – Rui Barradas Jun 03 '19 at 09:26
  • Yes! That was it. I actually figured it out this morning. I needed to create temporary variables and I also created a list containing each of the regression results. Thanks very much @ruibarradas for replying and your help! What a relief to have it sorted :) – Tess K Jun 04 '19 at 11:20

0 Answers0