<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/forecasting-gdp-with-r-and-dataseries-org/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://usefulr.wordpress.com/2017/05/03/forecasting-gdp-with-r-and-dataseries-org/"> R – usefulr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The website <a target="_blank" rel="nofollow" href="http://www.dataseries.org/">dataseries.org</a> aims to be Switzerland’s <a target="_blank" rel="nofollow" href="https://fred.stlouisfed.org/">FRED</a> – a free comprehensive database of Swiss time series. Powered by R and written in <a target="_blank" rel="nofollow" href="https://shiny.rstudio.com/">Shiny</a> (also using a bit of JavaScript) it allows you to quickly search and explore a large number of data series.</p>
<div></div>
<div>
<h3>Switzerland’s time series in one place</h3>
<p>Similarly to the United States, public data in Switzerland is produced by a large number of different offices, which makes it hard to find any particular series. <a target="_blank" rel="nofollow" href="http://www.dataseries.org/">dataseries.org</a> provides a structured and automatically updated collection of most of these series. We are still working on the data input, but are pretty much complete in the field of Economics.</p>
<p>You can download the data as spreadsheets or graphs, or embed interactive widgets in your website. Alternatively, you can import the data directly into R, using the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=dataseries">dataseries</a> package. Install the package from CRAN,</p>
<pre><code>install.packages(&quot;dataseries&quot;)</code></pre>
<p>and run the <code>ds</code> function with the <code>id</code> argument that you find on the <a target="_blank" rel="nofollow" href="http://www.dataseries.org/?main=GDP&split=PBRTT&select=A,R&var=&freq=quarterly&start=&end=&trans=none">website</a>:</p>
<pre><code>plot(dataseries::ds(&quot;GDP.PBRTT.A.R&quot;, &quot;ts&quot;), 
     ylab = &quot;mio CHF, at 2010 prices, s. adj.&quot;, 
     main = &quot;Gross Domestic Product&quot;)
