/ˈt͡sɪbəl/

Travis-CI Build Status Coverage Status CRAN_Status_Badge Downloads lifecycle

The tsibble package provides a data class of tbl_ts to represent tidy temporal-context data. A tsibble consists of a time index, key and other measured variables in a data-centric format, which is built on top of the tibble.

Installation

You could install the stable version on CRAN:

You could install the development version from Github using

# install.packages("devtools")
devtools::install_github("tidyverts/tsibble", build_vignettes = TRUE)

Usage

Coerce to a tsibble with as_tsibble()

The weather data included in the package nycflights13 is used as an example to illustrate. The “index” variable is the time_hour containing the date-times, and the “key” is the origin as weather stations created via id(). The key(s) together with the index uniquely identifies each observation, which gives a valid tsibble. Other columns can be considered as measured variables.

The key is not constrained to a single variable, but expressive of nested and crossed data structures. This incorporates univariate, multivariate, hierarchical and grouped time series into the tsibble framework. See package?tsibble and vignette("intro-tsibble") for details.

The tsibble internally computes the interval for given time indices based on the time representation, ranging from year to nanosecond. The POSIXct corresponds to sub-daily series, Date to daily, yearweek to weekly, yearmonth/yearmth to monthly, yearquarter/yearqtr to quarterly, and etc.

fill_na() to turn implicit missing values into explicit missing values

Often there are implicit missing cases in temporal data. If the observations are made at regular time interval, we could turn these implicit missings to be explicit simply using fill_na(). Meanwhile, fill NAs in by 0 for precipitation (precip). It is quite common to replaces NAs with its previous observation for each origin in time series analysis, which is easily done using fill() from tidyr.

fill_na() also handles filling NA by values or functions, and preserves time zones for date-times. Wanna a quick overview of implicit time gaps? Check out count_gaps().

index_by() + summarise() to aggregate over calendar periods

index_by() is the counterpart of group_by() in temporal context, but it groups the index only. In conjunction with index_by(), summarise() and its scoped variants aggregate interested variables over calendar periods. index_by() goes hand in hand with the index functions including as.Date(), yearweek(), yearmonth(), and yearquarter(), as well as other friends from lubridate. For example, it would be of interest in computing average temperature and total precipitation per month, by applying yearmonth() to the hourly time index.

While collapsing rows (like summarise()), group_by() and index_by() will take care of updating the key and index respectively. This index_by() + summarise() combo can help with regularising a tsibble of irregular time space too.

A family of window functions: slide(), tile(), stretch()

Temporal data often involves moving window calculations. Several functions in tsibble allow for different variations of moving windows using purrr-like syntax:

For example, a moving average of window size 3 is carried out on hourly temperatures for each group (origin).

Working with the tidyverse

It can be noticed that the tsibble seamlessly works with tidyverse verbs, but in a slightly different way that it does the best to keep the index. Use ?tidyverse for a full list of tidyverse functions.


Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.