Skip to contents

step_fourier creates a a specification of a recipe step that will convert a Date or Date-time column into a Fourier series

Usage

step_fourier(
  recipe,
  ...,
  period,
  K,
  role = "predictor",
  trained = FALSE,
  columns = NULL,
  scale_factor = NULL,
  skip = FALSE,
  id = rand_id("fourier")
)

# S3 method for step_fourier
tidy(x, ...)

Arguments

recipe

A recipe object. The step will be added to the sequence of operations for this recipe.

...

A single column with class Date or POSIXct. See recipes::selections() for more details. For the tidy method, these are not currently used.

period

The numeric period for the oscillation frequency. See details for examples of period specification.

K

The number of orders to include for each sine/cosine fourier series. More orders increase the number of fourier terms and therefore the variance of the fitted model at the expense of bias. See details for examples of K specification.

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.

scale_factor

A factor for scaling the numeric index extracted from the date or date-time feature. This 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_fourier object.

Value

For step_fourier, 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_fourier does not remove the original date variables. recipes::step_rm() can be used for this purpose.

Period Specification

The period argument is used to generate the distance between peaks in the fourier sequence. The key is to line up the peaks with unique seasonalities in the data.

For Daily Data, typical period specifications are:

  • Yearly frequency is 365

  • Quarterly frequency is 365 / 4 = 91.25

  • Monthly frequency is 365 / 12 = 30.42

K Specification

The K argument specifies the maximum number of orders of Fourier terms. Examples:

  • Specifying period = 365 and K = 1 will return a cos365_K1 and sin365_K1 fourier series

  • Specifying period = 365 and K = 2 will return a cos365_K1, cos365_K2, sin365_K1 and sin365_K2 sequence, which tends to increase the models ability to fit vs the K = 1 specification (at the expense of possibly overfitting).

Multiple values of period and K

It's possible to specify multiple values of period in a single step such as step_fourier(period = c(91.25, 365), K = 2. This returns 8 Fouriers series:

  • cos91.25_K1, sin91.25_K1, cos91.25_K2, sin91.25_K2

  • cos365_K1, sin365_K1, cos365_K2, sin365_K2

See also

Time Series Analysis:

Main Recipe Functions:

Examples

library(recipes)
library(tidyverse)
library(tidyquant)
library(timetk)

FB_tbl <- FANG %>%
    filter(symbol == "FB") %>%
    select(symbol, date, adjusted)

# Create a recipe object with a timeseries signature step
# - 252 Trade days per year
# - period = c(252/4, 252): Adds quarterly and yearly fourier series
# - K = 2: Adds 1st and 2nd fourier orders

rec_obj <- recipe(adjusted ~ ., data = FB_tbl) %>%
    step_fourier(date, period = c(252/4, 252), K = 2)

# View the recipe object
rec_obj
#> Recipe
#> 
#> Inputs:
#> 
#>       role #variables
#>    outcome          1
#>  predictor          2
#> 
#> Operations:
#> 
#> Fourier series features from date

# Prepare the recipe object
prep(rec_obj)
#> Recipe
#> 
#> Inputs:
#> 
#>       role #variables
#>    outcome          1
#>  predictor          2
#> 
#> Training data contained 1008 data points and no missing data.
#> 
#> Operations:
#> 
#> Fourier series features from date [trained]

# Bake the recipe object - Adds the Fourier Series
bake(prep(rec_obj), FB_tbl)
#> # A tibble: 1,008 × 11
#>    symbol date       adjusted date_sin…¹ date_…² date_…³ date_…⁴ date_…⁵ date_…⁶
#>    <fct>  <date>        <dbl>      <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#>  1 FB     2013-01-02     28       0.912   -0.411 -0.750   -0.662   0.878  -0.478
#>  2 FB     2013-01-03     27.8     0.866   -0.500 -0.866   -0.500   0.866  -0.500
#>  3 FB     2013-01-04     28.8     0.812   -0.584 -0.948   -0.318   0.853  -0.521
#>  4 FB     2013-01-07     29.4     0.604   -0.797 -0.963    0.271   0.812  -0.584
#>  5 FB     2013-01-08     29.1     0.521   -0.853 -0.890    0.456   0.797  -0.604
#>  6 FB     2013-01-09     30.6     0.434   -0.901 -0.782    0.623   0.782  -0.623
#>  7 FB     2013-01-10     31.3     0.342   -0.940 -0.643    0.766   0.766  -0.643
#>  8 FB     2013-01-11     31.7     0.247   -0.969 -0.478    0.878   0.750  -0.662
#>  9 FB     2013-01-14     31.0    -0.0498  -0.999  0.0996   0.995   0.698  -0.716
#> 10 FB     2013-01-15     30.1    -0.149   -0.989  0.295    0.956   0.680  -0.733
#> # … with 998 more rows, 2 more variables: date_sin252_K2 <dbl>,
#> #   date_cos252_K2 <dbl>, and abbreviated variable names ¹​date_sin63_K1,
#> #   ²​date_cos63_K1, ³​date_sin63_K2, ⁴​date_cos63_K2, ⁵​date_sin252_K1,
#> #   ⁶​date_cos252_K1
#> # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names

# 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      fourier TRUE    FALSE fourier_e5rOX
tidy(prep(rec_obj), number = 1)
#> # A tibble: 8 × 5
#>   terms          type      K period id           
#>   <chr>          <chr> <int>  <dbl> <chr>        
#> 1 date_sin63_K1  sin       1     63 fourier_e5rOX
#> 2 date_cos63_K1  cos       1     63 fourier_e5rOX
#> 3 date_sin63_K2  sin       2     63 fourier_e5rOX
#> 4 date_cos63_K2  cos       2     63 fourier_e5rOX
#> 5 date_sin252_K1 sin       1    252 fourier_e5rOX
#> 6 date_cos252_K1 cos       1    252 fourier_e5rOX
#> 7 date_sin252_K2 sin       2    252 fourier_e5rOX
#> 8 date_cos252_K2 cos       2    252 fourier_e5rOX