---
title: "Intelligent Date & Time Sequences"
author: "Matt Dancho"
date: "`r Sys.Date()`"
output: 
  rmarkdown::html_vignette:
    toc: true
    toc_depth: 2
vignette: >
  %\VignetteIndexEntry{Intelligent Date & Time Sequences}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, echo = FALSE, message = FALSE, warning = FALSE}
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:

- Making a Time Series Sequence: `tk_make_timeseries()`
- Making a Future Sequence: `tk_make_future_timeseries()`
- Holiday & Weekday/Weekend Sequences


# Prerequisites

Before we get started, load the following packages.

```{r, message=FALSE}
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__

- Can use `by = "day"` or leave blank. 
- `include_endpoints = FALSE` removes the last value so your series is only 7 observations. 

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

__By 2 Seconds__

- Can use `by = "2 sec"` to adjust the interval width.
- `include_endpoints = TRUE` keeps the last value the series ends on the 6th second. 

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

__Length Out = 1 year 6 months__

- `length_out = "1 year 6 months"` - Can include complex expressions like "1 year 4 months 6 days".

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

__Go In Reverse__

- To go in reverse, just use `end_date` as where you want the series to end. 

```{r}
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. 

```{r}
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()`. 

```{r}
tk_make_future_timeseries(idx, length_out = "2 years")
```


# Weekends & Holidays

__Make weekday sequence removing holidays__

- Result is 252 days.

```{r}
idx <- tk_make_weekday_sequence("2012",
                                remove_weekends = TRUE, 
                                remove_holidays = TRUE, calendar = "NYSE")

tk_get_timeseries_summary(idx)
```

__Which holidays were removed?__

- NYSE Trading holidays which are days most businesses observe 

```{r}
tk_make_holiday_sequence("2012", calendar = "NYSE")
```


__Make future index removing holidays__

```{r}
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

<p>
	<iframe width="100%" height="450" src="https://www.youtube.com/embed/elQb4VzRINg" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" style="box-shadow: 0 0 5px 2px rgba(0, 0, 0, .5);"><span id="selection-marker-1" class="redactor-selection-marker"></span><span id="selection-marker-1" class="redactor-selection-marker"></span><span id="selection-marker-1" class="redactor-selection-marker"></span><span id="selection-marker-1" class="redactor-selection-marker"></span>
	</iframe>
</p>

_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](https://university.business-science.io/p/ds4b-203-r-high-performance-time-series-forecasting). 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.

<p class="text-center" style="font-size:30px;">
<a href="https://university.business-science.io/p/ds4b-203-r-high-performance-time-series-forecasting">Unlock the High-Performance Time Series Forecasting Course</a>
</p>
