tsbox relies on a set of converters to convert time series stored as ts, xts, data.frame, data.table, tibble, zoo, tsibble, tibbletime or timeSeries to each other. This vignette describes some background on two particular challenges, the conversion of equispaced points in time to actual dates or times, and the regularization of irregular time sequences.
The classic way of storing time series in R in
"ts" objects. These are simple vectors with an attribute that describes the beginning of the series, the (redundant) end, and the frequency. Thus, a monthly series, e.g.,
AirPassengers, is defined as a numeric vector that starts in 1949, with frequency 1. Thus, months are thought of as equispaced periods with a length of exactly 1/12 of a year.
For most time series, this is not what is meant. The second period of
AirPassengers, February 1949, is actually shorter than the first one, but this is not reflected in the
"ts" object. When converting to classes with actual time stamps, tsbox tries to correct it by using heuristic, rather than exact time conversion if possible.
Whenever possible, tsbox relies on heuristic time conversion. When a monthly
"ts" time series, e.g.,
AirPassengers, is converted to a data frame, each time stamp (of class
"Date") indicates the first day of the month. Heuristic conversion is done for the following frequencies:
For example, converting
AirPassengers to a data frame returns:
Heuristic conversion works both ways, so we can get back to the original
For non standard frequencies, e.g. 260, of
EuStockMarkets, tsbox uses exact time conversion. The year is divided into 260 equispaced units, each somewhat longer than a day. The time stamp of a period will be an exact point in time (of class
Higher frequencies, such as days, hours, minutes or seconds, are naturally equispaced, and exact time conversion is used as well.
Exact time conversion is generally reversible:
However, for high frequencies, rounding errors can lead to unavoidable small differences when going from data frame to
"ts" and back.
Conversion does not work reliably if the frequency higher than one second. For these ultra high frequencies, tsbox is not tested and may not work as expected.
In data frames or
"xts" objects, missing values are generally omitted. These omitted missing values are called implicit, as opposite to explicit
NA values. The function
ts_regular allows the user to regularize a series, by making implicit missing values explicit.
ts_regular analyzes the differences in the time stamp for known frequencies. If it detects any, it builds a regular sequence based on the highest known frequency, and tries to match the time stamps to the regular series. The result is a data frame or
"xts" object with explicit missing values. Regularization is automatically done when an object is converted to a
For example, the following time series contains an implicit
NA value in February 1974:
ts_regular can be used to turn it into a explicit
head(ts_regular(df)) #> time value #> 1 1974-01-01 901 #> 2 1974-02-01 NA #> 3 1974-03-01 827 #> 4 1974-04-01 677 #> 5 1974-05-01 522 #> 6 1974-06-01 406
Regularization can be done for all frequencies that are suited for heuristic conversion, as listed above. In addition to these frequencies, the following higher frequencies are detected and regularized as well: