<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/sweep-extending-broom-for-time-series-forecasting/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.business-science.io/code-tools/2017/07/09/sweep-0-1-0.html"> business-science.io - Articles</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>We’re pleased to introduce a new package, <code>sweep</code>, now on CRAN! Think of it like <code>broom</code> for the <code>forecast</code> package. The <code>forecast</code> package is the <strong>most popular package for forecasting</strong>, and for good reason: it has a number of <em>sophisticated forecast modeling functions</em>. There’s one problem: <code>forecast</code> is based on the <code>ts</code> system, which makes it difficult work within the <code>tidyverse</code>. This is where <code>sweep</code> fits in! <strong>The <code>sweep</code> package has tidiers that convert the output from <code>forecast</code> modeling and forecasting functions to “tidy” data frames.</strong> We’ll go through a quick introduction to show how the tidiers can be used, and then show a fun example of forecasting GDP trends of US states. If you’re familiar with <code>broom</code> it will feel like second nature. If you like what you read, don’t forget to <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/07/09/sweep-0-1-0.html#social" target="_blank">follow us on social media</a> to stay up on the latest <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/07/09/sweep-0-1-0.html#contact" target="_blank">Business Science</a> news, events and information!</p>
<p>An example of the visualization we can create using <code>sw_sweep()</code> for tidying a forecast:</p>
<p><img alt="Forecasting State GDP Trends" src="https://i1.wp.com/www.business-science.io/assets/state_forecast.png?w=456" data-recalc-dims="1"></p>
<h1>Benefits</h1>
<p>The <code>sweep</code> package makes it easy to transition from the <code>forecast</code> package to the <code>tidyverse</code>. The main benefits are:</p>
<ol>
<li>
<p><strong>Converting forecasts to data frames</strong>: The <code>forecast</code> package uses <code>ts</code> objects under the hood, thus making it difficult to use in the “tidyverse”. With <code>sw_sweep</code>, we can now easily convert forecasts to tidy data frames.</p>
</li>
<li>
<p><strong>Dates are carried through to the end</strong>: The <code>ts</code> objects traditionally lose date information. The <code>sweep</code> package uses <code>timekit</code> under the hood to maintain the original time series index the whole way through the process. The result is ability to <strong>forecast in the original date or date-time time-base</strong> by setting <code>timekit_idx = TRUE</code>. Future dates are computed using <code>tk_make_future_timeseries()</code> from <code>timekit</code>.</p>
</li>
<li>
<p><strong>Intermediate modeling tidiers</strong>: The <code>sweep</code> package uses <code>broom</code>-style tidiers, <code>sw_tidy</code>, <code>sw_glance</code>, and <code>sw_augment</code> to extract important model information into tidy data frames.</p>
</li>
</ol>
<h1>Libraries Needed</h1>
<p>You can quickly install the packages used with the following script:</p>
<figure>
<pre><code data-lang="r"><span># Install packages
</span><span>pkgs</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;forecast&quot;</span><span>,</span><span> </span><span>&quot;sweep&quot;</span><span>,</span><span> </span><span>&quot;timekit&quot;</span><span>,</span><span> </span><span>&quot;tidyquant&quot;</span><span>,</span><span> </span><span>&quot;geofacet&quot;</span><span>)</span><span>
</span><span>install.packages</span><span>(</span><span>pkgs</span><span>)</span></code></pre>
</figure>
<p>Load the following packages:</p>
<ul>
<li><code>forecast</code>: Has excellent modeling functions such as <code>auto.arima()</code>, <code>ets()</code> and <code>bats()</code> and the <code>forecast()</code> function for predicting future observations.</li>
<li><code>sweep</code>: Tidies the output of <code>forecast</code> functions using a similar strategy as the <code>broom</code> package.</li>
<li><code>timekit</code>: Coercion function <code>tk_ts()</code> for converting a tibble to <code>ts</code> while maintaining time-based data.</li>
<li><code>tidyquant</code>: Used to get FRED data and for its <code>ggplot2</code> theme.</li>
<li><code>geofacet</code>: Really useful <code>facet_geo()</code> function to visualize facets organized by geography.</li>
</ul>
<figure>
<pre><code data-lang="r"><span># Load packages
</span><span>library</span><span>(</span><span>forecast</span><span>)</span><span> </span><span># Most popular forecasting pkg
</span><span>library</span><span>(</span><span>sweep</span><span>)</span><span> </span><span># Broom tidiers for forecast pkg
</span><span>library</span><span>(</span><span>timekit</span><span>)</span><span> </span><span># Working with time series in R
</span><span>library</span><span>(</span><span>tidyquant</span><span>)</span><span> </span><span># Get's data from FRED, loads tidyverse behind the scenes
</span><span>library</span><span>(</span><span>geofacet</span><span>)</span><span> </span><span>#</span><span> </span><span>facet_geo</span><span>()</span><span> </span><span>for</span><span> </span><span>visualizing</span><span> </span><span>facets</span><span> </span><span>organized</span><span> </span><span>as</span><span> </span><span>states</span></code></pre>
</figure>
<h1>Data</h1>
<p>We’ll be working with <strong>Annual Gross Domestic Product (GDP) time series data for each of the US States</strong> from the <a rel="nofollow" href="https://fred.stlouisfed.org/" target="_blank">FRED database</a>.</p>
<h4>One State: Nebraska</h4>
<p>We can get the data for one of the states by using <code>tq_get()</code> from the <code>tidyquant</code> package. The FRED code we will use is “NENGSP”, for Nebraska’s annual GDP. Set <code>get = &quot;economic.data&quot;</code> and supply a date range. By default, the returned values are named “price”. Rename “gdp”.</p>
<figure>
<pre><code data-lang="r"><span># Get Annual GDP time series, Nebraska
# https://fred.stlouisfed.org/series/NENGSP
</span><span>ne_gdp</span><span> </span><span>&lt;-</span><span> </span><span>tq_get</span><span>(</span><span>&quot;NENGSP&quot;</span><span>,</span><span> </span><span>get</span><span> </span><span>=</span><span> </span><span>&quot;economic.data&quot;</span><span>,</span><span> </span><span>from</span><span> </span><span>=</span><span> </span><span>&quot;2007-01-01&quot;</span><span>,</span><span> </span><span>to</span><span> </span><span>=</span><span> </span><span>&quot;2017-06-01&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>gdp</span><span> </span><span>=</span><span> </span><span>price</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>ne_gdp</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 10 x 2
##          date    gdp
##        </code></pre></figure>