<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/direct-forecast-x-recursive-forecast/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://insightr.wordpress.com/2018/01/10/direct-forecast-x-recursive-forecast/"> R – insightR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><strong>By Gabriel Vasconcelos</strong></p>
<p>When dealing with forecasting models there is an issue that generates a lot of confusion, which is the difference between direct and recursive forecasts. I believe most people are more used to recursive forecasts because they are the first we learn when studying ARIMA models.</p>
<p><span></span></p>
<p>Suppose you want to forecast the variable <img alt="y" title="y" src="https://s0.wp.com/latex.php?latex=y&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex">, <img alt="h" title="h" src="https://s0.wp.com/latex.php?latex=h&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> steps ahead using only past information of <img alt="y" title="y" src="https://s0.wp.com/latex.php?latex=y&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> and consider the two equations below:</p>
<p><img alt="displaystyle y_{t+1} = alpha_0 + phi_1 y_{t} + varepsilon_{t+1}" title="displaystyle y_{t+1} = alpha_0 + phi_1 y_{t} + varepsilon_{t+1}" src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+y_%7Bt%2B1%7D+%3D+%5Calpha_0+%2B+%5Cphi_1+y_%7Bt%7D+%2B+%5Cvarepsilon_%7Bt%2B1%7D&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"><br>
<img alt="displaystyle y_{t+h} = alpha_0 + phi_1 y_{t} + varepsilon_{t+h}" title="displaystyle y_{t+h} = alpha_0 + phi_1 y_{t} + varepsilon_{t+h}" src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+y_%7Bt%2Bh%7D+%3D+%5Calpha_0+%2B+%5Cphi_1+y_%7Bt%7D+%2B+%5Cvarepsilon_%7Bt%2Bh%7D&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>The first equation is an AR(1) and the second equation is a more general version where we estimate a model directly to the forecasting horizon we want. Now let’s see how the one step ahead forecast would be for each model. In the first equation we would have <img alt="hat{y}_{t+1} = hat{alpha}_0 + hat{phi_1} y_{t}" title="hat{y}_{t+1} = hat{alpha}_0 + hat{phi_1} y_{t}" src="https://s0.wp.com/latex.php?latex=%5Chat%7By%7D_%7Bt%2B1%7D+%3D+%5Chat%7B%5Calpha%7D_0+%2B+%5Chat%7B%5Cphi_1%7D+y_%7Bt%7D&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> and in the second equation we must make <img alt="h=1" title="h=1" src="https://s0.wp.com/latex.php?latex=h%3D1&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> to obtain the same result.</p>
<p>For two steps ahead things start to change. In the first equation we have:</p>
<p><img alt="displaystyle hat{y}_{t+2}=hat{alpha}_0 + hat{phi_1} hat{y}_{t+1} = hat{alpha}_0 (1+ hat{phi_1}) + hat{phi_1}^2 y_{t}" title="displaystyle hat{y}_{t+2}=hat{alpha}_0 + hat{phi_1} hat{y}_{t+1} = hat{alpha}_0 (1+ hat{phi_1}) + hat{phi_1}^2 y_{t}" src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Chat%7By%7D_%7Bt%2B2%7D%3D%5Chat%7B%5Calpha%7D_0+%2B+%5Chat%7B%5Cphi_1%7D+%5Chat%7By%7D_%7Bt%2B1%7D+%3D+%5Chat%7B%5Calpha%7D_0+%281%2B+%5Chat%7B%5Cphi_1%7D%29+%2B+%5Chat%7B%5Cphi_1%7D%5E2+y_%7Bt%7D&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>and in the second:</p>
<p><img alt="displaystyle hat{y}_{t+2}=hat{alpha}_{0,2} + hat{phi}_{1,2} y_{t}" title="displaystyle hat{y}_{t+2}=hat{alpha}_{0,2} + hat{phi}_{1,2} y_{t}" src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Chat%7By%7D_%7Bt%2B2%7D%3D%5Chat%7B%5Calpha%7D_%7B0%2C2%7D+%2B+%5Chat%7B%5Cphi%7D_%7B1%2C2%7D+y_%7Bt%7D&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>Note that there is an extra number 2 subscript in the coefficients of the equation above. It indicates that both <img alt="alpha_0" title="alpha_0" src="https://s0.wp.com/latex.php?latex=%5Calpha_0&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> and <img alt="phi_1" title="phi_1" src="https://s0.wp.com/latex.php?latex=%5Cphi_1&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> will depend on the choice of <img alt="h" title="h" src="https://s0.wp.com/latex.php?latex=h&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex">. Now we can generalize both cases to any <img alt="h" title="h" src="https://s0.wp.com/latex.php?latex=h&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> in order to have:</p>
<p><img alt="displaystyle hat{y}_{t+h}=hat{alpha}_0sum_{i=1}^h hat{phi}_1^{i-1}+hat{phi}_1^hy_t" title="displaystyle hat{y}_{t+h}=hat{alpha}_0sum_{i=1}^h hat{phi}_1^{i-1}+hat{phi}_1^hy_t" src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Chat%7By%7D_%7Bt%2Bh%7D%3D%5Chat%7B%5Calpha%7D_0%5Csum_%7Bi%3D1%7D%5Eh+%5Chat%7B%5Cphi%7D_1%5E%7Bi-1%7D%2B%5Chat%7B%5Cphi%7D_1%5Ehy_t&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>
<img alt="displaystyle hat{y}_{t+h}=hat{alpha}_{0,h} + hat{phi}_{1,h} y_{t}" title="displaystyle hat{y}_{t+h}=hat{alpha}_{0,h} + hat{phi}_{1,h} y_{t}" src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Chat%7By%7D_%7Bt%2Bh%7D%3D%5Chat%7B%5Calpha%7D_%7B0%2Ch%7D+%2B+%5Chat%7B%5Cphi%7D_%7B1%2Ch%7D+y_%7Bt%7D&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>The first case is called recursive forecast and the second case is called direct forecast. In the recursive forecast we only need to estimate one model and use its coefficients to iterate on the forecasting horizon until we have the horizon we want. In the direct forecast we need to estimate one different model for each forecasting horizon but we do not need to iterate the forecast. The first out-of-sample prediction of the direct forecast will be already on the desired horizon.</p>
<h2>Multivariate Problems</h2>
<p>Now suppose we want to forecast <img alt="y" title="y" src="https://s0.wp.com/latex.php?latex=y&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> using past information of <img alt="y" title="y" src="https://s0.wp.com/latex.php?latex=y&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> and <img alt="x" title="x" src="https://s0.wp.com/latex.php?latex=x&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex">. The recursive model would be:</p>
<p><img alt="displaystyle y_{t+1}=alpha_0 + phi_1 y_t + gamma_1 x_t +varepsilon_{t+1}" title="displaystyle y_{t+1}=alpha_0 + phi_1 y_t + gamma_1 x_t +varepsilon_{t+1}" src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+y_%7Bt%2B1%7D%3D%5Calpha_0+%2B+%5Cphi_1+y_t+%2B+%5Cgamma_1+x_t+%2B%5Cvarepsilon_%7Bt%2B1%7D&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>The one step ahead forecast would be:</p>
<p>
<img alt="displaystyle hat{y}_{t+1}=hat{alpha}_0 + hat{phi}_1 y_t + hat{gamma}_1 x_t" title="displaystyle hat{y}_{t+1}=hat{alpha}_0 + hat{phi}_1 y_t + hat{gamma}_1 x_t" src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Chat%7By%7D_%7Bt%2B1%7D%3D%5Chat%7B%5Calpha%7D_0+%2B+%5Chat%7B%5Cphi%7D_1+y_t+%2B+%5Chat%7B%5Cgamma%7D_1+x_t&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>The two step ahead forecast would be:</p>
<p>
<img alt="displaystyle hat{y}_{t+2}=hat{alpha}_0 + hat{phi}_1 hat{y}_{t+1} + hat{gamma}_1 x_{t+1}" title="displaystyle hat{y}_{t+2}=hat{alpha}_0 + hat{phi}_1 hat{y}_{t+1} + hat{gamma}_1 x_{t+1}" src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+%5Chat%7By%7D_%7Bt%2B2%7D%3D%5Chat%7B%5Calpha%7D_0+%2B+%5Chat%7B%5Cphi%7D_1+%5Chat%7By%7D_%7Bt%2B1%7D+%2B+%5Chat%7B%5Cgamma%7D_1+x_%7Bt%2B1%7D&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>
Now we have a problem. In the two steps ahead forecast we can just replace <img alt="hat{y}_{t+1}" title="hat{y}_{t+1}" src="https://s0.wp.com/latex.php?latex=%5Chat%7By%7D_%7Bt%2B1%7D&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> by the one step ahead equation just like we did in the univariate case. However, we do not have a way to obtain <img alt="x_{t+1}" title="x_{t+1}" src="https://s0.wp.com/latex.php?latex=x_%7Bt%2B1%7D&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex">. In fact, if we use other variables in recursive forecasts we must also forecast these variables in a Vector Autorregressive (VAR) framework for example. In the direct case nothing changes: we could just estimate an equation for <img alt="y_{t+h}" title="y_{t+h}" src="https://s0.wp.com/latex.php?latex=y_%7Bt%2Bh%7D&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> on <img alt="y_t" title="y_t" src="https://s0.wp.com/latex.php?latex=y_t&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> and <img alt="x_t" title="x_t" src="https://s0.wp.com/latex.php?latex=x_t&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex">.</p>
<h2>Example</h2>
<p>In this example we are going to forecast the Brazilian inflation using past information of the inflation, the industrial production and the unemployment rate. The recursive model will be a VAR(3) and the direct model will be a simple regression with three lags of each variable.</p>
<pre>#library devtools
#install_github(gabrielrvsc/HDeconometrics)
library(HDeconometrics)
library(reshape2)
library(ggplot2)

