step_holiday_signature: Holiday Feature (Signature) Generator

View source: R/recipes-step_holiday_signature.R

step_holiday_signatureR Documentation

Holiday Feature (Signature) Generator

Description

step_holiday_signature creates a a specification of a recipe step that will convert date or date-time data into many holiday features that can aid in machine learning with time-series data. By default, many features are returned for different holidays, locales, and stock exchanges.

Usage

step_holiday_signature(
  recipe,
  ...,
  holiday_pattern = ".",
  locale_set = "all",
  exchange_set = "all",
  role = "predictor",
  trained = FALSE,
  columns = NULL,
  features = NULL,
  skip = FALSE,
  id = rand_id("holiday_signature")
)

## S3 method for class 'step_holiday_signature'
tidy(x, ...)

Arguments

recipe

A recipe object. The step will be added to the sequence of operations for this recipe.

...

One or more selector functions to choose which variables that will be used to create the new variables. The selected variables should have class Date or POSIXct. See recipes::selections() for more details. For the tidy method, these are not currently used.

holiday_pattern

A regular expression pattern to search the "Holiday Set".

locale_set

Return binary holidays based on locale. One of: "all", "none", "World", "US", "CA", "GB", "FR", "IT", "JP", "CH", "DE".

exchange_set

Return binary holidays based on Stock Exchange Calendars. One of: "all", "none", "NYSE", "LONDON", "NERC", "TSX", "ZURICH".

role

For model terms created by this step, what analysis role should they be assigned?. By default, the function assumes that the new variable columns created by the original variables will be used as predictors in a model.

trained

A logical to indicate if the quantities for preprocessing have been estimated.

columns

A character string of variables that will be used as inputs. This field is a placeholder and will be populated once recipes::prep() is used.

features

A character string of features that will be generated. This field is a placeholder and will be populated once recipes::prep() is used.

skip

A logical. Should the step be skipped when the recipe is baked by bake.recipe()? While all operations are baked when prep.recipe() is run, some operations may not be able to be conducted on new data (e.g. processing the outcome variable(s)). Care should be taken when using skip = TRUE as it may affect the computations for subsequent operations.

id

A character string that is unique to this step to identify it.

x

A step_holiday_signature object.

Details

Use Holiday Pattern and Feature Sets to Pare Down Features By default, you're going to get A LOT of Features. This is a good thing because many machine learning algorithms have regularization built in. But, in many cases you will still want to reduce the number of unnecessary features. Here's how:

  • Holiday Pattern: This is a Regular Expression pattern that can be used to filter. Try holiday_pattern = "(US_Christ)|(US_Thanks)" to return just Christmas and Thanksgiving features.

  • Locale Sets: This is a logical as to whether or not the locale has a holiday. For locales outside of US you may want to combine multiple locales. For example, locale_set = c("World", "GB") returns both World Holidays and Great Britain.

  • Exchange Sets: This is a logical as to whether or not the Business is off due to a holiday. Different Stock Exchanges are used as a proxy for business holiday calendars. For example, exchange_set = "NYSE" returns business holidays for New York Stock Exchange.

Removing Unnecessary Features By default, many features are created automatically. Unnecessary features can be removed using recipes::step_rm() and recipes::selections() for more details.

Value

For step_holiday_signature, an updated version of recipe with the new step added to the sequence of existing steps (if any). For the tidy method, a tibble with columns terms (the selectors or variables selected), value (the feature names).

See Also

Time Series Analysis:

  • Engineered Features: step_timeseries_signature(), step_holiday_signature(), step_fourier()

  • Diffs & Lags step_diff(), recipes::step_lag()

  • Smoothing: step_slidify(), step_smooth()

  • Variance Reduction: step_box_cox()

  • Imputation: step_ts_impute(), step_ts_clean()

  • Padding: step_ts_pad()

Main Recipe Functions:

  • recipes::recipe()

  • recipes::prep()

  • recipes::bake()

Examples

library(recipes)
library(dplyr)

# Sample Data
dates_in_2017_tbl <- tibble::tibble(
    index = tk_make_timeseries("2017-01-01", "2017-12-31", by = "day")
)

# Add US holidays and Non-Working Days due to Holidays
# - Physical Holidays are added with holiday pattern (individual) and locale_set
rec_holiday <- recipe(~ ., dates_in_2017_tbl) %>%
    step_holiday_signature(index,
                           holiday_pattern = "^US_",
                           locale_set      = "US",
                           exchange_set    = "NYSE")

# Not yet prep'ed - just returns parameters selected
rec_holiday %>% tidy(1)

# Prep the recipe
rec_holiday_prep <- prep(rec_holiday)

# Now prep'ed - returns new features that will be created
rec_holiday_prep %>% tidy(1)

# Apply the recipe to add new holiday features!
bake(rec_holiday_prep, dates_in_2017_tbl)





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