<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-time-series-analysis-with-tibbletime/"></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/26/demo_week_tibbletime.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 fourth day of <strong>Business Science Demo Week</strong>. We have a really cool one in store today: <code>tibbletime</code>, which uses a new <code>tbl_time</code> class that is time-aware!! 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>. Let’s take <code>tibbletime</code> for a spin!</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>
</ul>
<h1>tibbletime: What’s It Used For?</h1>
<ol>
<li>
<p><strong>The future of “tidy” time series analysis</strong>: New class <code>tbl_time</code> rests on top of <code>tbl</code> and makes tibbles time aware.</p>
</li>
<li>
<p><strong>Time Series Functions</strong>: Can use a series of “tidy” time series functions designed specifically for <code>tbl_time</code> objects. Some of them are:</p>
<ul>
<li>
<p><a rel="nofollow" href="https://business-science.github.io/tibbletime/reference/time_filter.html" target="_blank"><code>time_filter()</code></a>: Succinctly filter a tbl_time object by date.</p>
</li>
<li>
<p><a rel="nofollow" href="https://business-science.github.io/tibbletime/reference/time_summarise.html" target="_blank"><code>time_summarise()</code></a>: Similar to dplyr::summarise but with the added benefit of being able to summarise by a time period such as “yearly” or “monthly”.</p>
</li>
<li>
<p><a rel="nofollow" href="https://business-science.github.io/tibbletime/reference/tmap.html" target="_blank"><code>tmap()</code></a>: The family of tmap functions transform a tbl_time input by applying a function to each column at a specified time interval.</p>
</li>
<li>
<p><a rel="nofollow" href="https://business-science.github.io/tibbletime/reference/as_period.html" target="_blank"><code>as_period()</code></a>: Convert a tbl_time object from daily to monthly, from minute data to hourly, and more. This allows the user to easily aggregate data to a less granular level.</p>
</li>
<li>
<p><a rel="nofollow" href="https://business-science.github.io/tibbletime/reference/time_collapse.html" target="_blank"><code>time_collapse()</code></a>: When time_collapse is used, the index of a tbl_time object is altered so that all dates that fall in a period share a common date.</p>
</li>
<li>
<p><a rel="nofollow" href="https://business-science.github.io/tibbletime/reference/rollify.html" target="_blank"><code>rollify()</code></a>: Modify a function so that it calculates a value (or a set of values) at specific time intervals. This can be used for rolling averages and other rolling calculations inside the <code>tidyverse</code> framework.</p>
</li>
<li>
<p><a rel="nofollow" href="https://business-science.github.io/tibbletime/reference/create_series.html" target="_blank"><code>create_series()</code></a>: Use shorthand notation to quickly initialize a <code>tbl_time</code> object containing a <code>date</code> column with a regularly spaced time series.</p>
</li>
</ul>
</li>
</ol>
<p><img alt="Demo Week: tibbletime" src="https://i0.wp.com/www.business-science.io/assets/demo-week-tibbletime.png?w=456" data-recalc-dims="1"></p>
<h1>Load Libraries</h1>
<p>The <code>tibbletime</code> package is under active development, and because of this we recommend downloading the package from GitHub using <code>devtools</code>. You’ll get the latest functionality with all of the features demo-ed in this article.</p>
<figure>
<pre><code data-lang="r"><span># Get tibbletime version with latest features</span><span>
</span><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;business-science/tibbletime&quot;</span><span>)</span></code></pre>
</figure>
<p>Once installed, load the following libraries:</p>
<ul>
<li><code>tibbletime</code>: Enables creation of time-aware tibbles. Can use new <code>tbl_time</code> functions.</li>
<li><code>tidyquant</code>: Loads <code>tidyverse</code>, and is used to get data with <code>tq_get()</code>.</li>
</ul>
<figure>
<pre><code data-lang="r"><span># Load libraries</span><span>
</span><span>library</span><span>(</span><span>tibbletime</span><span>)</span><span> </span><span># Future of tidy time series analysis</span><span>
</span><span>library</span><span>(</span><span>tidyquant</span><span>)</span><span> </span><span># Loads tidyverse, tq_get()</span></code></pre>
</figure>
<h1>Data</h1>
<p>We’ll download the daily stock prices for the FANG stocks (FB, AMZN, NFLX, GOOG) using <code>tq_get()</code>.</p>
<figure>
<pre><code data-lang="r"><span># Stock Prices from Yahoo! Finance</span><span>
</span><span>FANG_symbols</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;FB&quot;</span><span>,</span><span> </span><span>&quot;AMZN&quot;</span><span>,</span><span> </span><span>&quot;NFLX&quot;</span><span>,</span><span> </span><span>&quot;GOOG&quot;</span><span>)</span><span>
</span><span>FANG_tbl_d</span><span> </span><span>&lt;-</span><span> </span><span>FANG_symbols</span><span> </span><span>%>%</span><span>
</span><span>tq_get</span><span>(</span><span>get</span><span> </span><span>=</span><span> </span><span>&quot;stock.prices&quot;</span><span>,</span><span> </span><span>from</span><span> </span><span>=</span><span> </span><span>&quot;2014-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>FANG_tbl_d</span><span> </span><span>&lt;-</span><span> </span><span>FANG_tbl_d</span><span> </span><span>%>%</span><span>
</span><span>group_by</span><span>(</span><span>symbol</span><span>)</span><span>
</span><span>FANG_tbl_d</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 3,024 x 8
## # Groups:   symbol [4]
##    symbol       date  open  high   low close   volume adjusted
##     </code></pre></figure>