# = Load data = #
data(&quot;BRinf&quot;)
data = BRinf[ , c(1, 12, 14)]
colnames(data) = c(&quot;INF&quot;, &quot;IP&quot;, &quot;U&quot;)
</pre>
<h2>Recursive Forecast</h2>
<p>First we are going to estimate the recursive forecasts for 1 to 24 steps (months) ahead. The VAR will forecast all variables but we are only interested in the inflation. The plot below shows that the forecast converges very fast to the yellow line, which is the unconditional mean of the inflation in the training set. Recursive forecasts using AR or VAR on stationary and stable data will always converge to the unconditional mean unless we include more features in the model such as exogenous variables.</p>
<pre># = 24 out-of-sample (test) observations = #
train = data[1:132, ]
test = data[-c(1:132), ]

# = Estimate model and compute forecasts = #
VAR = HDvar(train, p = 3)
recursive = predict(VAR, 24)

df = data.frame(date = as.Date(rownames(data)),
    INF = data[,&quot;INF&quot;],
    fitted=c(rep(NA, 3), fitted(VAR)[ ,1], rep(NA, 24)),
    forecast = c(rep(NA,132), recursive[, 1]))

# = Plot = #
dfm = melt(df,id.vars = &quot;date&quot;)
ggplot(data = dfm) + geom_line(aes(x = date, y = value, color = variable))+
  geom_hline(yintercept = mean(train[ ,1]), linetype = 2,color = &quot;yellow&quot;)
