step_slidify_augment
creates a a specification of a recipe
step that will "augment" (add multiple new columns) that have had a sliding function applied.
step_slidify_augment(
recipe,
...,
period,
.f,
align = c("center", "left", "right"),
partial = FALSE,
prefix = "slidify_",
role = "predictor",
trained = FALSE,
columns = NULL,
f_name = NULL,
skip = FALSE,
id = rand_id("slidify_augment")
)# S3 method for step_slidify_augment
tidy(x, ...)
For step_slidify_augment
, 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).
A recipe object. The step will be added to the sequence of operations for this recipe.
One or more numeric columns to be smoothed.
See recipes::selections()
for more details.
For the tidy
method, these are not currently used.
The number of periods to include in the local rolling window. This is effectively the "window size".
A summary formula in one of the following formats:
mean
with no arguments
function(x) mean(x, na.rm = TRUE)
~ mean(.x, na.rm = TRUE)
, it is converted to a function.
Rolling functions generate period - 1
fewer values than the incoming vector.
Thus, the vector needs to be aligned. Alignment of the vector follows 3 types:
Center: NA
or .partial
values are divided and added to the beginning and
end of the series to "Center" the moving average.
This is common for de-noising operations. See also [smooth_vec()]
for LOESS without NA values.
Left: NA
or .partial
values are added to the end to shift the series to the Left.
Right: NA
or .partial
values are added to the beginning to shif the series to the Right. This is common in
Financial Applications such as moving average cross-overs.
Should the moving window be allowed to return partial (incomplete) windows instead of NA values. Set to FALSE by default, but can be switched to TRUE to remove NA's.
A prefix for generated column names, default to "slidify_".
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 variable names that will
be populated (eventually) by the terms
argument.
A character string for the function being applied.
This field is a placeholder and will be populated during the tidy()
step.
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.
A step_slidify_augment
object.
Alignment
Rolling functions generate period - 1
fewer values than the incoming vector.
Thus, the vector needs to be aligned. Alignment of the vector follows 3 types:
Center: NA
or partial
values are divided and added to the beginning and
end of the series to "Center" the moving average.
This is common for de-noising operations. See also [smooth_vec()]
for LOESS without NA values.
Left: NA
or partial
values are added to the end to shift the series to the Left.
Right: NA
or partial
values are added to the beginning to shif the series to the Right. This is common in
Financial Applications such as moving average cross-overs.
Partial Values
The advantage to using partial
values vs NA
padding is that
the series can be filled (good for time-series de-noising operations).
The downside to partial values is that the partials can become less stable at the regions where incomplete windows are used.
If instability is not desirable for de-noising operations, a suitable alternative
is step_smooth()
, which implements local polynomial regression.
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:
# library(tidymodels)
library(dplyr)
library(recipes)
library(parsnip)
m750 <- m4_monthly %>%
filter(id == "M750") %>%
mutate(value_2 = value / 2)
m750_splits <- time_series_split(m750, assess = "2 years", cumulative = TRUE)
# Make a recipe
recipe_spec <- recipe(value ~ date + value_2, rsample::training(m750_splits)) %>%
step_slidify_augment(
value, value_2,
period = c(6, 12, 24),
.f = ~ mean(.x),
align = "center",
partial = FALSE
)
recipe_spec %>% prep() %>% juice()
bake(prep(recipe_spec), rsample::testing(m750_splits))
Run the code above in your browser using DataLab