<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/time-series-of-the-world-unite/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://usefulr.wordpress.com/2018/05/15/time-series-of-the-world-unite/"> R – usefulr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>The R ecosystem knows a <a rel="nofollow" href="https://cran.r-project.org/view=TimeSeries" target="_blank">ridiculous number</a> of time series classes. So, I decided to create a new universal standard that finally covers everyone’s use case… Ok, <a rel="nofollow" href="https://xkcd.com/927/" target="_blank">just kidding!</a></p>
<p><a rel="nofollow" href="https://www.tsbox.help/" target="_blank">tsbox</a>, now freshly on <a rel="nofollow" href="https://cran.r-project.org/package=tsbox" target="_blank">CRAN</a>, provides a set of tools that are agnostic towards existing time series classes. It is built around a set of converters, which convert time series stored as <a rel="nofollow" href="https://www.rdocumentation.org/packages/stats/versions/3.5.0/topics/ts?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">ts</a>, <a rel="nofollow" href="https://cran.r-project.org/package=xts" target="_blank">xts</a>, <a rel="nofollow" href="https://www.rdocumentation.org/packages/stats/versions/3.5.0/topics/data.frame?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">data.frame</a>, <a rel="nofollow" href="https://cran.r-project.org/package=data.table" target="_blank">data.table</a>, <a rel="nofollow" href="http://tibble.tidyverse.org/" target="_blank">tibble</a>, <a rel="nofollow" href="https://cran.r-project.org/package=zoo" target="_blank">zoo</a>, <a rel="nofollow" href="https://pkg.earo.me/tsibble/" target="_blank">tsibble</a> or <a rel="nofollow" href="https://cran.r-project.org/package=timeSeries" target="_blank">timeSeries</a> to each other.</p>
<p>To install the stable version from CRAN:</p>
<pre>install.packages(&quot;tsbox&quot;)</pre>
<p>To get an idea how easy it is to switch from one class to another, consider this:</p>
<pre>library(tsbox)
x.ts &lt;- ts_c(mdeaths, fdeaths)
x.xts &lt;- ts_xts(x.ts)
x.df &lt;- ts_df(x.xts)
x.tbl &lt;- ts_tbl(x.df)
x.dt &lt;- ts_tbl(x.tbl)
x.zoo &lt;- ts_zoo(x.dt)
x.tsibble &lt;- ts_tsibble(x.zoo)
x.timeSeries &lt;- ts_timeSeries(x.tsibble)</pre>
<p>We jump form good old <code>ts</code> objects to<code>xts</code>, store our time series in various data frames and convert them to some highly specialized time series formats.</p>
<div>
<h3>tsbox is class-agnostic</h3>
<p>Because these converters work nicely, we can use them to make functions class-agnostic. If a class-agnostic function works for one class, it works for all:</p>
<pre>ts_scale(x.ts)           
ts_scale(x.xts)
ts_scale(x.df)
ts_scale(x.dt)
ts_scale(x.tbl)
</pre>
<p><code>ts_scale</code> normalizes one or multiple series, by subtracting the mean and dividing by the standard deviation. It works like a ‘generic’ function: You can apply it on any time series object, and it will return an object of the same class as its input.</p>
<p>So, whether we want to smooth, scale, differentiate, chain-link, forecast, regularize or seasonally adjust a series, we can use the same commands to whatever time series at hand. tsbox offers a comprehensive toolkit for the basics of time series manipulation. Here are some additional operations:</p>
<pre>ts_pc(x.ts)                 # percentage change rates 
ts_forecast(x.xts)          # forecast, by exponential smoothing
ts_seas(x.df)               # seasonal adjustment, by X-13
ts_frequency(x.dt, &quot;year&quot;)  # convert to annual frequency
ts_span(x.tbl, &quot;-1 year&quot;)   # limit time span to final year</pre>
</div>
<div>
<h3>tsbox is frequency-agnostic</h3>
<p>There are many more. Because they all start with <code>ts_</code>, you can use auto-complete to see what’s around. Most conveniently, there is a time series plot function that works for all classes and frequencies:</p>
<pre>ts_plot(
  `Airline Passengers` = AirPassengers, 
  `Lynx trappings` = ts_df(lynx), 
  `Deaths from Lung Diseases` = ts_xts(fdeaths),
  title = &quot;Airlines, trappings, and deaths&quot;,
  subtitle = &quot;Monthly passengers, annual trappings, monthly deaths&quot;
)</pre>
<p><img data-orig-file="https://usefulr.files.wordpress.com/2018/05/unnamed-chunk-2-1.png?w=456" data-orig-size="1344,960" data-medium-file="https://usefulr.files.wordpress.com/2018/05/unnamed-chunk-2-1.png?w=456?w=300" data-attachment-id="260" data-image-meta="{" data-image-description="" src="https://usefulr.files.wordpress.com/2018/05/unnamed-chunk-2-1.png?w=456" data-permalink="https://usefulr.wordpress.com/2018/05/15/time-series-of-the-world-unite/unnamed-chunk-2-1/" data-large-file="https://usefulr.files.wordpress.com/2018/05/unnamed-chunk-2-1.png?w=456?w=625" alt="unnamed-chunk-2-1" data-image-title="unnamed-chunk-2-1" sizes="(max-width: 625px) 100vw, 625px" data-comments-opened="1" class="wp-image-260" data-recalc-dims="1"></p>
<p>There is also a version that uses <a rel="nofollow" href="https://cran.r-project.org/package=ggplot2" target="_blank">ggplot2</a> and has the same syntax.</p>
</div>
<div>
<h3>Time series in data frames</h3>
<p>You may have wondered why we treated data frames as a time series class. The spread of <a rel="nofollow" href="https://cran.r-project.org/package=dplyr" target="_blank">dplyr</a> and <a rel="nofollow" href="https://cran.r-project.org/package=data.table" target="_blank">data.table</a> has given data frames a boost and made them one of the most popular data structures in R. So, storing time series in a data frame is an obvious consequence. And even if you don’t intend to keep time series in data frames, this is still the format in which you import and export your data. tsbox makes it easy to switch from data frames to time series and back.</p>
</div>
<div>
<h3>Make existing functions class-agnostic</h3>
<p>tsbox includes <a rel="nofollow" href="https://www.tsbox.help/articles/ts-functions.html" target="_blank">tools</a> to make existing functions class-agnostic. To do so, the <code>ts_</code> function can be used to wrap any function that works with time series. For a function that works on <code>&quot;ts&quot;</code> objects, this is as simple as that:</p>
<pre>ts_rowsums &lt;- ts_(rowSums)
ts_rowsums(ts_c(mdeaths, fdeaths))
</pre>
<p>Note that <code>ts_</code> returns a function, which can be used with or without a name.</p>
<p>In case you are wondering, tsbox uses <a rel="nofollow" href="https://cran.r-project.org/package=data.table" target="_blank">data.table</a> as a backend, and makes use of its incredibly efficient reshaping facilities, its joins and rolling joins. And thanks to <a rel="nofollow" href="https://cran.r-project.org/package=anytime" target="_blank">anytime</a>, tsbox will be able to recongnize almost any date format without manual intervention.</p>
<p>So, enjoy some relieve in R’s time series class struggle.</p>
<p>Website: <a rel="nofollow" href="https://www.tsbox.help/" target="_blank">www.tsbox.help</a></p>
</div>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://usefulr.wordpress.com/2018/05/15/time-series-of-the-world-unite/"> R – usefulr</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/B5JkJq5vFjk" height="1"></a></div>