</pre>
<p><img alt="plot of chunk unnamed-chunk-83" src="https://i2.wp.com/i.imgur.com/NwLHhwG.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Direct Forecast</h2>
<p>In direct forecasts we will need to estimate 24 models for the inflation to obtain the 24 forecasts. We must arrange the data in the right way for the model to estimate the regression on the right lags. This is where most people get confused. Let’s look at an univariate example using the function <code>embed</code> to arrange the data (click <a rel="nofollow" href="https://insightr.wordpress.com/2017/11/09/formal-ways-to-compare-forecasting-models-rolling-windows/" target="_blank">here</a> for more details on the function). I created a variable <img alt="y" title="y" src="https://s0.wp.com/latex.php?latex=y&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> the is just the sequence from 1 to 10. The embed function was used to generate a matrix with <img alt="y_t" title="y_t" src="https://s0.wp.com/latex.php?latex=y_t&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> and its first three lags (we lost three observations because of the lags). The model for <img alt="h=1" title="h=1" src="https://s0.wp.com/latex.php?latex=h%3D1&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> is a regression of the column <img alt="y_t" title="y_t" src="https://s0.wp.com/latex.php?latex=y_t&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> on the remaining columns.</p>
<pre>y=1:10
lags=embed(y,4)
colnames(lags)=c(&quot;yt&quot;,&quot;yt-1&quot;,&quot;yt-2&quot;,&quot;yt-3&quot;)
lags
</pre>
<pre>##      yt yt-1 yt-2 yt-3
## [1,]  4    3    2    1
## [2,]  5    4    3    2
## [3,]  6    5    4    3
## [4,]  7    6    5    4
## [5,]  8    7    6    5
## [6,]  9    8    7    6
## [7,] 10    9    8    7
</pre>
<p>If we want to run a model for two steps ahead we must remove the fist observation in the <img alt="y_t" title="y_t" src="https://s0.wp.com/latex.php?latex=y_t&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> column and the last observation in the lagged columns:</p>
<pre>lags = cbind(lags[-1, 1], lags[-nrow(lags), -1])
colnames(lags) = c(&quot;yt&quot;, &quot;yt-2&quot;, &quot;yt-3&quot;, &quot;yt-4&quot;)
lags
</pre>
<pre>##      yt yt-2 yt-3 yt-4
## [1,]  5    3    2    1
## [2,]  6    4    3    2
## [3,]  7    5    4    3
## [4,]  8    6    5    4
## [5,]  9    7    6    5
## [6,] 10    8    7    6
</pre>
<p>Now we just run a regression of the <img alt="y_t" title="y_t" src="https://s0.wp.com/latex.php?latex=y_t&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> columns on the other columns. For <img alt="h=3" title="h=3" src="https://s0.wp.com/latex.php?latex=h%3D3&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> we must do the same procedure again and for a general <img alt="h" title="h" src="https://s0.wp.com/latex.php?latex=h&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> we must remove the first <img alt="h-1" title="h-1" src="https://s0.wp.com/latex.php?latex=h-1&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> observations of the first column and the last <img alt="h-1" title="h-1" src="https://s0.wp.com/latex.php?latex=h-1&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> observations of the remaining columns.</p>
<p>The embed function also works for matrices with multiple columns. I will use it on the data to make it ready for the model. The code below runs the direct forecast for the forecasting horizons 1 to 24. The plot does not have a fitted line because there is one fitted model for each horizon. You can see that the direct forecasting is considerably different from the recursive case. It does not converge to the unconditional mean.</p>
<pre># = Create matrix with lags = #
X = embed(data, 4)
train = X[1:129, ]
test = X[-c(1:129), ]

