<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/demo-week-tidy-forecasting-with-sweep/"></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/10/25/demo_week_sweep.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 into the third day of <strong>Business Science Demo Week</strong>. Hopefully by now you’re getting a taste of some interesting and useful packages. For those that may have missed it, every day this week we are demo-ing an R package: <code>tidyquant</code> (Monday), <code>timetk</code> (Tuesday), <code>sweep</code> (Wednesday), <code>tibbletime</code> (Thursday) and <code>h2o</code> (Friday)! <strong>That’s five packages in five days</strong>! We’ll give you intel on <strong>what you need to know about these packages to go from zero to hero</strong>. Today is <code>sweep</code>, which has <code>broom</code>-style tidiers for forecasting. Let’s get going!</p>
<h4>Previous Demo Week Demos</h4>
<ul>
<li><a rel="nofollow" href="http://www.business-science.io/code-tools/2017/10/23/demo_week_tidyquant.html" target="_blank">class(Monday) &lt;-tidyquant</a></li>
<li><a rel="nofollow" href="http://www.business-science.io/code-tools/2017/10/24/demo_week_timetk.html" target="_blank">class(Tuesday) &lt;-timetk</a></li>
</ul>
<h1>sweep: What’s It Used For?</h1>
<p><code>sweep</code> is used for tidying the <code>forecast</code> package workflow. Like <code>broom</code> is to the <code>stats</code> library, <code>sweep</code> is to <code>forecast</code> package. It has useful functions including: <code>sw_tidy</code>, <code>sw_glance</code>, <code>sw_augment</code>, and <code>sw_sweep</code>. We’ll check out each in this demo.</p>
<p>An added benefit to <code>sweep</code> and <code>timetk</code> is if the ts-objects are created from time-based tibbles (tibbles with date or datetime index), the date or datetime information is carried through the forecasting process as a timetk index attribute. <strong>Bottom Line: This means we can finally use dates when forecasting as opposed to the regularly spaced numeric dates that the ts-system uses!</strong></p>
<p><img alt="Demo Week: sweep" src="https://i0.wp.com/www.business-science.io/assets/demo-week-sweep.png?w=456" data-recalc-dims="1"></p>
<h1>Load Libraries</h1>
<p>We’ll need four libraries today:</p>
<ul>
<li><code>sweep</code>: For tidying the <code>forecast</code> package (like <code>broom</code> is to <code>stats</code>, <code>sweep</code> is to <code>forecast</code>)</li>
<li><code>forecast</code>: Package that includes ARIMA, ETS, and other popular forecasting algorithms</li>
<li><code>tidyquant</code>: For getting data and loading the tidyverse behind the scenes</li>
<li><code>timetk</code>: Toolkit for working with time series in R. We’ll use to coerce from <code>tbl</code> to <code>ts</code>.</li>
</ul>
<p>If you don’t already have installed, you can install with <code>install.packages()</code>. Then load the libraries as follows.</p>
<figure>
<pre><code data-lang="r"><span># Load libraries</span><span>
</span><span>library</span><span>(</span><span>sweep</span><span>)</span><span> </span><span># Broom-style tidiers for the forecast package</span><span>
</span><span>library</span><span>(</span><span>forecast</span><span>)</span><span> </span><span># Forecasting models and predictions package</span><span>
</span><span>library</span><span>(</span><span>tidyquant</span><span>)</span><span> </span><span># Loads tidyverse, financial pkgs, used to get data</span><span>
</span><span>library</span><span>(</span><span>timetk</span><span>)</span><span> </span><span># Functions working with time series</span></code></pre>
</figure>
<h1>Data</h1>
<p>We’ll use the same data as in the <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/10/23/demo_week_timetk.html" target="_blank">previous post</a> where we used <code>timetk</code> to forecast with time series machine learning. We get data using the <code>tq_get()</code> function from <code>tidyquant</code>. The data comes from FRED: <a rel="nofollow" href="https://fred.stlouisfed.org/series/S4248SM144NCEN" target="_blank">Beer, Wine, and Distilled Alcoholic Beverages Sales</a>.</p>
<figure>
<pre><code data-lang="r"><span># Beer, Wine, Distilled Alcoholic Beverages, in Millions USD</span><span>
</span><span>beer_sales_tbl</span><span> </span><span>&lt;-</span><span> </span><span>tq_get</span><span>(</span><span>&quot;S4248SM144NCEN&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;2010-01-01&quot;</span><span>,</span><span> </span><span>to</span><span> </span><span>=</span><span> </span><span>&quot;2016-12-31&quot;</span><span>)</span><span>
</span><span>beer_sales_tbl</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 84 x 2
##          date price
##        </code></pre></figure>