<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-h2o-and-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/28/demo_week_h2o.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 at the final day of <strong>Business Science Demo Week</strong>. Today we are demo-ing the <code>h2o</code> package for machine learning on time series data. 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>. Today you’ll see how we can use <code>timetk</code> + <code>h2o</code> to get really accurate time series forecasts. 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>
<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>
<li><a rel="nofollow" href="http://www.business-science.io/code-tools/2017/10/25/demo_week_sweep.html" target="_blank">class(Wednesday) &lt;-sweep</a></li>
<li><a rel="nofollow" href="http://www.business-science.io/code-tools/2017/10/26/demo_week_tibbletime.html" target="_blank">class(Thursday) &lt;-tibbletime</a></li>
</ul>
<h1>h2o: What’s It Used For?</h1>
<p>The <code>h2o</code> package is a product offered by <a rel="nofollow" href="https://www.h2o.ai/" target="_blank">H2O.ai</a> that contains a number of cutting edge machine learning algorithms, performance metrics, and auxiliary functions to make machine learning both powerful and easy. One of the main benefits of H2O is that it can be deployed on a cluster (this will not be discussed today). From the R perspective, there are four main uses:</p>
<ol>
<li>
<p><strong>Data Manipulation</strong>: Merging, grouping, pivoting, imputing, splitting into training/test/validation sets, etc.</p>
</li>
<li>
<p><strong>Machine Learning Algorithms</strong>: Very sophisiticated algorithms in both supervised and unsupervised categories. Supervised include deep learning (neural networks), random forest, generalized linear model, gradient boosting machine, naive bayes, stacked ensembles, and xgboost. Unsupervised include generalized low rank models, k-means and PCA. There’s also Word2vec for text analysis. The latest stable release also has <strong>AutoML: automatic machine learning</strong>, which is really cool as we’ll see in this post!</p>
</li>
<li>
<p><strong>Auxiliary ML Functionality</strong> Performance analysis and grid hyperparameter search</p>
</li>
<li>
<p><strong>Production, Map/Reduce and Cloud</strong>: Capabilities for productionizing into Java environments, cluster deployment with Hadoop / Spark (Sparkling Water), deploying in cloud environments (Azure, AWS, Databricks, etc)</p>
</li>
</ol>
<p>Sticking with the theme for the week, we’ll go over how <code>h2o</code> can be used for time series machine learning as an advanced algorithm. We’ll use <code>h2o</code> locally to develop a high accuracy time series model on the same data set (<code>beer_sales_tbl</code>) from the <code>timetk</code> and <code>sweep</code> posts. This is a supervised regression problem.</p>
<p><img alt="Demo Week: H2O" src="https://i2.wp.com/www.business-science.io/assets/demo-week-h2o.png?w=456" data-recalc-dims="1"></p>
<h1>Load Libraries</h1>
<p>We’ll need three libraries today:</p>
<ul>
<li><code>h2o</code>: Awesome machine learning library</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</li>
</ul>
<h4>IMPORTANT FOR INSTALLING H2O</h4>
<p>For <code>h2o</code>, you must install the <a rel="nofollow" href="https://www.h2o.ai/download/" target="_blank">latest stable release</a>. Select H2O » Latest Stable Release » Install in R. Then follow the instructions exactly.</p>
<h4>Installing Other Packages</h4>
<p>If you haven’t done so already, install the <code>timetk</code> and <code>tidyquant</code> 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>
<h4>Loading Libraries</h4>
<p>Load the libraries.</p>
<figure>
<pre><code data-lang="r"><span># Load libraries</span><span>
</span><span>library</span><span>(</span><span>h</span><span>2</span><span>o</span><span>)</span><span> </span><span># Awesome ML Library</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;2017-10-27&quot;</span><span>)</span><span>
</span><span>beer_sales_tbl</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 92 x 2
##          date price
##        </code></pre></figure>