Skip to contents

step_timeseries_signature creates a a specification of a recipe step that will convert date or date-time data into many features that can aid in machine learning with time-series data

Usage

step_timeseries_signature(
  recipe,
  ...,
  role = "predictor",
  trained = FALSE,
  columns = NULL,
  skip = FALSE,
  id = rand_id("timeseries_signature")
)

# S3 method for step_timeseries_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.

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.

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

Value

For step_timeseries_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

Date Variable Unlike other steps, step_timeseries_signature does not remove the original date variables. recipes::step_rm() can be used for this purpose.

Scaling index.num The index.num feature created has a large magnitude (number of seconds since 1970-01-01). It's a good idea to scale and center this feature (e.g. use recipes::step_normalize()).

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

See also

Time Series Analysis:

Main Recipe Functions:

Examples

library(recipes)
library(dplyr)

FB_tbl <- FANG %>% dplyr::filter(symbol == "FB")

# Create a recipe object with a timeseries signature step
rec_obj <- recipe(adjusted ~ ., data = FB_tbl) %>%
    step_timeseries_signature(date)

# View the recipe object
rec_obj
#> 
#> ── Recipe ──────────────────────────────────────────────────────────────────────
#> 
#> ── Inputs 
#> Number of variables by role
#> outcome:   1
#> predictor: 7
#> 
#> ── Operations 
#>  Timeseries signature features from: date

# Prepare the recipe object
prep(rec_obj)
#> 
#> ── Recipe ──────────────────────────────────────────────────────────────────────
#> 
#> ── Inputs 
#> Number of variables by role
#> outcome:   1
#> predictor: 7
#> 
#> ── Training information 
#> Training data contained 1008 data points and no incomplete rows.
#> 
#> ── Operations 
#>  Timeseries signature features from: date | Trained

# Bake the recipe object - Adds the Time Series Signature
bake(prep(rec_obj), FB_tbl)
#> # A tibble: 1,008 × 35
#>    symbol date        open  high   low close    volume adjusted date_index.num
#>    <fct>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>          <dbl>
#>  1 FB     2013-01-02  27.4  28.2  27.4  28    69846400     28       1357084800
#>  2 FB     2013-01-03  27.9  28.5  27.6  27.8  63140600     27.8     1357171200
#>  3 FB     2013-01-04  28.0  28.9  27.8  28.8  72715400     28.8     1357257600
#>  4 FB     2013-01-07  28.7  29.8  28.6  29.4  83781800     29.4     1357516800
#>  5 FB     2013-01-08  29.5  29.6  28.9  29.1  45871300     29.1     1357603200
#>  6 FB     2013-01-09  29.7  30.6  29.5  30.6 104787700     30.6     1357689600
#>  7 FB     2013-01-10  30.6  31.5  30.3  31.3  95316400     31.3     1357776000
#>  8 FB     2013-01-11  31.3  32.0  31.1  31.7  89598000     31.7     1357862400
#>  9 FB     2013-01-14  32.1  32.2  30.6  31.0  98892800     31.0     1358121600
#> 10 FB     2013-01-15  30.6  31.7  29.9  30.1 173242600     30.1     1358208000
#> # ℹ 998 more rows
#> # ℹ 26 more variables: date_year <int>, date_year.iso <int>, date_half <int>,
#> #   date_quarter <int>, date_month <int>, date_month.xts <int>,
#> #   date_month.lbl <ord>, date_day <int>, date_hour <int>, date_minute <int>,
#> #   date_second <int>, date_hour12 <int>, date_am.pm <int>, date_wday <int>,
#> #   date_wday.xts <int>, date_wday.lbl <ord>, date_mday <int>, date_qday <int>,
#> #   date_yday <int>, date_mweek <int>, date_week <int>, date_week.iso <int>, …

# Tidy shows which features have been added during the 1st step
#  in this case, step 1 is the step_timeseries_signature step
tidy(rec_obj)
#> # A tibble: 1 × 6
#>   number operation type                 trained skip  id                        
#>    <int> <chr>     <chr>                <lgl>   <lgl> <chr>                     
#> 1      1 step      timeseries_signature FALSE   FALSE timeseries_signature_qcwig
tidy(rec_obj, number = 1)
#> # A tibble: 27 × 3
#>    terms value     id                        
#>    <fct> <fct>     <chr>                     
#>  1 date  index.num timeseries_signature_qcwig
#>  2 date  year      timeseries_signature_qcwig
#>  3 date  year.iso  timeseries_signature_qcwig
#>  4 date  half      timeseries_signature_qcwig
#>  5 date  quarter   timeseries_signature_qcwig
#>  6 date  month     timeseries_signature_qcwig
#>  7 date  month.xts timeseries_signature_qcwig
#>  8 date  month.lbl timeseries_signature_qcwig
#>  9 date  day       timeseries_signature_qcwig
#> 10 date  hour      timeseries_signature_qcwig
#> # ℹ 17 more rows