<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/timekit-new-documentation-function-improvements-forecasting-vignette/"></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/17/timekit-0-3-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>We’ve just released <code>timekit</code> v0.3.0 to CRAN. The package updates include changes that help with making an accurate future time series with <code>tk_make_future_timeseries()</code> and we’ve added a few features to <code>tk_get_timeseries_signature()</code>. Most important are the <strong>new vignettes</strong> that cover both the making of future time series task and forecasting using the <code>timekit</code> package. If you saw <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/05/02/timekit-0-2-0.html" target="_blank">our last timekit post</a>, you were probably surprised to learn that <strong>you can use machine learning to forecast using the time series signature as an engineered feature space</strong>. Now we are expanding on that concept by providing two new vignettes that teach you how to use ML and data mining for time series predictions. We’re really excited about the prospects of ML applications with time series. If you are too, I strongly encourage you to explore the <code>timekit</code> package <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/05/17/timekit-0-3-0.html#important-links" target="_blank">important links</a> below. Don’t forget to <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/05/17/timekit-0-3-0.html#announcements" target="_blank">check out our announcements</a> and to <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/05/17/timekit-0-3-0.html#social" target="_blank">follow us on social media</a> to stay up on the latest <em>Business Science</em> news, events and information! Here’s a summary of the updates.</p>
<h1>Updates</h1>
<p>We’ve got some great new documentation to really help speed up usage of <code>timekit</code> particularly with respect to applications in forecasting. The doc’s are a big part of the update so we’ll start there first. The package changes have to do with the <code>tk_make_future_timeseries()</code> and <code>tk_get_timeseries_signature()</code> functions. Let’s explore.</p>
<h3>New Forecasting Vignette</h3>
<p>The vignette <a rel="nofollow" href="https://business-science.github.io/timekit/articles/TK03_Forecasting_Using_Time_Series_Signature.html" target="_blank">Forecasting Using the Time Series Signature with timekit</a> is a new resource for anyone interested in using <strong>data mining</strong> and <strong>machine learning</strong> to predict (forecast) future time-based observations. The idea is simple: there’s a lot of information within a time stamp that can be expanded into what is called the <strong>time series signature</strong>. Patterns between the target and the signature can be modeled or learned. The vignette uses the <a rel="nofollow" href="https://archive.ics.uci.edu/ml/datasets/bike+sharing+dataset" target="_blank">Bike Sharing Dataset</a> available at the UCI Machine Learning Repository and made popular by Kaggle Competitions. We walk the reader through the forecasting process using the time series signature from start to finish covering all major tasks including:</p>
<ul>
<li>Creating training and test sets</li>
<li>Modeling with the time series signature</li>
<li>Validating the model using the test set and inspecting the test set accuracy</li>
<li>Forecasting using the model and accounting for prediction intervals</li>
</ul>
<p>The reader learns how to start with a dataset and end with the forecast shown below!</p>
<p><img alt="Forecasting Bike Sharing" src="https://i2.wp.com/www.business-science.io/assets/timekit-0-3-0.png?w=456" data-recalc-dims="1"></p>
<p><center><a rel="nofollow" href="https://business-science.github.io/timekit/articles/TK03_Forecasting_Using_Time_Series_Signature.html" target="_blank">Check out the Forecasting Vignette!</a></center></p>
<h3>New Future Time Series Vignette</h3>
<p>Making a future time series can be difficult, especially when dealing with daily data that can have missing weekends, holidays, or periods of the year that activity is slower (or non-existent). Further, when forecasting using a time series signature it’s important to get the future time series sequence correct. Fortunately, we’ve put a lot of thought into creating a useful and user friendly function, <code>tk_make_future_timeseries()</code>, to help with this task.</p>
<p>We thought future timeseries accuracy was so important that <a rel="nofollow" href="https://business-science.github.io/timekit/articles/TK02_Making_A_Future_Time_Series_Index.html" target="_blank">we gave it a vignette</a>. In the vignette you’ll learn how to inspect a time series to identify patterns in the time series frequency. We show you how to plot the frequency and what to look for. Then, we show you how to use the various arguments to <code>tk_make_future_timeseries()</code> to input the existing index and output a future index that matches your expectations. We focus on the two main arguments that adjust the internal time-series-picking algorithm: <code>inspect_weekdays</code> and <code>inspect_months</code> (new!). We address the pros and cons to each argument by exposing the user to Type I and Type II errors. Then we show how to counteract these errors with <code>skip_values</code> and <code>insert_values</code> (new!). The vignette walks you through the entire process showing you how to create plots to analyze and simulate a future frequency like the one below.</p>
<p><img alt="Making Future Time Series" src="https://i2.wp.com/www.business-science.io/assets/timekit-0-3-0-2.png?w=456" data-recalc-dims="1"></p>
<p><center><a rel="nofollow" href="https://business-science.github.io/timekit/articles/TK02_Making_A_Future_Time_Series_Index.html" target="_blank">Check out the Future Time Series Index Vignette!</a></center></p>
<h3>Function Improvements: tk_make_future_timeseries()</h3>
<p>The function <code>tk_make_future_timeseries()</code> is a function designed to make your life easier when creating a future time series from an existing one. It evaluates the periodicity of the existing time series and then creates a future time series that matches the frequency. While it works with all frequencies from seconds to years, the biggest benefit is with daily time series.</p>
<p>The <code>tk_make_future_timeseries()</code> function is discussed at length in the <a rel="nofollow" href="https://business-science.github.io/timekit/articles/TK02_Making_A_Future_Time_Series_Index.html" target="_blank">vignette</a> so we’ll just go over the changes briefly. To summarize, the function already had the argument <code>inspect_weekdays</code> to look at days that are missing on a weekly, bi-weekly, tri-weekly and quad-weekly frequency. Now it has <code>inspect_months</code> to look for days that are missing on a monthly, quarterly, or yearly schedule. Further, the function already had a <code>skip_values</code> argument to skip irregular time sequences such as holidays. Now it has <code>insert_values</code> to add back in dates that might be incorrectly skipped by the algorithm.</p>
<p>The handling of the <code>n_future</code> argument was changed to now produce an end date that is the number of periods <em>including</em> days removed from <code>inspect_weekends</code>, <code>inspect_months</code>, and <code>skip_days</code>. This helps with keeping a <strong>constant end date</strong> while varying the inspection and skip days, which is more intuitive when creating a future time series. Here’s a brief example with <code>n_future = 90</code> using the FB stock from the FANG dataset in the <code>tidyquant</code> package. Note that the end date is always 90 total periods (“2017-03-30”) from the index end date.</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><span>
</span><span># Get FB stock prices
</span><span>FB_prices</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>adjusted</span><span>)</span><span>
</span><span># Extract index
</span><span>idx</span><span> </span><span>&lt;-</span><span> </span><span>tk_index</span><span>(</span><span>FB_prices</span><span>)</span><span>
</span><span># Sequential dates matching frequency of idx
</span><span>idx</span><span> </span><span>%>%</span><span>
</span><span>tk_make_future_timeseries</span><span>(</span><span>n_future</span><span> </span><span>=</span><span> </span><span>90</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##  [1] &quot;2016-12-31&quot; &quot;2017-01-01&quot; &quot;2017-01-02&quot; &quot;2017-01-03&quot; &quot;2017-01-04&quot;
##  [6] &quot;2017-01-05&quot; &quot;2017-01-06&quot; &quot;2017-01-07&quot; &quot;2017-01-08&quot; &quot;2017-01-09&quot;
## [11] &quot;2017-01-10&quot; &quot;2017-01-11&quot; &quot;2017-01-12&quot; &quot;2017-01-13&quot; &quot;2017-01-14&quot;
## [16] &quot;2017-01-15&quot; &quot;2017-01-16&quot; &quot;2017-01-17&quot; &quot;2017-01-18&quot; &quot;2017-01-19&quot;
## [21] &quot;2017-01-20&quot; &quot;2017-01-21&quot; &quot;2017-01-22&quot; &quot;2017-01-23&quot; &quot;2017-01-24&quot;
## [26] &quot;2017-01-25&quot; &quot;2017-01-26&quot; &quot;2017-01-27&quot; &quot;2017-01-28&quot; &quot;2017-01-29&quot;
## [31] &quot;2017-01-30&quot; &quot;2017-01-31&quot; &quot;2017-02-01&quot; &quot;2017-02-02&quot; &quot;2017-02-03&quot;
## [36] &quot;2017-02-04&quot; &quot;2017-02-05&quot; &quot;2017-02-06&quot; &quot;2017-02-07&quot; &quot;2017-02-08&quot;
## [41] &quot;2017-02-09&quot; &quot;2017-02-10&quot; &quot;2017-02-11&quot; &quot;2017-02-12&quot; &quot;2017-02-13&quot;
## [46] &quot;2017-02-14&quot; &quot;2017-02-15&quot; &quot;2017-02-16&quot; &quot;2017-02-17&quot; &quot;2017-02-18&quot;
## [51] &quot;2017-02-19&quot; &quot;2017-02-20&quot; &quot;2017-02-21&quot; &quot;2017-02-22&quot; &quot;2017-02-23&quot;
## [56] &quot;2017-02-24&quot; &quot;2017-02-25&quot; &quot;2017-02-26&quot; &quot;2017-02-27&quot; &quot;2017-02-28&quot;
## [61] &quot;2017-03-01&quot; &quot;2017-03-02&quot; &quot;2017-03-03&quot; &quot;2017-03-04&quot; &quot;2017-03-05&quot;
## [66] &quot;2017-03-06&quot; &quot;2017-03-07&quot; &quot;2017-03-08&quot; &quot;2017-03-09&quot; &quot;2017-03-10&quot;
## [71] &quot;2017-03-11&quot; &quot;2017-03-12&quot; &quot;2017-03-13&quot; &quot;2017-03-14&quot; &quot;2017-03-15&quot;
## [76] &quot;2017-03-16&quot; &quot;2017-03-17&quot; &quot;2017-03-18&quot; &quot;2017-03-19&quot; &quot;2017-03-20&quot;
## [81] &quot;2017-03-21&quot; &quot;2017-03-22&quot; &quot;2017-03-23&quot; &quot;2017-03-24&quot; &quot;2017-03-25&quot;
## [86] &quot;2017-03-26&quot; &quot;2017-03-27&quot; &quot;2017-03-28&quot; &quot;2017-03-29&quot; &quot;2017-03-30&quot;</code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span># Detects and removes weekends that are non-trade days
</span><span>idx</span><span> </span><span>%>%</span><span>
</span><span>tk_make_future_timeseries</span><span>(</span><span>n_future</span><span> </span><span>=</span><span> </span><span>90</span><span>,</span><span> </span><span>inspect_weekdays</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##  [1] &quot;2017-01-02&quot; &quot;2017-01-03&quot; &quot;2017-01-04&quot; &quot;2017-01-05&quot; &quot;2017-01-06&quot;
##  [6] &quot;2017-01-09&quot; &quot;2017-01-10&quot; &quot;2017-01-11&quot; &quot;2017-01-12&quot; &quot;2017-01-13&quot;
## [11] &quot;2017-01-16&quot; &quot;2017-01-17&quot; &quot;2017-01-18&quot; &quot;2017-01-19&quot; &quot;2017-01-20&quot;
## [16] &quot;2017-01-23&quot; &quot;2017-01-24&quot; &quot;2017-01-25&quot; &quot;2017-01-26&quot; &quot;2017-01-27&quot;
## [21] &quot;2017-01-30&quot; &quot;2017-01-31&quot; &quot;2017-02-01&quot; &quot;2017-02-02&quot; &quot;2017-02-03&quot;
## [26] &quot;2017-02-06&quot; &quot;2017-02-07&quot; &quot;2017-02-08&quot; &quot;2017-02-09&quot; &quot;2017-02-10&quot;
## [31] &quot;2017-02-13&quot; &quot;2017-02-14&quot; &quot;2017-02-15&quot; &quot;2017-02-16&quot; &quot;2017-02-17&quot;
## [36] &quot;2017-02-20&quot; &quot;2017-02-21&quot; &quot;2017-02-22&quot; &quot;2017-02-23&quot; &quot;2017-02-24&quot;
## [41] &quot;2017-02-27&quot; &quot;2017-02-28&quot; &quot;2017-03-01&quot; &quot;2017-03-02&quot; &quot;2017-03-03&quot;
## [46] &quot;2017-03-06&quot; &quot;2017-03-07&quot; &quot;2017-03-08&quot; &quot;2017-03-09&quot; &quot;2017-03-10&quot;
## [51] &quot;2017-03-13&quot; &quot;2017-03-14&quot; &quot;2017-03-15&quot; &quot;2017-03-16&quot; &quot;2017-03-17&quot;
## [56] &quot;2017-03-20&quot; &quot;2017-03-21&quot; &quot;2017-03-22&quot; &quot;2017-03-23&quot; &quot;2017-03-24&quot;
## [61] &quot;2017-03-27&quot; &quot;2017-03-28&quot; &quot;2017-03-29&quot; &quot;2017-03-30&quot;</code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span># Manually remove holidays that are irregularly spaced year-to-year
</span><span>holidays</span><span> </span><span>&lt;-</span><span> </span><span>ymd</span><span>(</span><span>&quot;2017-01-02&quot;</span><span>,</span><span> </span><span>&quot;2017-01-16&quot;</span><span>,</span><span> </span><span>&quot;2017-02-20&quot;</span><span>)</span><span>
</span><span>idx</span><span> </span><span>%>%</span><span>
</span><span>tk_make_future_timeseries</span><span>(</span><span>n_future</span><span> </span><span>=</span><span> </span><span>90</span><span>,</span><span> </span><span>inspect_weekdays</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>skip_values</span><span> </span><span>=</span><span> </span><span>holidays</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##  [1] &quot;2017-01-03&quot; &quot;2017-01-04&quot; &quot;2017-01-05&quot; &quot;2017-01-06&quot; &quot;2017-01-09&quot;
##  [6] &quot;2017-01-10&quot; &quot;2017-01-11&quot; &quot;2017-01-12&quot; &quot;2017-01-13&quot; &quot;2017-01-17&quot;
## [11] &quot;2017-01-18&quot; &quot;2017-01-19&quot; &quot;2017-01-20&quot; &quot;2017-01-23&quot; &quot;2017-01-24&quot;
## [16] &quot;2017-01-25&quot; &quot;2017-01-26&quot; &quot;2017-01-27&quot; &quot;2017-01-30&quot; &quot;2017-01-31&quot;
## [21] &quot;2017-02-01&quot; &quot;2017-02-02&quot; &quot;2017-02-03&quot; &quot;2017-02-06&quot; &quot;2017-02-07&quot;
## [26] &quot;2017-02-08&quot; &quot;2017-02-09&quot; &quot;2017-02-10&quot; &quot;2017-02-13&quot; &quot;2017-02-14&quot;
## [31] &quot;2017-02-15&quot; &quot;2017-02-16&quot; &quot;2017-02-17&quot; &quot;2017-02-21&quot; &quot;2017-02-22&quot;
## [36] &quot;2017-02-23&quot; &quot;2017-02-24&quot; &quot;2017-02-27&quot; &quot;2017-02-28&quot; &quot;2017-03-01&quot;
## [41] &quot;2017-03-02&quot; &quot;2017-03-03&quot; &quot;2017-03-06&quot; &quot;2017-03-07&quot; &quot;2017-03-08&quot;
## [46] &quot;2017-03-09&quot; &quot;2017-03-10&quot; &quot;2017-03-13&quot; &quot;2017-03-14&quot; &quot;2017-03-15&quot;
## [51] &quot;2017-03-16&quot; &quot;2017-03-17&quot; &quot;2017-03-20&quot; &quot;2017-03-21&quot; &quot;2017-03-22&quot;
## [56] &quot;2017-03-23&quot; &quot;2017-03-24&quot; &quot;2017-03-27&quot; &quot;2017-03-28&quot; &quot;2017-03-29&quot;
## [61] &quot;2017-03-30&quot;</code></pre>
</figure>
<h3>Function Improvements: tk_get_timeseries_signature()</h3>
<p>We’ve included a few new features that are output when using <code>tk_get_timeseries_signature()</code>. The full list of features can be found on <a rel="nofollow" href="https://business-science.github.io/timekit/articles/TK01_Working_With_Time_Series_Index.html#tk_get_timeseries_signature-and-tk_augment_timeseries_signature" target="_blank">our pkgdown site</a>. The new features include:</p>
<ul>
<li><strong>hour12</strong>: The hour component on a 12 hour scale.</li>
<li><strong>am.pm</strong>: Morning (AM) = 1, Afternoon (PM) = 2.</li>
<li><strong>qday</strong>: The day of the quarter.</li>
<li><strong>mweek</strong>: The week of the month.</li>
<li><strong>mday7</strong>: The integer division of day of the month by seven. Returns the first, second, third, … instance that the day has appeared in the month. Values begin at 1. For example, the first Saturday in the month has mday7 = 1. The second Saturday has mday7 = 2.</li>
</ul>
<p>We are now up to 28 features (including the original index) that can be data mined for relationships with the target (response or independent variable).</p>
<figure>
<pre><code data-lang="r"><span>idx</span><span> </span><span>%>%</span><span>
</span><span>tk_get_timeseries_signature</span><span>()</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 1,008 × 28
##         index  index.num   diff  year  half quarter month month.xts
##        </code></pre></figure>