knitr::opts_chunk$set( # message = FALSE, # warning = FALSE, fig.width = 8, fig.height = 4.5, fig.align = 'center', out.width='95%', dpi = 100 ) # devtools::load_all() # Travis CI fails on load_all()

This vignette covers making and working with **Calendar Features**, which are derived from a time series index, or the sequence of date/datetime stamps that accompany time series data.

The **time series index** consists of a collection of time-based values that define *when* each observation occurred, is the most important part of a time series object.

The index gives the user a lot of information in a simple timestamp. Consider the datetime ** "2016-01-01 00:00:00"**.

From this timestamp, we can decompose the date and time information to get the **signature**, which consists of the year, quarter, month, day, day of year, day of month, hour, minute, and second of the occurrence of a single observation. Further, the difference between two or more observations is the **frequency** from which we can obtain even more information such as the periodicity of the data and whether or not these observations are on a regular interval. This information is critical as it provides the basis for performance over time in finance, decay rates in biology, growth rates in economics, and so on.

In this vignette the user will be exposed to:

- Time Series Index
- Time Series Signature
- Time Series Summary

Before we get started, load the following packages.

library(dplyr) library(timetk)

We'll use the Facebook stock prices from the `FANG`

data set. These are the historical stock prices (open, high, low, close, volume, and adjusted) for the "FB" stock from 2013 through 2016.

data("FANG") FB_tbl <- FANG %>% dplyr::filter(symbol == "FB") FB_tbl

To simplify the tutorial, we will select only the "date" and "volume" columns. For the `FB_vol_date`

data frame, we can see from the "date" column that the observations are *daily* beginning on the second day of 2013.

FB_vol_date <- FB_tbl %>% select(date, volume) FB_vol_date

Before we can analyze an index, we need to extract it from the object. The function `tk_index()`

extracts the index from any time series object including data frame (or `tbl`

), `xts`

, `zoo`

, etc. The index is always returned in the native date, datetime, yearmon, or yearqtr format. Note that the index must be in one of these time-based classes for extraction to work:

- datetimes: Must inherit
`POSIXt`

- dates: Must inherit
`Date`

- yearmon: Must inherit
`yearmon`

from the`zoo`

package - yearqtr: Must inherit
`yearqtr`

from the`zoo`

package

Extract the index using `tk_index()`

. The structure is shown to see the output format, which is a vector of dates.

# idx_date idx_date <- tk_index(FB_vol_date) str(idx_date)

The index can be decomposed into a *signature*. The time series signature is a unique set of properties of the time series values that describe the time series.

The function `tk_get_timeseries_signature()`

can be used to convert the index to a tibble containing the following values (columns):

**index**: The index value that was decomposed**index.num**: The numeric value of the index in seconds. The base is "1970-01-01 00:00:00" (Execute`"1970-01-01 00:00:00" %>% ymd_hms() %>% as.numeric()`

to see the value returned is zero). Every time series value after this date can be converted to a numeric value in seconds.**diff**: The difference in seconds from the previous numeric index value.**year**: The year component of the index.**year.iso**: The ISO year number of the year (Monday start).**half**: The half component of the index.**quarter**: The quarter component of the index.**month**: The month component of the index with base 1.**month.xts**: The month component of the index with base 0, which is what`xts`

implements.**month.lbl**: The month label as an ordered factor begining with January and ending with December.**day**: The day component of the index.**hour**: The hour component of the index.**minute**: The minute component of the index.**second**: The second component of the index.**hour12**: The hour component on a 12 hour scale.**am.pm**: Morning (AM) = 1, Afternoon (PM) = 2.**wday**: The day of the week with base 1. Sunday = 1 and Saturday = 7.**wday.xts**: The day of the week with base 0, which is what`xts`

implements. Sunday = 0 and Saturday = 6.**wday.lbl**: The day of the week label as an ordered factor begining with Sunday and ending with Saturday.**mday**: The day of the month.**qday**: The day of the quarter.**yday**: The day of the year.**mweek**: The week of the month.**week**: The week number of the year (Sunday start).**week.iso**: The ISO week number of the year (Monday start).**week2**: The modulus for bi-weekly frequency.**week3**: The modulus for tri-weekly frequency.**week4**: The modulus for quad-weekly frequency.**mday7**: The integer division of day of the month by seven, which returns the first, second, third, ... instance the day has appeared in the month. Values begin at 1. For example, the first Saturday in the month has mday7 = 1. The second has mday7 = 2.

# idx_date signature tk_get_timeseries_signature(idx_date)

It's usually important to keep the index signature with the values (e.g. volume in our example). We can use an expedited approach with `tk_augment_timeseries_signature()`

, which adds the signature to the end of the time series object.

# Augmenting a data frame FB_vol_date_signature <- FB_vol_date %>% tk_augment_timeseries_signature(.date_var = date) FB_vol_date_signature

Modeling is now much easier. As an example, we can use linear regression model using the `lm()`

function with the month and year as a predictor of volume.

# Example Benefit 2: Modeling is easier fit <- lm(volume ~ year + month.lbl, data = FB_vol_date_signature) summary(fit)

The next index analysis tool is the summary metrics, which can be retrieved using the `tk_get_timeseries_summary()`

function. The summary reports the following attributes as a single-row tibble.

*General Summary*:

The first six columns are general summary information.

**n.obs**: The total number of observations**start**: The start in the appropriate time class**end**: The end in the appropriate time class**units**: A label that describes the unit of the index value that is independent of frequency (i.e. a date class will always be "days" whereas a datetime class will always be "seconds"). Values can be days, hours, mins, secs.**scale**: A label that describes the the median difference (frequency) between observations. Values can be quarter, month, day, hour, minute, second.**tzone**: The timezone of the index.

# idx_date: First six columns, general summary tk_get_timeseries_summary(idx_date)[,1:6]

*Differences Summary*:

The next group of values are the **differences summary** (i.e. summary of frequency). All values are in seconds:

**diff.minimum**: The minimum difference between index values.**diff.q1**: The first quartile of the index differences.**diff.median**: The median difference between index values (i.e. most common frequency).**diff.mean**: The average difference between index values.**diff.q3**: The third quartile of the index differences.**diff.maximum**: The maximum difference between index values.

# idx_date: Last six columns, difference summary tk_get_timeseries_summary(idx_date)[,7:12]

The **differences** provide information about the *regularity of the frequency*. Generally speaking if all difference values are equal, the index is regular. However, scales beyond "day" are never theoretically regular since the differences in seconds are not equivalent. However, conceptually monthly, quarterly and yearly data can be thought of as regular if the index contains consecutive months, quarters, or years, respectively. Therefore, the difference attributes are most meaningful for daily and lower time scales because the difference summary always indicates level of regularity.

From the second group (differences summary), we immediately recognize that the mean is different than the median and therefore the index is *irregular* (meaning certain days are missing). Further we can see that the maximum difference is 345,600 seconds, indicating the maximum difference is 4 days (345,600 seconds / 86400 seconds/day).

*My Talk on High-Performance Time Series Forecasting*

Time series is changing. **Businesses now need 10,000+ time series forecasts every day.**

**High-Performance Forecasting Systems will save companies MILLIONS of dollars.** Imagine what will happen to your career if you can provide your organization a "High-Performance Time Series Forecasting System" (HPTSF System).

I teach how to build a HPTFS System in my **High-Performance Time Series Forecasting Course**. If interested in learning Scalable High-Performance Forecasting Strategies then take my course. You will learn:

- Time Series Machine Learning (cutting-edge) with
`Modeltime`

- 30+ Models (Prophet, ARIMA, XGBoost, Random Forest, & many more) - NEW - Deep Learning with
`GluonTS`

(Competition Winners) - Time Series Preprocessing, Noise Reduction, & Anomaly Detection
- Feature engineering using lagged variables & external regressors
- Hyperparameter Tuning
- Time series cross-validation
- Ensembling Multiple Machine Learning & Univariate Modeling Techniques (Competition Winner)
- Scalable Forecasting - Forecast 1000+ time series in parallel
- and more.

Unlock the High-Performance Time Series Forecasting Course

**Any scripts or data that you put into this service are public.**

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.