step_fourier creates a a specification of a recipe
step that will convert a Date or Date-time column into a Fourier
step_fourier( recipe, ..., period, K, role = "predictor", trained = FALSE, columns = NULL, scale_factor = NULL, skip = FALSE, id = rand_id("fourier") ) ## S3 method for class 'step_fourier' tidy(x, ...)
A recipe object. The step will be added to the sequence of operations for this recipe.
A single column with class
The numeric period for the oscillation frequency.
See details for examples of
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
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
A factor for scaling the numeric index extracted
from the date or date-time feature. This is a placeholder and will be populated
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.
Unlike other steps,
step_fourier does not
remove the original date variables.
recipes::step_rm() can be
used for this purpose.
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 argument specifies the maximum number of orders of Fourier terms.
period = 365 and
K = 1 will return a
sin365_K1 fourier series
period = 365 and
K = 2 will return a
sequence, which tends to increase the models ability to fit vs the
K = 1 specification
(at the expense of possibly overfitting).
Multiple values of
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:
step_fourier, 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
Time Series Analysis:
Diffs & Lags
Main Recipe Functions:
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 # Prepare the recipe object prep(rec_obj) # Bake the recipe object - Adds the Fourier Series bake(prep(rec_obj), FB_tbl) # 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)) tidy(prep(rec_obj), number = 1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.