<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/bizsci-package-updates-formerly-timekit-now-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/07/27/timetk-0-1-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 have several announcements regarding <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/07/27/timetk-0-1-0.html#contact" target="_blank"><em>Business Science</em></a> R packages. First, as of this week the R package formerly known as <code>timekit</code> has changed to <code>timetk</code> for <strong>time series tool kit</strong>. There are a few “breaking” changes because of the name change, and this is discussed further below. Second, the <code>sweep</code> and <code>tidyquant</code> packages have several improvements, which are discussed in detail below. Finally, don’t miss a beat on future news, events and information by <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/07/27/timetk-0-1-0.html#social" target="_blank">following us on social media</a>.</p>
<h1>timetk</h1>
<p>The <code>timetk</code> package (formerly <code>timekit</code>) is a relatively new package that is aimed at assisting users with working with time series in R. It helps users switch back and forth between time based “tibbles” (tidy data frames with dates or date times) and the other time series objects in R (<code>xts</code>, <code>zoo</code>, <code>ts</code>, etc). Equally important, <code>timetk</code> includes functions that help setup time series for data mining and machine learning. Here’s an example from <a rel="nofollow" href="https://business-science.github.io/timetk/articles/TK03_Forecasting_Using_Time_Series_Signature.html" target="_blank">Vignette #4: Forecasting Using a Time Series Signature with timetk</a>:</p>
<p><img alt="Time Series Machine Learning with timetk" src="https://i0.wp.com/www.business-science.io/assets/timetk-0-1-0.png?w=456" data-recalc-dims="1"></p>
<p><strong>Why the name change?</strong> The name change was made to help differentiate from <a rel="nofollow" href="https://www.timekit.io/" target="_blank"><em>timekit.io</em></a>. It was in both parties best interest to differentiate, which is less confusing to users of both organizations’ software.</p>
<p>The primary change is to the name of the package, and most functions are the same. There are however a few “breaking changes” that are a result of the name change. <strong>We made the transition very simple. All you need to do to refactor is “Ctrl+F” or “Cmd+F” and find and replace “timekit” with “timetk”, and everything will work. We promise!</strong></p>
<p>Here’s some examples of the changes. Notice the functions are all the same. Only changes are related to “timekit”.</p>
<ol>
<li>
<p>The function <code>has_timekit_index()</code> now changes to <code>has_timetk_index()</code>. This function is used to detect if a <code>ts</code> object has a “timetk index” (non-regularized date or datetime, which are present in <code>ts</code> objects coerced with <code>tk_ts()</code>). Again, just refactor and you will be fine.</p>
</li>
<li>
<p>Functions with the boolean argument <code>timekit_idx</code> have the argument changed to <code>timetk_idx</code>. Examples include <code>tk_index(timetk_idx = TRUE)</code> and <code>tk_tbl(timetk_idx = TRUE)</code>. The <code>timetk_idx</code> argument enables retrieving a non-regularized date or datetime series rather than the regularized time series typically present in <code>ts</code> objects. Note that this only is applicable if the <code>tk_ts()</code> coercion function is used during initial coercion to a <code>ts</code> object. Refer to the <a rel="nofollow" href="https://business-science.github.io/timetk/articles/TK00_Time_Series_Coercion.html" target="_blank">Vignette #1: Time Series Coercion Using timetk</a> for more details.</p>
</li>
</ol>
<p>That’s it. It should be very easy to make the transition to <code>timetk</code> via a simple refactor. Please let us know if you have any issues. You can contact us at <a rel="nofollow" href="mailto:info@business-science.io" target="_blank">info@business-science.io</a> or via <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/07/27/timetk-0-1-0.html#social" target="_blank">social media</a> below.</p>
<h1>sweep</h1>
<p>The <code>sweep</code> package is designed to “tidy” the model and forecast output of packages that use the <code>ts</code> system. The most popular example is the <code>forecast</code> package. The package uses <code>broom</code>-style tidiers (<code>sw_tidy</code>, <code>sw_glance</code>, <code>sw_augment</code> and <code>sw_sweep</code>) to convert the output to “tibbles”. Here’s an example from our <a rel="nofollow" href="http://www.business-science.io/code-tools/2017/07/09/sweep-0-1-0.html" target="_blank">recent sweep blog post</a> where we collected GDP for each US state and plotted “tidy” ARIMA forecasts.</p>
<p><img alt="Time Series Machine Learning with timetk" src="https://i1.wp.com/www.business-science.io/assets/state_forecast.png?w=456" data-recalc-dims="1"></p>
<p>The main addition to <code>sweep</code> v0.2.0 is support for the <a rel="nofollow" href="https://cran.r-project.org/package=robets" target="_blank"><code>robets</code> package</a>. The addition was user supplied via pull request (thanks <a rel="nofollow" href="https://github.com/joelgombin" target="_blank">Joel Gombin</a>). We highly encourage users interested in converting models in other <code>ts</code>-based packages to “tidy” output to submit pull requests! Let us know if you are interested in helping.</p>
<p>The other main change is to the <code>sw_sweep()</code> function, which is used to convert <code>forecast</code> objects to “tidy” data frames. Because it uses <code>timetk</code> under the hood to convert the <code>ts</code> object time series to date or datetime, we changed the <code>timekit_idx</code> argument to <code>timetk_idx</code>. Again, just refactor to change to the <code>sw_sweep(timetk_idx)</code> argument. For more information, refer to the <a rel="nofollow" href="https://cran.r-project.org/web/packages/sweep/vignettes/SW00_Introduction_to_sweep.html" target="_blank">Introduction to sweep Vignette </a>.</p>
<h1>tidyquant</h1>
<p>The <code>tidyquant</code> package bridges a gap between the “tidyverse” and many of the financial and time series packages that depend on the <code>xts</code> and <code>zoo</code> time series objects. The main benefit is the scale-ability to perform grouped operations, which can be difficult in the <code>xts</code> system when managing multiple time series. Here’s a simple example to show why you might consider using <code>tidyquant</code>. The script below retrieves the past 10-years of stock prices for every stock in the SP500, then calculates the average and standard deviation of the daily returns.</p>
<figure>
<pre><code data-lang="r"><span># Analyzing every stock in SP500
</span><span>library</span><span>(</span><span>tidyquant</span><span>)</span><span>
</span><span>sp500_analysis</span><span> </span><span>&lt;-</span><span> </span><span>tq_index</span><span>(</span><span>&quot;SP500&quot;</span><span>)</span><span> </span><span>%>%</span><span> </span><span># Get stock list
</span><span> </span><span># Get stock prices for each stock in list
</span><span> </span><span>tq_get</span><span>()</span><span> </span><span>%>%</span><span> </span><span># Group for grouped analysis
</span><span> </span><span>group_by</span><span>(</span><span>symbol</span><span>)</span><span> </span><span>%>%</span><span> </span><span># Convert from daily prices to daily log(returns)
</span><span> </span><span>tq_transmute</span><span>(</span><span> </span><span>select</span><span> </span><span>=</span><span> </span><span>adjusted</span><span>,</span><span>
</span><span>mutate_fun</span><span> </span><span>=</span><span> </span><span>periodReturn</span><span>,</span><span>
</span><span>period</span><span> </span><span>=</span><span> </span><span>&quot;daily&quot;</span><span>,</span><span>
</span><span>type</span><span> </span><span>=</span><span> </span><span>&quot;log&quot;</span><span>,</span><span>
</span><span>col_rename</span><span> </span><span>=</span><span> </span><span>&quot;daily.log.return&quot;</span><span>
</span><span>)</span><span> </span><span>%>%</span><span>
</span><span># Calculate mean and stdev of log returns
</span><span> </span><span>summarize</span><span>(</span><span> </span><span>dlr_mean</span><span> </span><span>=</span><span> </span><span>mean</span><span>(</span><span>daily.log.return</span><span>),</span><span>
</span><span>dlr_sd</span><span> </span><span>=</span><span> </span><span>sd</span><span>(</span><span>daily.log.return</span><span>)</span><span>
</span><span>)</span></code></pre>
</figure>
<p>Here’s a useful plot you can make with the data. An investor can easily focus on stocks that have lower risk and higher reward metrics.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>ggrepel</span><span>)</span><span>
</span><span># Plot mean vs standard deviation of daily log returns
</span><span>sp500_analysis</span><span> </span><span>%>%</span><span>
</span><span>ggplot</span><span>(</span><span>aes</span><span>(</span><span>x</span><span> </span><span>=</span><span> </span><span>dlr_sd</span><span>,</span><span> </span><span>y</span><span> </span><span>=</span><span> </span><span>dlr_mean</span><span>))</span><span> </span><span>+</span><span>
</span><span># Data viz
</span><span> </span><span>geom_point</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>palette_light</span><span>()[[</span><span>1</span><span>]],</span><span> </span><span>alpha</span><span> </span><span>=</span><span> </span><span>0.5</span><span>)</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>(</span><span>method</span><span> </span><span>=</span><span> </span><span>&quot;lm&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span># Show high ratio of returns to volatility
</span><span> </span><span>geom_label_repel</span><span>(</span><span>
</span><span>aes</span><span>(</span><span>label</span><span> </span><span>=</span><span> </span><span>symbol</span><span>),</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>palette_light</span><span>()[[</span><span>1</span><span>]],</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>subset</span><span>(</span><span>sp500_analysis</span><span>,</span><span> </span><span>dlr_mean</span><span> </span><span>/</span><span> </span><span>dlr_sd</span><span> </span><span>></span><span> </span><span>.04</span><span>)</span><span>
</span><span>)</span><span> </span><span>+</span><span>
</span><span># Aethetics
</span><span> </span><span>scale_x_continuous</span><span>(</span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>0.01</span><span>,</span><span> </span><span>0.05</span><span>))</span><span> </span><span>+</span><span>
</span><span>scale_y_continuous</span><span>(</span><span>limits</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>-0.001</span><span>,</span><span> </span><span>0.002</span><span>))</span><span> </span><span>+</span><span>
</span><span>labs</span><span>(</span><span>
</span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;SP500: Investigating Average Return vs Average Standard Deviation&quot;</span><span>,</span><span>
</span><span>subtitle</span><span> </span><span>=</span><span> </span><span>&quot;Stocks with lower standard deviation tend to have higher returns; focus on stocks with high ratio of returns to stdev&quot;</span><span>,</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>&quot;Standard Deviation of Daily Log Returns&quot;</span><span>,</span><span>
</span><span>y</span><span> </span><span>=</span><span> </span><span>&quot;Mean of Daily Log Returns&quot;</span><span>
</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_tq</span><span>()</span></code></pre>
</figure>
<p><img alt="plot of chunk unnamed-chunk-3" src="https://i2.wp.com/www.business-science.io/figure/source/2017-7-27-timetk-0-1-0/unnamed-chunk-3-1.png?w=456" data-recalc-dims="1"></p>
<p><strong>What are the changes?</strong> There’s two main changes in <code>tidyquant</code> v0.5.2.</p>
<p>First, <code>tq_index()</code>, the function used to get stock indexes such as SP500, DOW, and RUSSELL2000 now collects its data from <a rel="nofollow" href="https://us.spdrs.com/en" target="_blank">SPDRs</a>. The <code>tq_index_options</code> are:</p>
<figure>
<pre><code data-lang="r"><span># List of indexes available for download, supply desired option to tq_index()
</span><span>tq_index_options</span><span>()</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## [1] &quot;RUSSELL1000&quot; &quot;RUSSELL2000&quot; &quot;RUSSELL3000&quot; &quot;DOW&quot;        
## [5] &quot;DOWGLOBAL&quot;   &quot;SP400&quot;       &quot;SP500&quot;       &quot;SP600&quot;      
## [9] &quot;SP1000&quot;</code></pre>
</figure>
<p>The return from <code>tq_index()</code> now include weight, sector, and shares_held from the associated SPDR.</p>
<figure>
<pre><code data-lang="r"><span># New source for index
</span><span>library</span><span>(</span><span>tidyquant</span><span>)</span><span>
</span><span>tq_index</span><span>(</span><span>&quot;SP500&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>head</span><span>()</span></code></pre>
</figure>
<table>
<thead>
<tr>
<th>symbol</th>
<th>company</th>
<th>weight</th>
<th>sector</th>
<th>shares_held</th>
</tr>
</thead>
<tbody>
<tr>
<td>AAPL</td>
<td>Apple Inc.</td>
<td>0.0377152</td>
<td>Information Technology</td>
<td>59372276</td>
</tr>
<tr>
<td>MSFT</td>
<td>Microsoft Corporation</td>
<td>0.0269475</td>
<td>Information Technology</td>
<td>87913770</td>
</tr>
<tr>
<td>AMZN</td>
<td>Amazon.com Inc.</td>
<td>0.0196867</td>
<td>Consumer Discretionary</td>
<td>4517403</td>
</tr>
<tr>
<td>FB</td>
<td>Facebook Inc. Class A</td>
<td>0.0184511</td>
<td>Information Technology</td>
<td>26915296</td>
</tr>
<tr>
<td>JNJ</td>
<td>Johnson &amp; Johnson</td>
<td>0.0166280</td>
<td>Health Care</td>
<td>30675892</td>
</tr>
<tr>
<td>XOM</td>
<td>Exxon Mobil Corporation</td>
<td>0.0160500</td>
<td>Energy</td>
<td>48243920</td>
</tr>
</tbody>
</table>
<p>Second, the <code>tidyquant::as_tibble()</code> and <code>tidyquant::as_xts()</code> functions are now deprecated. These were used to convert between xts and time-based tibble objects. You can still use them (for now), but you will receive a warning. Rather, you should transition to the more robust <code>timetk</code> <code>tk_tbl()</code> and <code>tk_xts()</code> functions, which do the same thing in a more automated way.</p>
<figure>
<pre><code data-lang="r"><span># Create some prices as time-based tibble
</span><span>set.seed</span><span>(</span><span>8357</span><span>)</span><span>
</span><span>prices_tbl</span><span> </span><span>&lt;-</span><span> </span><span>tibble</span><span>(</span><span>
</span><span>date</span><span> </span><span>=</span><span> </span><span>seq.Date</span><span>(</span><span>from</span><span> </span><span>=</span><span> </span><span>as.Date</span><span>(</span><span>&quot;2017-01-01&quot;</span><span>),</span><span> </span><span>to</span><span> </span><span>=</span><span> </span><span>as.Date</span><span>(</span><span>&quot;2017-01-10&quot;</span><span>),</span><span> </span><span>by</span><span> </span><span>=</span><span> </span><span>&quot;day&quot;</span><span>),</span><span>
</span><span>price</span><span> </span><span>=</span><span> </span><span>seq</span><span>(</span><span>from</span><span> </span><span>=</span><span> </span><span>100</span><span>,</span><span> </span><span>120</span><span>,</span><span> </span><span>length.out</span><span> </span><span>=</span><span> </span><span>10</span><span>)</span><span> </span><span>+</span><span> </span><span>rnorm</span><span>(</span><span>n</span><span> </span><span>=</span><span> </span><span>10</span><span>)</span><span>
</span><span>)</span></code></pre>
</figure>
<p>Use <code>timetk::tk_xts()</code> to coerce <em>any time based object</em> to <code>xts</code>. Dates are converted and dropped automatically. Use <code>silent = TRUE</code> to eliminate messages describing date column being dropped and converted to index. This replaces <code>tidyquant::as_xts()</code> which only worked with tibble objects.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>timetk</span><span>)</span><span>
</span><span># Use tk_xts() to easily coerce to xts
</span><span>prices_xts</span><span> </span><span>&lt;-</span><span> </span><span>tk_xts</span><span>(</span><span>prices_tbl</span><span>,</span><span> </span><span>silent</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span>
</span><span>prices_xts</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">##                price
## 2017-01-01  99.22609
## 2017-01-02 100.98272
## 2017-01-03 103.50840
## 2017-01-04 105.20828
## 2017-01-05 110.83621
## 2017-01-06 110.63730
## 2017-01-07 113.96259
## 2017-01-08 115.70832
## 2017-01-09 117.94495
## 2017-01-10 120.25419</code></pre>
</figure>
<figure>
<pre><code data-lang="r"><span>class</span><span>(</span><span>prices_xts</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## [1] &quot;xts&quot; &quot;zoo&quot;</code></pre>
</figure>
<p>Use <code>timetk::tk_tbl()</code> to coerce <em>any time based object</em> to <code>tibble</code>. This replaces <code>tidyquant::as_tibble()</code> which only worked with <code>xts</code> objects.</p>
<figure>
<pre><code data-lang="r"><span># Use tk_tbl() to easily coerce back to tbl
</span><span>tk_tbl</span><span>(</span><span>prices_xts</span><span>,</span><span> </span><span>rename_index</span><span> </span><span>=</span><span> </span><span>&quot;date&quot;</span><span>,</span><span> </span><span>silent</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">## # A tibble: 10 x 2
##          date     price
##        </code></pre></figure>