Learn R Programming

⚠️There's a newer version (0.4.1) of this package.Take me there.

tsbox: Class-Agnostic Time Series in R

The R ecosystem knows a vast number of time series standards. Instead of creating the ultimate 15th time series class, tsbox provides a set of tools that are agnostic towards the existing standards. The tools also allow you to handle time series as plain data frames, thus making it easy to deal with time series in a dplyr or data.table workflow.

See tsbox.help for the full documentation of the package.

To install the stable version from CRAN:

install.packages("tsbox")

To install the development version:

# install.packages("remotes")
remotes::install_github("christophsax/tsbox")

Convert everything to everything

tsbox is built around a set of converters, which convert time series stored as ts, xts, data.frame, data.table, tibble, zoo, tsibble, tibbletime, timeSeries, irts or tis to each other:

library(tsbox)
x.ts <- ts_c(fdeaths, mdeaths)
x.xts <- ts_xts(x.ts)
x.df <- ts_df(x.xts)
x.dt <- ts_dt(x.df)
x.tbl <- ts_tbl(x.dt)
x.zoo <- ts_zoo(x.tbl)
x.tsibble <- ts_tsibble(x.zoo)
x.tibbletime <- ts_tibbletime(x.tsibble)
x.timeSeries <- ts_timeSeries(x.tibbletime)
x.irts <- ts_irts(x.tibbletime)
x.tis <- ts_tis(x.irts)
all.equal(ts_ts(x.tis), x.ts)
#> [1] TRUE

Use same functions for time series classes

Because this works reliably, it is easy to write functions that work for all classes. So whether we want to smooth, scale, differentiate, chain, forecast, regularize or seasonally adjust a time series, we can use the same commands to whatever time series class at hand:

ts_trend(x.ts)
ts_pc(x.xts)
ts_pcy(x.df)
ts_lag(x.dt)

Time series of the world, unite!

A set of helper functions makes it easy to combine or align multiple time series of all classes:

# collect time series as multiple time series
ts_c(ts_dt(EuStockMarkets), AirPassengers)
ts_c(EuStockMarkets, mdeaths)

# combine time series to a new, single time series
ts_bind(ts_dt(mdeaths), AirPassengers)
ts_bind(ts_xts(AirPassengers), ts_tbl(mdeaths))

And plot just about everything

Plotting all kinds of classes and frequencies is as simple as it should be. And we finally get a legend!

ts_plot(ts_scale(ts_c(mdeaths, austres, AirPassengers, DAX = EuStockMarkets[ ,'DAX'])))

Cheatsheet

Copy Link

Version

Install

install.packages('tsbox')

Monthly Downloads

2,658

Version

0.3.1

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

September 16th, 2021

Functions in tsbox (0.3.1)

copy_class

Re-Class ts-Boxable Object
ts_c

Collect Time Series
ts_dts

Internal Time Series Class
relevant_class

Extract Relevant Class
ts_boxable

Test if an Object is ts-Boxable
ts_

Constructing ts-Functions
ts_bind

Bind Time Series
ts_examples

Principal Components, Dygraphs, Forecasts, Seasonal Adjustment
ts_default

Default Column Names
ts_arithmetic

Arithmetic Operators for ts-boxable objects
ts_pick

Pick Series (Experimental)
ts_plot

Plot Time Series
ts_frequency

Change Frequency
ts_first_of_period

Use First Date of a Period
tsbox-package

tsbox: Class-Agnostic Time Series
ts_span

Limit Time Span
ts_pc

First Differences and Percentage Change Rates
ts_scale

Normalized Time Series
ts_na_omit

Omit NA values
ts_ggplot

Plot Time Series, Using ggplot2
ts_ts

Convert Everything to Everything
ts_index

Indices from Levels or Percentage Rates
tsbox-deprecated

Start and end of time series
ts_regular

Enforce Regularity
ts_save

Save Previous Plot
ts_trend

Loess Trend Estimation
ts_long

Reshaping Multiple Time Series
ts_lag

Lag or Lead of Time Series
ts_summary

Time Series Properties