Holiday Feature (Signature) Generator
Source:R/recipes-step_holiday_signature.R
step_holiday_signature.Rd
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.
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
orPOSIXct
. Seerecipes::selections()
for more details. For thetidy
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.
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).
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.
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:
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)
#> # A tibble: 3 × 4
#> terms param value id
#> <chr> <chr> <chr> <chr>
#> 1 index holiday_pattern ^US_ holiday_signature_on1VO
#> 2 index locale_set US holiday_signature_on1VO
#> 3 index exchange_set NYSE holiday_signature_on1VO
# Prep the recipe
rec_holiday_prep <- prep(rec_holiday)
# Now prep'ed - returns new features that will be created
rec_holiday_prep %>% tidy(1)
#> # A tibble: 20 × 3
#> terms value id
#> <fct> <fct> <chr>
#> 1 index exch_NYSE holiday_signature_on1VO
#> 2 index locale_US holiday_signature_on1VO
#> 3 index US_NewYearsDay holiday_signature_on1VO
#> 4 index US_MLKingsBirthday holiday_signature_on1VO
#> 5 index US_InaugurationDay holiday_signature_on1VO
#> 6 index US_LincolnsBirthday holiday_signature_on1VO
#> 7 index US_PresidentsDay holiday_signature_on1VO
#> 8 index US_WashingtonsBirthday holiday_signature_on1VO
#> 9 index US_CPulaskisBirthday holiday_signature_on1VO
#> 10 index US_GoodFriday holiday_signature_on1VO
#> 11 index US_DecorationMemorialDay holiday_signature_on1VO
#> 12 index US_MemorialDay holiday_signature_on1VO
#> 13 index US_IndependenceDay holiday_signature_on1VO
#> 14 index US_LaborDay holiday_signature_on1VO
#> 15 index US_ColumbusDay holiday_signature_on1VO
#> 16 index US_ElectionDay holiday_signature_on1VO
#> 17 index US_VeteransDay holiday_signature_on1VO
#> 18 index US_ThanksgivingDay holiday_signature_on1VO
#> 19 index US_ChristmasDay holiday_signature_on1VO
#> 20 index US_JuneteenthNationalIndependenceDay holiday_signature_on1VO
# Apply the recipe to add new holiday features!
bake(rec_holiday_prep, dates_in_2017_tbl)
#> # A tibble: 365 × 21
#> index index_exch_NYSE index_locale_US index_US_NewYearsDay
#> <date> <dbl> <dbl> <dbl>
#> 1 2017-01-01 0 1 1
#> 2 2017-01-02 1 0 0
#> 3 2017-01-03 0 0 0
#> 4 2017-01-04 0 0 0
#> 5 2017-01-05 0 0 0
#> 6 2017-01-06 0 0 0
#> 7 2017-01-07 0 0 0
#> 8 2017-01-08 0 0 0
#> 9 2017-01-09 0 0 0
#> 10 2017-01-10 0 0 0
#> # ℹ 355 more rows
#> # ℹ 17 more variables: index_US_MLKingsBirthday <dbl>,
#> # index_US_InaugurationDay <dbl>, index_US_LincolnsBirthday <dbl>,
#> # index_US_PresidentsDay <dbl>, index_US_WashingtonsBirthday <dbl>,
#> # index_US_CPulaskisBirthday <dbl>, index_US_GoodFriday <dbl>,
#> # index_US_MemorialDay <dbl>, index_US_DecorationMemorialDay <dbl>,
#> # index_US_IndependenceDay <dbl>, index_US_LaborDay <dbl>, …