Integrating new classes to tsbox is relatively easy. The following discusses some of the classes, starting with a very new one.

tsibble

The tsibble package provides a data class of tbl_ts to store time series data frames in a ‘tidy’ manner. A tsibble consists of a time index, keys and other measured variables and is built on top of the tibble.

tsbox converters can be applied to tsibbles the usual way:

suppressMessages(library(tsibble))
library(tsbox)

x.tsbl <- ts_tsibble(AirPassengers)
head(x.tsbl)
head(ts_xts(x.tsbl))
head(ts_ts(x.tsbl))
head(ts_df(x.tsbl))
# etc.

Here is a longer example, using the example from the tsibble documentation.

tsbox want deals with multiple time series in the long format, so we transform the variables into the desired form, using ts_long:

suppressMessages(library(dplyr))
dta.tbl <- nycflights13::weather %>% 
  select(origin, time = time_hour, temp, humid, precip) %>% 
  ts_long() 

Next, we convert the tibble to a tsibble. Because tsbox guesses the time and the value and the key columns, calling ts_tsibble is sufficient:

dta.tsibble <-
  dta.tbl %>% 
  ts_tsibble() 

With a long tsibble at hand, we can apply all usual functions from tsbox:

dta.tsibble %>% 
  filter(id == "temp") %>% 
  ts_span(start = "2013-08-01") %>% 
  ts_trend() %>% 
  ts_plot() 

Because tsbox functions work identical for all ts-boxable objects, the same commands could be used, e.g., on a standard tibble:

dta.tbl %>% 
  filter(id == "temp") %>% 
  ts_span(start = "2013-08-01") %>% 
  ts_trend() %>% 
  ts_plot() 

Aggregation, 3 ways

Some special functionality is offered by the tsibble package The following shows how aggregation can be performed using tsibbles, tibbles or any ts-boxable object:

the tsibble way

library(data.table) # for month(), year()
first_of_month <- function(x) as.Date(paste(year(x), month(x), 1, sep = "-"))

a1 <- dta.tsibble %>%
  group_by(origin, id) %>% 
  tsummarise(
    time = first_of_month(time),
    value = mean(value, na.rm = TRUE)
  )

the dplyr way

a2 <- dta.tsibble %>%
  ts_tbl %>% 
  group_by(origin, id, time = first_of_month(time)) %>%
  summarise(
    value = mean(value, na.rm = TRUE)
  )

the tsbox way

a3 <- dta.tsibble %>%
  ts_frequency("month", mean, na.rm = TRUE)
all.equal(a1, a2)
all.equal(a1, a3)