<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-time-series-machine-learning-with-timetk/"></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/24/demo_week_timetk.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 second day of <strong>Business Science Demo Week</strong>. What’s demo week? Every day this week we are demoing 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>. Second up is <code>timetk</code>, your toolkit for time series in R. Here we go!</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>
</ul>
<h1>timetk: What’s It Used For?</h1>
<p>There are three main uses:</p>
<ol>
<li>
<p><strong>Time series machine learning</strong>: Using regression algorithms to forecast</p>
</li>
<li>
<p><strong>Making future time series indicies</strong>: Extract, explore, and extend a time series index using patterns in the time-base</p>
</li>
<li>
<p><strong>Coercing (converting) between time classes</strong> (e.g. between <code>tbl</code>, <code>xts</code>, <code>zoo</code>, <code>ts</code>): Consistent coercion makes working in the various time classes much easier!</p>
</li>
</ol>
<p>We’ll go over time series ML and coercion today. The second (extracting and making future time series) will be touched on in time series ML as this is very critical to prediction accuracy.</p>
<p><img alt="Demo Week: timetk" src="https://i0.wp.com/www.business-science.io/assets/demo-week-timetk.png?w=456" data-recalc-dims="1"></p>
<h1>Load Libraries</h1>
<p>We’ll need two libraries today:</p>
<ul>
<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</li>
</ul>
<p>If you haven’t done so already, install the packages:</p>
<figure>
<pre><code data-lang="r"><span># Install packages</span><span>
</span><span>install.packages</span><span>(</span><span>&quot;timetk&quot;</span><span>)</span><span>
</span><span>install.packages</span><span>(</span><span>&quot;tidyquant&quot;</span><span>)</span></code></pre>
</figure>
<p>Load the libraries.</p>
<figure>
<pre><code data-lang="r"><span># Load libraries</span><span>
</span><span>library</span><span>(</span><span>timetk</span><span>)</span><span> </span><span># Toolkit for working with time series in R</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></code></pre>
</figure>
<h1>Data</h1>
<p>We’ll 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>