</code></pre>
<p><img sizes="(max-width: 625px) 100vw, 625px" data-image-description="" data-orig-file="https://usefulr.files.wordpress.com/2017/05/fig-1.png?w=456" data-orig-size="1344,960" data-image-title="fig-1" class="wp-image-228" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="228" data-permalink="https://usefulr.wordpress.com/2017/05/03/forecasting-gdp-with-r-and-dataseries-org/fig-1/" alt="fig-1" data-medium-file="https://usefulr.files.wordpress.com/2017/05/fig-1.png?w=456?w=300" data-large-file="https://usefulr.files.wordpress.com/2017/05/fig-1.png?w=456?w=625" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://usefulr.files.wordpress.com/2017/05/fig-1.png?w=456"></p>
<p>This will give you an R plot of Switzerland’s GDP. (The data is cached, so calling the function again will not re-download until you restart the R session.)</p>
</div>
<div>
<h3>Live Import of Series to R</h3>
<p>In the following, I will use data from <a target="_blank" rel="nofollow" href="http://www.dataseries.org/">dataseries.org</a> to produce a live forecast of <a target="_blank" rel="nofollow" href="http://www.dataseries.org/?main=GDP&split=PBRTT&select=A,R&var=&freq=quarterly&start=&end=&trans=none">Switzerland’s GDP</a>. Each day the model is run, it will be ensured that the latest data is used. That way it is possible to produce a transparent and up-to-date forecast. For the following exercise, I will only use tools from R base, but it is of course possible to use the same data in a more advanced modeling framework.</p>
<p>In order to produce a reasonable forecast, we want to track early information on the business cycle, which is mostly survey data. We will use a question from the <a target="_blank" rel="nofollow" href="http://www.dataseries.org/?main=CCI&split=GEPC&select=A,R&var=&freq=quarterly&start=&end=&trans=none">SECO Consumer Confidence Survey on current economic performance</a>, the <a target="_blank" rel="nofollow" href="http://www.dataseries.org/?main=PMI&split=PM&select=SA&var=&freq=monthly&start=&end=&trans=none">Credit Suisse / Procure Purchasing Managers’ Index</a> and the <a target="_blank" rel="nofollow" href="http://www.dataseries.org/?main=KOF&split=KFBR&select=SA&var=&freq=monthly&start=&end=&trans=none">ETHZ KOF Barometer</a>.</p>
</div>
<div>
<h3>Transforming the data</h3>
<p>Getting these indicators from <a target="_blank" rel="nofollow" href="http://www.dataseries.org/">dataseries.org</a> directly into R is easy. Because these data are measured at different frequencies, we need to convert them to the same quarterly frequency as GDP. There are many packages that offer functions for that (e.g., the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=tempdisagg">tempdisagg</a> package has functions to move both to higher or lower frequencies), but I will stick to basic R here:</p>
<pre># Aggregating months to quarters
to_quarterly &lt;- function(x){
  sq &lt;- seq(floor(min(time(x))), ceiling(max(time(x))), by = 0.25)
  new.time &lt;- sq[findInterval(time(x), sq)]
  ts(tapply(x, new.time, FUN = mean), start = new.time[1], 
     frequency = 4)
}

pmi &lt;- to_quarterly(dataseries::ds(&quot;PMI.SA.PM&quot;, &quot;ts&quot;))
kof &lt;- to_quarterly(dataseries::ds(&quot;KOF.KFBR&quot;, &quot;ts&quot;))
csent &lt;- dataseries::ds(&quot;CCI.GEPC&quot;, &quot;ts&quot;)</pre>
<p>A plot of these series shows the common trend in these variables and gives you an indication of the business cycle, which may have turned upward in recent months.</p>
<pre><code>plot(cbind(pmi, kof, csent), main = &quot;Business Cycle Indicators&quot;)</code></pre>
<p><img sizes="(max-width: 625px) 100vw, 625px" data-image-description="" data-orig-file="https://usefulr.files.wordpress.com/2017/05/fig-2.png?w=456" data-orig-size="1344,960" data-image-title="fig-2" data-comments-opened="1" class="wp-image-229" data-recalc-dims="1" data-attachment-id="229" data-permalink="https://usefulr.wordpress.com/2017/05/03/forecasting-gdp-with-r-and-dataseries-org/fig-2/" alt="fig-2" data-medium-file="https://usefulr.files.wordpress.com/2017/05/fig-2.png?w=456?w=300" data-large-file="https://usefulr.files.wordpress.com/2017/05/fig-2.png?w=456?w=625" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://usefulr.files.wordpress.com/2017/05/fig-2.png?w=456"></p>
<p>Since these series are stationary, our left hand side variable should be stationary as well. This is accomplished by calculating percentage change rates of GDP:</p>
<pre>gdp.level &lt;- dataseries::ds(&quot;GDP.PBRTT.A.R&quot;, &quot;ts&quot;)
gdp &lt;- (gdp.level / lag(gdp.level, -1)) - 1</pre>
</div>
<div>
<h3>ARIMA modelling</h3>
<p>R’s workhorse for time series modeling is the <code>arima</code> function, which allows you to construct a univariate or multivariate model of GDP growth. Since the data is seasonally adjusted, a simple autoregressive process (AR1) offers a good benchmark:</p>
<pre><code># AR1
m0 &lt;- arima(gdp, order = c(1, 0, 0))
fct0 &lt;- predict(m0, n.ahead = 1)$pred
# GDP Growth Q1: +0.3 </code></pre>
<p>If you need advice on which ARIMA model to choose, the information criterions, accessed by the R functions <code>AIC</code> or <code>BIC</code>, can help you to choose a model. Simply take the model with lowest information criterion. The <code>auto.arima</code> function from the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=forecast">forecast</a> package also allows you to do the selection automatically.</p>
<p>We can include our series individually or jointly and estimate a range of different models. A good model (in terms of the AIC information criterion) is the following, which uses PMI and KOF data (but not consumer sentiment data):</p>
<pre># PMI, KOF
dta &lt;- window(cbind(pmi, kof), start = start(pmi), end = end(pmi))
m1 &lt;- arima(window(gdp, start = start(dta)), 
            xreg = window(dta, end = end(gdp)))
fct1 &lt;- predict(m1, n.ahead = 1, 
                newxreg = window(dta, start = tsp(gdp)[2] + 0.25,
                                 end = tsp(gdp)[2] + 0.25)
                )$pred
# GDP Growth Q1: +0.7</pre>
<p>The model’s forecast for the first quarter of 2017 is 0.7 – a value that hasn’t been reached for more than two years.</p>
</div>
<div>
<h3>A factor model</h3>
<p>If you have multiple indicators at hand, a common problem is multicollinearity, the fact that indicators are correlated, and therefore too many indicators deteriorate the quality of the model estimation.</p>
<p>An easy fix is to use a factor model, where the indicators are summarized in a few factors, which can be calculated by principal components (see <a target="_blank" rel="nofollow" href="https://www.princeton.edu/~mwatson/papers/Stock_Watson_JASA_2002.pdf">Stock and Watson 2002</a>):</p>
<pre># PMI, KOF, Consumer Sentiment, first Principal Component
pca &lt;- prcomp(window(cbind(pmi, kof, csent), start = start(pmi), 
                     end = tsp(gdp)[2] + 0.25),
             scale. = TRUE)
dta.pca &lt;- ts(pca$x[, 'PC1'], start = start(pmi), frequency = 4)

m2 &lt;- arima(window(gdp, start = start(dta)), 
            xreg = window(dta.pca, end = end(gdp)))
fct2 &lt;- predict(m2, n.ahead = 1, 
                newxreg = window(dta.pca, start = tsp(gdp)[2] + 0.25)
                )$pred
# GDP Growth Q1: +0.7</pre>
<p>Again, we get a forecast value of 0.7. Overall, survey data indicates that the economy is well on track. Let’s do a graphical comparison of our forecasts:</p>
<pre># skeletons to include forecasts 
gdp.fct0 &lt;- window(gdp, extend = TRUE, end = tsp(gdp)[2] + 0.25)
gdp.fct1 &lt;- gdp.fct2 &lt;- gdp.fct0

# plug forecasts into skeletons
window(gdp.fct0, start = end(gdp.fct0)) &lt;- fct0
window(gdp.fct1, start = end(gdp.fct1)) &lt;- fct1
window(gdp.fct2, start = end(gdp.fct2)) &lt;- fct2

ts.plot(window(cbind(gdp, gdp.fct0, gdp.fct1, gdp.fct2), 
               start = 2010), 
        col = 1:4, ylab = &quot;quarterly growth rates, s. adj.&quot;, 
        main = &quot;GDP Forecasts&quot;)
legend(&quot;topright&quot;, legend = c(&quot;GDP Growth Rate&quot;, &quot;AR 1 Forecast&quot;, 
                              &quot;PMI, KOF&quot;, &quot;Principal Component&quot;), 
       lty = 1, col = 1:4, bty = &quot;n&quot;)</pre>
<p><img sizes="(max-width: 625px) 100vw, 625px" data-image-description="" data-orig-file="https://usefulr.files.wordpress.com/2017/05/fig-3.png?w=456" data-orig-size="1344,960" data-image-title="fig-3" data-comments-opened="1" class="wp-image-230" data-recalc-dims="1" data-attachment-id="230" data-permalink="https://usefulr.wordpress.com/2017/05/03/forecasting-gdp-with-r-and-dataseries-org/fig-3/" alt="fig-3" data-medium-file="https://usefulr.files.wordpress.com/2017/05/fig-3.png?w=456?w=300" data-large-file="https://usefulr.files.wordpress.com/2017/05/fig-3.png?w=456?w=625" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://usefulr.files.wordpress.com/2017/05/fig-3.png?w=456"></p>
<p>Publication of first quarter GDP is on <a target="_blank" rel="nofollow" href="https://www.seco.admin.ch/seco/en/home/wirtschaftslage---wirtschaftspolitik/Wirtschaftslage/bip-quartalsschaetzungen-.html">June 1, 2017</a>. See you in a month!</p>
</div>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/usefulr.wordpress.com/215/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/usefulr.wordpress.com/215/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" 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://usefulr.wordpress.com/2017/05/03/forecasting-gdp-with-r-and-dataseries-org/"> R – usefulr</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/unhBN3AWVKA"></a></div>