<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/formal-ways-to-compare-forecasting-models-rolling-windows/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://insightr.wordpress.com/2017/11/09/formal-ways-to-compare-forecasting-models-rolling-windows/"> R – insightR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<div><strong>By Gabriel Vasconcelos</strong></div>
</div>
<h2>Overview</h2>
<p>When working with time-series forecasting we often have to choose between a few potential models and the best way is to test each model in pseudo-out-of-sample estimations. In other words, we simulate a forecasting situation where we drop some data from the estimation sample to see how each model perform.</p>
<p><span></span></p>
<p>Naturally, if you do only one (or just a few) forecasting test you results will have no robustness and in the next forecast the results may change drastically. Another possibility is to estimate the model in, let’s say, half of the sample, and use the estimated model to forecast the other half. This is better than a single forecast but it does not account for possible changes in the structure of the data over the time because you have only one estimation of the model. The most accurate way to compare models is using rolling windows. Suppose you have, for example, 200 observations of a time-series. First you estimate the model with the first 100 observations to forecast the observation 101. Then you include the observation 101 in the estimation sample and estimate the model again to forecast the observation 102. The process is repeated until you have a forecast for all 100 out-of-sample observations. This procedure is also called expanding window. If you drop the first observation in each iteration to keep the window size always the same then you have a fixed rolling window estimation. In the end you will have 100 forecasts for each model and you can calculate RMSE, MAE and formal tests such as Diebold &amp; Mariano.</p>
<p>In general, the fixed rolling window is better than the expanding window because of the following example. Suppose we have two models:</p>
<p><img alt="displaystyle y_t = b_1x_{1,t-1}+varepsilon_t" title="displaystyle y_t = b_1x_{1,t-1}+varepsilon_t" src="https://s0.wp.com/latex.php?latex=%5Cdisplaystyle+y_t+%3D+b_1x_%7B1%2Ct-1%7D%2B%5Cvarepsilon_t&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"><br>
<img alt="y_t = b_1x_{1,t-1}+b_2x_{2,t-1}+varepsilon_t" title="y_t = b_1x_{1,t-1}+b_2x_{2,t-1}+varepsilon_t" src="https://s0.wp.com/latex.php?latex=y_t+%3D+b_1x_%7B1%2Ct-1%7D%2Bb_2x_%7B2%2Ct-1%7D%2B%5Cvarepsilon_t&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"></p>
<p>Let’s assume that the true value of <img alt="b_2" title="b_2" src="https://s0.wp.com/latex.php?latex=b_2&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> is zero. If we use expanding windows the asymptotic theory tells us that <img alt="hat{b}_2" title="hat{b}_2" src="https://s0.wp.com/latex.php?latex=%5Chat%7Bb%7D_2&bg=ffffff&%23038;fg=333333&%23038;s=0" class="latex"> will go to zero and both models will be the same. If that is the case, we may be unable to distinguish which model is more accurate to forecast <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">. However, the first model is better than the second model in small samples and it is just as good in large samples. We should be able to identify this feature. Fixed rolling windows keep the sample size fixed and they are free from this problem conditional on the sample size. In this case, the Diebold &amp; Mariano test becomes the Giacomini &amp; White test.</p>
<h2>Application</h2>
<p>In this example we are going to use some inflation data from the AER package. First let’s have a look at the function embed. This function is very useful in this rolling window framework because we often include lags of variables in the models and the function embed creates all lags for us in a single line of code. Here is an example:</p>
<pre>library(AER)
library(xts)
library(foreach)
library(reshape2)
library(ggplot2)
library(forecast)

## = embed = ##
x1 = c(1, 2, 3, 4, 5)
x2 = c(11, 12, 13, 14, 15)
x = cbind(x1, x2)
(x_embed = embed(x, 2))
</pre>
<pre>##      [,1] [,2] [,3] [,4]
## [1,]    2   12    1   11
## [2,]    3   13    2   12
## [3,]    4   14    3   13
## [4,]    5   15    4   14
</pre>
<p>As you can see. The first two columns show the variables x1 and x2 at lag 0 and the second column shows the same variables with one lag. We lost one observation because of the lag operation.</p>
<p>To the real example!!! We are going to estimate a model to forecast the US inflation using four autorregressive variables (four lags of the inflation), four lags of the industrial production and dummy variables for months. The second model will be a simple random walk. I took the first log-difference on both variables (CPI and industrial production index). The code below loads and prepare the data with the embed function.</p>
<pre>## = Load Data = ##
data(&quot;USMacroSWM&quot;)
data = as.xts(USMacroSWM)[ , c(&quot;cpi&quot;, &quot;production&quot;), ]
data = cbind(diff(log(data[ ,&quot;cpi&quot;])), diff(log(data[ ,&quot;production&quot;])))[-1, ]

