<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/timekit-time-series-forecast-applications-using-data-mining/"></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/05/02/timekit-0-2-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>The <code>timekit</code> package contains a collection of tools for working with time series in R. There’s a number of benefits. One of the biggest is the ability to use a time series signature to predict future values (forecast) through data mining techniques. While this post is geared toward exposing the user to the <code>timekit</code> package, there are examples showing the power of data mining a time series as well as how to work with time series in general. A number of <code>timekit</code> functions will be discussed and implemented in the post. The first group of functions works with the time series index, and these include functions <code>tk_index()</code>, <code>tk_get_timeseries_signature()</code>, <code>tk_augment_timeseries_signature()</code> and <code>tk_get_timeseries_summary()</code>. We’ll spend the bulk of this post introducing you to these. The next function deals with creating a future time series from an existing index, <code>tk_make_future_timeseries()</code>. The last set of functions deal with coercion to and from the major time series classes in R, <code>tk_tbl()</code>, <code>tk_xts()</code>, <code>tk_zoo()</code> (and <code>tk_zooreg()</code>), and <code>tk_ts()</code>.</p>
<h1>Benefits</h1>
<p>So, why another time series package? The short answer is because it helps with data mining, communication between time series objects, and facilitating accurate future time series. The long answer is slightly more complicated, and I will attempt to explain.</p>
<h5>Time Series Signature</h5>
<p>The first reason and arguably the most important reason is the idea that there is a large amount of information stored inside a simple yet complex <strong>time index</strong> that is very useful for modeling and data mining. The time index is the collection of time-based values that define <em>when</em> each observation occurred. Consider the timestamp “2016-01-01 00:00:00”. This contains a wealth of information related to the observation including year, month, day, hour, minute and second. We can even extract more information including half, quarter, week of year, day of year, and so on with little effort. Next is the concept of the <strong>frequency</strong> (or periodicity or scale), which is the amount of time between multiple observations. From this time difference we can get even more information such as the periodicity of the data, whether the observations are regular or irregularly spaced, and even which observations are frequently missing. By my count, there’s at least 20+ features that can be retrieved from a simple timestamp. The important concept is that these features can exploded (or broken out) into what I’m calling the <strong>time series signature</strong>, which is nothing more than a decomposition of the unique features related to time index values. This data is very useful as it can be summarized, modeled, mined, sliced and diced, etc. As and example of the power of the signature, we can generate a prediction using data mining techniques such as this (see alcohol sales example later).</p>
<p><img alt="Forecasting Alcohol Sales" src="https://i1.wp.com/www.business-science.io/assets/timekit-0-2-0.png?w=456" data-recalc-dims="1"></p>
<h5>Prediction and Forecast Accuracy</h5>
<p>The second reason is that often we want to make predictions into the future. There’s a number of packages such as <code>forecast</code> and <code>prophet</code> that already specialize in this. For <code>forecast</code> the future dates can be incorrect especially for daily data. A regular numeric system doesn’t contain true dates and a sequential system results in inaccuracy with respect to irregular dates. For prophet, the mechanism to compute holidays and missing days is internal to the <code>predict()</code> method, and therefore the a method specific to creating future dates is needed. Two types of days cause problems: those that are regularly skipped and irregularly skipped. The regularly skipped days (such as weekends or sometimes companies get to take every other Friday off) need to be factored into the future date sequence. The irregularly skipped days (think holidays) cause issues as well, and these suffer the additional problem as they can be difficult (but not impossible) to predict.</p>
<h5>Communication and Coercion Between Time-Based Object Classes</h5>
<p>The third reason is that the R object structures that contain time-based information are difficult use together. My first attempt was born in <code>tidyquant</code> where I created the <code>as_tibble()</code> and <code>as_xts()</code> functions to coerce (convert) back and forth. I was naive in this attempt because the problem is larger: we have <code>zoo</code>, <code>ts</code> and many other packages that work with time-based information. The <code>xts</code> and <code>zoo</code> packages solved part of the problem, but there’s two issues that persist. First, the time-based tibble (“tidy” data frame with class <code>tbl</code>) does not communicate well with the rest of the group. Coercing to <code>xts</code>, <code>zoo</code> and <code>ts</code> objects can result in a lot of issues especially when coercion rules for homogeneous object classes take over. Weird things can happen such as turning numeric data into character or converting date to numeric without warning. Further, each coercion method (<code>as_tibble</code>, <code>as.xts</code>, <code>as.zoo</code>, <code>as.ts</code>) has its own nuances that are inconsistent. Second, some classes like <code>ts</code> do not use a time-based index, but rather use a regularized numeric-based index. Without maintaining the time-based index, we can never go back to the original data structure, whether it is <code>tbl</code>, <code>xts</code>, <code>zoo</code>, etc.</p>
<h5>Enter timekit</h5>
<p>The <code>timekit</code> package solves each of these issues. It includes functions to create a time series signature and a time series summary from a sequence of dates. It includes methods to accurately generate future time series index values, which is especially important for daily data. It provides consistent coercion methods that prevent inadvertent class coercion issues resulting from homogeneous object structures and that maximize time-based index retention for regularized data structures.</p>
<h1>Test Driving timekit</h1>
<p>Let’s take <code>timekit</code> for a test drive. We’ll be using a few other packages in the process to help with the examples. First, install <code>timekit</code>.</p>
<figure>
<pre><code data-lang="r"><span>install.packages</span><span>(</span><span>&quot;timekit&quot;</span><span>)</span></code></pre>
</figure>
<p>Next, load these packages:</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyquant</span><span>)</span><span>
</span><span>library</span><span>(</span><span>timekit</span><span>)</span></code></pre>
</figure>
<h2>Example 1: Predicting Daily Volume for FB</h2>
<p>This example is intended to expose potential users to several functions in <code>timekit</code>. We’ll develop a prediction algorithm to predict daily volume using the <strong>time series signature</strong>. First, start with the <code>FANG</code> data set from the <code>tidyquant</code> package. Filter to get just the FB stock prices, and select the “date” and “volume” columns. This is a typical time series data structure. A time-based tibble with a “date” column and a features column (“volume” in this case).</p>
<figure>
<pre><code data-lang="r"><span>FB_tbl</span><span> </span><span>&lt;-</span><span> </span><span>FANG</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>symbol</span><span> </span><span>==</span><span> </span><span>&quot;FB&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>date</span><span>,</span><span> </span><span>volume</span><span>)</span><span>
</span><span>FB_tbl</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 1,008 × 2
##          date    volume
##        </code></pre></figure>