Pad: Add rows to fill gaps and go from low to high frequencySource:
step_ts_pad creates a a specification of a recipe
step that will analyze a Date or Date-time column adding rows
at a specified interval.
A recipe object. The step will be added to the sequence of operations for this recipe.
A single column with class
recipes::selections()for more details. For the
tidymethod, these are not currently used.
Either "auto", a time-based frequency like "year", "month", "day", "hour", etc, or a time expression like "5 min", or "7 days". See Details.
Fills in padded values. Default is
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.
A logical to indicate if the quantities for preprocessing have been estimated.
A character string of variables that will be used as inputs. This field is a placeholder and will be populated once
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.
A character string that is unique to this step to identify it.
step_ts_pad, an updated version of recipe with
the new step added to the sequence of existing steps (if any).
tidy method, a tibble with columns
(the selectors or variables selected),
value (the feature
Only one date or date-time variable may be supplied.
step_ts_pad())does not remove the original date variables.
Interval Specification (by)
Padding can be applied in the following ways:
The eight intervals in are: year, quarter, month, week, day, hour, min, and sec.
Intervals like 30 minutes, 1 hours, 14 days are possible.
Imputing Missing Values
pad_value defaults to
NA, which typically requires imputation.
Some common strategies include:
Numeric data: The
step_ts_impute()preprocessing step can be used to impute numeric time series data with or without seasonality
Nominal data: The
step_mode_impute()preprocessing step can be used to replace missing values with the most common value.
Padding & Imputation:
Time Series Analysis:
Main Recipe Functions:
library(recipes) library(dplyr) FB_tbl <- FANG %>% filter(symbol == "FB") %>% select(symbol, date, adjusted) rec_obj <- recipe(adjusted ~ ., data = FB_tbl) %>% step_ts_pad(date, by = "day", pad_value = NA) # View the recipe object rec_obj #> #> ── Recipe ────────────────────────────────────────────────────────────────────── #> #> ── Inputs #> Number of variables by role #> outcome: 1 #> predictor: 2 #> #> ── Operations #> • Padded time series features from: date # Prepare the recipe object prep(rec_obj) #> #> ── Recipe ────────────────────────────────────────────────────────────────────── #> #> ── Inputs #> Number of variables by role #> outcome: 1 #> predictor: 2 #> #> ── Training information #> Training data contained 1008 data points and no incomplete rows. #> #> ── Operations #> • Padded time series features from: date | Trained # Bake the recipe object - Adds the padding bake(prep(rec_obj), FB_tbl) #> # A tibble: 1,459 × 3 #> symbol date adjusted #> <fct> <date> <dbl> #> 1 FB 2013-01-02 28 #> 2 FB 2013-01-03 27.8 #> 3 FB 2013-01-04 28.8 #> 4 NA 2013-01-05 NA #> 5 NA 2013-01-06 NA #> 6 FB 2013-01-07 29.4 #> 7 FB 2013-01-08 29.1 #> 8 FB 2013-01-09 30.6 #> 9 FB 2013-01-10 31.3 #> 10 FB 2013-01-11 31.7 #> # ℹ 1,449 more rows # Tidy shows which features have been added during the 1st step # in this case, step 1 is the step_timeseries_signature step tidy(prep(rec_obj)) #> # A tibble: 1 × 6 #> number operation type trained skip id #> <int> <chr> <chr> <lgl> <lgl> <chr> #> 1 1 step ts_pad TRUE FALSE ts_padding_baQBi tidy(prep(rec_obj), number = 1) #> # A tibble: 1 × 4 #> terms by pad_value id #> <chr> <chr> <lgl> <chr> #> 1 date day NA ts_padding_baQBi