## = Prep data with embed = ##
lag = 4
X = embed(data, lag + 1)
X = as.data.frame(X)
colnames(X) = paste(rep(c(&quot;inf&quot;, &quot;prod&quot;), lag + 1),
                    sort(rep(paste(&quot;l&quot;, 0:lag, sep = &quot;&quot;),2)), sep = &quot;&quot; )
X$month = months(tail(index(data), nrow(X)))
head(X)
</pre>
<pre>##         infl0       prodl0        infl1       prodl1        infl2
## 1 0.005905082  0.000000000 -0.002275314  0.004085211  0.000000000
## 2 0.006770507 -0.005841138  0.005905082  0.000000000 -0.002275314
## 3 0.007618231  0.005841138  0.006770507 -0.005841138  0.005905082
## 4 0.019452426  0.007542827  0.007618231  0.005841138  0.006770507
## 5 0.003060112  0.009778623  0.019452426  0.007542827  0.007618231
## 6 0.006526018  0.013644328  0.003060112  0.009778623  0.019452426
##         prodl2        infl3       prodl3        infl4       prodl4
## 1 -0.008153802  0.017423641  0.005817352  0.006496543  0.005851392
## 2  0.004085211  0.000000000 -0.008153802  0.017423641  0.005817352
## 3  0.000000000 -0.002275314  0.004085211  0.000000000 -0.008153802
## 4 -0.005841138  0.005905082  0.000000000 -0.002275314  0.004085211
## 5  0.005841138  0.006770507 -0.005841138  0.005905082  0.000000000
## 6  0.007542827  0.007618231  0.005841138  0.006770507 -0.005841138
##       month
## 1      June
## 2      July
## 3    August
## 4 September
## 5   October
## 6  November
</pre>
<p>The following code estimates 391 fixed rolling windows with a sample size of 300 in each window:</p>
<pre># = Number of windows and window size
w_size = 300
n_windows = nrow(X) - 300

# = Rolling Window Loop = #
forecasts = foreach(i=1:n_windows, .combine = rbind) %do%{

  # = Select data for the window (in and out-of-sample) = #
  X_in = X[i:(w_size + i - 1), ] # = change to X[1:(w_size + i - 1), ] for expanding window
  X_out = X[w_size + i, ]

  # = Regression Model = #
  m1 = lm(infl0 ~ . - prodl0, data = X_in)
  f1 = predict(m1, X_out)

  # = Random Walk = #
  f2 = tail(X_in$infl0, 1)

  return(c(f1, f2))
}
</pre>
<p>Finally, the remaining code calculates the forecasting errors, forecasting RMSE across the rolling windows and the Giacomini &amp; White test. As you can see, the test rejected the null hypothesis of both models being equally accurate and the RMSE was smaller for the model with the lags, production and dummies.</p>
<pre># = Calculate and plot errors = #
e1 = tail(X[ ,&quot;infl0&quot;], nrow(forecasts)) - forecasts[ ,1]
e2 = tail(X[ ,&quot;infl0&quot;], nrow(forecasts)) - forecasts[ ,2]
df = data.frame(&quot;date&quot;=tail(as.Date(index(data)), n_windows), &quot;Regression&quot; = e1, &quot;RandomWalk&quot; = e2)
mdf = melt(df,id.vars = &quot;date&quot;)
ggplot(data = mdf) + geom_line(aes(x = date, y = value, linetype = variable, color = variable))
</pre>
<p><img alt="plot of chunk unnamed-chunk-4" src="https://i0.wp.com/i.imgur.com/64dN70T.png?w=456&ssl=1" data-recalc-dims="1"></p>
<pre># = RMSE = #
(rmse1 = 1000 * sqrt(mean(e1 ^ 2)))
</pre>
<pre>## [1] 2.400037
</pre>
<pre>(rmse2 = 1000 * sqrt(mean(e2 ^ 2)))
</pre>
<pre>## [1] 2.62445
</pre>
<pre># = DM test = #
(dm = dm.test(e1, e2))
</pre>
<pre>##
##  Diebold-Mariano Test
##
## data:  e1e2
## DM = -1.977, Forecast horizon = 1, Loss function power = 2,
## p-value = 0.04874
## alternative hypothesis: two.sided
</pre>
<h2>References</h2>
<p>Diebold, Francis X., and Robert S. Mariano. “Comparing predictive accuracy.” Journal of Business &amp; economic statistics 20.1 (2002): 134-144.</p>
<p>Giacomini, Raffaella, and Halbert White. “Tests of conditional predictive ability.” Econometrica 74.6 (2006): 1545-1578.</p>
<p> <a rel="nofollow" href="https://feeds.wordpress.com/1.0/gocomments/insightr.wordpress.com/761/" target="_blank"><img border="0" alt="" src="https://feeds.wordpress.com/1.0/comments/insightr.wordpress.com/761/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" data-recalc-dims="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/2017/11/09/formal-ways-to-compare-forecasting-models-rolling-windows/"> 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/5qgJd78sCFo" height="1"></a></div>