Skip to contents

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.

Usage

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, ...)

Arguments

recipe

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.

period

The number of periods to include in the local rolling window. This is effectively the "window size".

.f

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.

align

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

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.

prefix

A prefix for generated column names, default to "slidify_".

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 variable names that will be populated (eventually) by the terms argument.

f_name

A character string for the function being applied. This field is a placeholder and will be populated during the tidy() step.

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_slidify_augment object.

Value

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).

Details

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.

See also

Examples

library(tidymodels)
#> ── Attaching packages ────────────────────────────────────── tidymodels 1.0.0 ──
#>  broom        1.0.1      tune         1.0.1
#>  dials        1.1.0      workflows    1.1.2
#>  infer        1.0.3      workflowsets 1.0.0
#>  modeldata    1.0.1      yardstick    1.1.0
#>  parsnip      1.0.3     
#> ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
#>  scales::discard() masks purrr::discard()
#>  dplyr::filter()   masks stats::filter()
#>  xts::first()      masks dplyr::first()
#>  recipes::fixed()  masks stringr::fixed()
#>  dplyr::lag()      masks stats::lag()
#>  xts::last()       masks dplyr::last()
#>  dials::momentum() masks TTR::momentum()
#>  yardstick::spec() masks readr::spec()
#>  recipes::step()   masks stats::step()
#>  Dig deeper into tidy modeling with R at https://www.tmwr.org
library(tidyverse)
library(timetk)

m750 <- m4_monthly %>%
    filter(id == "M750") %>%
    mutate(value_2 = value / 2)

m750_splits <- time_series_split(m750, assess = "2 years", cumulative = TRUE)
#> Using date_var: date

# Make a recipe
recipe_spec <- recipe(value ~ date + value_2, 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()
#> # A tibble: 282 × 9
#>    date       value_2 value slidify_6_…¹ slidi…² slidi…³ slidi…⁴ slidi…⁵ slidi…⁶
#>    <date>       <dbl> <dbl>        <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#>  1 1990-01-01    3185  6370          NA      NA      NA      NA       NA      NA
#>  2 1990-02-01    3215  6430          NA      NA      NA      NA       NA      NA
#>  3 1990-03-01    3260  6520        6535    3268.     NA      NA       NA      NA
#>  4 1990-04-01    3290  6580        6473.   3237.     NA      NA       NA      NA
#>  5 1990-05-01    3310  6620        6310    3155      NA      NA       NA      NA
#>  6 1990-06-01    3345  6690        6303.   3152.   6481.   3240.      NA      NA
#>  7 1990-07-01    3000  6000        6343.   3172.   6527.   3263.      NA      NA
#>  8 1990-08-01    2725  5450        6383.   3192.   6567.   3283.      NA      NA
#>  9 1990-09-01    3240  6480        6427.   3213.   6603.   3302.      NA      NA
#> 10 1990-10-01    3410  6820        6580    3290    6638.   3319.      NA      NA
#> # … with 272 more rows, and abbreviated variable names ¹​slidify_6_value,
#> #   ²​slidify_6_value_2, ³​slidify_12_value, ⁴​slidify_12_value_2,
#> #   ⁵​slidify_24_value, ⁶​slidify_24_value_2

bake(prep(recipe_spec), testing(m750_splits))
#> # A tibble: 24 × 9
#>    date       value_2 value slidify_6_…¹ slidi…² slidi…³ slidi…⁴ slidi…⁵ slidi…⁶
#>    <date>       <dbl> <dbl>        <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#>  1 2013-07-01    4515  9030          NA      NA      NA      NA       NA      NA
#>  2 2013-08-01    4810  9620          NA      NA      NA      NA       NA      NA
#>  3 2013-09-01    5025 10050       10107.   5053.     NA      NA       NA      NA
#>  4 2013-10-01    5305 10610       10390    5195      NA      NA       NA      NA
#>  5 2013-11-01    5380 10760       10563.   5282.     NA      NA       NA      NA
#>  6 2013-12-01    5285 10570       10705    5352.  10472.   5236.      NA      NA
#>  7 2014-01-01    5365 10730       10773.   5387.  10498.   5249.      NA      NA
#>  8 2014-02-01    5330 10660       10803.   5402.  10521.   5260.      NA      NA
#>  9 2014-03-01    5450 10900       10837.   5418.  10547.   5273.      NA      NA
#> 10 2014-04-01    5510 11020       10605    5302.  10575    5288.      NA      NA
#> # … with 14 more rows, and abbreviated variable names ¹​slidify_6_value,
#> #   ²​slidify_6_value_2, ³​slidify_12_value, ⁴​slidify_12_value_2,
#> #   ⁵​slidify_24_value, ⁶​slidify_24_value_2