Intelligent Date & Time Sequences

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()

Creating and modifying date sequences is critical to machine learning projects. We discuss:

Prerequisites

Before we get started, load the following packages.

library(dplyr)
library(timetk)

Making a Time Series Sequence

tk_make_timeseries() improves on the seq.Date() and seq.POSIXt() functions by simplifying into 1 function. Intelligently handles character dates and logical assumptions based on user inputs.

By Day

# Selects by day automatically
tk_make_timeseries("2011", length_out = "7 days", include_endpoints = FALSE)

By 2 Seconds

# Guesses by second
tk_make_timeseries("2016", by = "2 sec", length_out = "6 seconds")

Length Out = 1 year 6 months

tk_make_timeseries("2012-07", 
                   by = "1 month",
                   length_out = "1 year 6 months", 
                   include_endpoints = FALSE)

Go In Reverse

tk_make_timeseries(end_date = "2012-07-01", 
                   by = "1 month",
                   length_out = "1 year 6 months")

Future Time Series Sequence

A common operation is to make a future time series sequence that mimics an existing. This is what tk_make_future_timeseries() is for.

Suppose we have an existing time index.

idx <- tk_make_timeseries("2012", by = "3 months", 
                          length_out = "2 years", 
                          include_endpoints = FALSE)
idx

Make a Future Time Series from an Existing

We can create a future time sequence from the existing sequence using tk_make_future_timeseries().

tk_make_future_timeseries(idx, length_out = "2 years")

Weekends & Holidays

Make weekday sequence removing holidays

idx <- tk_make_weekday_sequence("2012",
                                remove_weekends = TRUE, 
                                remove_holidays = TRUE, calendar = "NYSE")

tk_get_timeseries_summary(idx)

Which holidays were removed?

tk_make_holiday_sequence("2012", calendar = "NYSE")

Make future index removing holidays

holidays <- tk_make_holiday_sequence(
    start_date = "2013-01-01",
    end_date   = "2013-12-31",
    calendar   = "NYSE")

idx_future <- idx %>%
   tk_make_future_timeseries(length_out       = "1 year",
                             inspect_weekdays = TRUE,
                             skip_values      = holidays)

tk_get_timeseries_summary(idx_future)

Learning More

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:

Unlock the High-Performance Time Series Forecasting Course



Try the timetk package in your browser

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

timetk documentation built on Nov. 2, 2023, 6:18 p.m.