ytrain = train[ ,1]

# = The Xtest is the same for all horizons = #
# = The last three observations (lags) of the train for each variable = #
Xtest = train[nrow(train), 1:9]

# = Remove the first three columns of the train = #
# = They are the three variables in t = #
Xtrain = train[ ,-c(1:3)]
ytest = test[, 1]

# = Run 24 models and forecasts = #
direct = c()
for(i in 1:24){
  model = lm(ytrain ~ Xtrain) # = Run regression = #
  direct[i] = c(1, Xtest) %*% coef(model) # = Calculate Forecast = #
  ytrain = ytrain[-1] # = Remove first observation of yt = #
  Xtrain = Xtrain[-nrow(Xtrain), ] # = Remove last observation of the other variables = #
}

# = plot = #
df = data.frame(data=as.Date(rownames(data)),
                INF = data[ ,&quot;INF&quot;],
                forecast = c(rep(NA, 132), direct))
dfm = melt(df,id.vars = &quot;data&quot;)
ggplot(data = dfm) +
  geom_line(aes(x = data, y = value, color = variable)) +
  geom_hline(yintercept = mean(train[ ,1]), linetype=2, color=&quot;yellow&quot;)
</pre>
<p><img alt="plot of chunk unnamed-chunk-86" src="https://i2.wp.com/i.imgur.com/5m7ew2M.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>As mentioned before, the one step ahead forecast is the same in both cases:</p>
<pre>print(direct[1])
</pre>
<pre>## [1] 0.7843985
</pre>
<pre>print(recursive[1])
</pre>
<pre>## [1] 0.7843985
</pre>
<h2>Conclusion</h2>
<ul>
<li>Recursive forecast:
<ul>
<li>Single model for all horizons,</li>
<li>must iterate the forecast using the coefficients for horizons different from one,</li>
<li>Forecasts converge to the unconditional mean for long horizons.</li>
</ul>
</li>
<li>Direct forecast:
<ul>
<li>One model for each horizon,</li>
<li>No need for iteration,</li>
<li>Forecasts does not converge to the unconditional mean,</li>
<li>Must be careful to arrange the data.</li>
</ul>
</li>
</ul>
<p> </p>
<p> <a rel="nofollow" href="https://feeds.wordpress.com/1.0/gocomments/insightr.wordpress.com/794/" target="_blank"><img border="0" alt="" src="https://feeds.wordpress.com/1.0/comments/insightr.wordpress.com/794/"></a> <img border="0" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" alt="" width="1" data-recalc-dims="1" height="1"></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://insightr.wordpress.com/2018/01/10/direct-forecast-x-recursive-forecast/"> R – insightR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/XdwBB1uZRQY" height="1"></a></div>