# fourier_vec: Fourier Series In timetk: A Tool Kit for Working with Time Series in R

## Description

`fourier_vec()` calculates a Fourier Series from a date or date-time index.

## Usage

 `1` ```fourier_vec(x, period, K = 1, type = c("sin", "cos")) ```

## Arguments

 `x` A date, POSIXct, yearmon, yearqtr, or numeric sequence (scaled to difference 1 for `period` alignment) to be converted to a fourier series. `period` The number of observations that complete one cycle. `K` The fourier term order. `type` Either "sin" or "cos" for the appropriate type of fourier term.

## Details

Benefits:

This function is `NA` padded by default so it works well with `dplyr::mutate()` operations.

Fourier Series Calculation

The internal calculation is relatively straightforward: `fourier(x) = sin(2 * pi * term * x) or cos(2 * pi * term * x)`, where `term = K / period`.

Period Alignment, period

The `period` alignment with the sequence is an essential part of fourier series calculation.

• Date, Date-Time, and Zoo (yearqtr and yearmon) Sequences - Are scaled to unit difference of 1. This happens internally, so there's nothing you need to do or to worry about. Future time series will be scaled appropriately.

• Numeric Sequences - Are not scaled, which means you should transform them to a unit difference of 1 so that your x is a sequence that increases by 1. Otherwise your period and fourier order will be incorrectly calculated. The solution is to just take your sequence and divide by the median difference between values.

Fourier Order, K

The fourier order is a parameter that increases the frequency. `K = 2` doubles the frequency. It's common in time series analysis to add multiple fourier orders (e.g. 1 through 5) to account for seasonalities that occur faster than the primary seasonality.

Type (Sin/Cos)

The type of the fourier series can be either `sin` or `cos`. It's common in time series analysis to add both sin and cos series.

## Value

A numeric vector

Fourier Modeling Functions:

• `step_fourier()` - Recipe for `tidymodels` workflow

• `tk_augment_fourier()` - Adds many fourier series to a `data.frame` (`tibble`)

• Fourier Series: `fourier_vec()`

• Box Cox Transformation: `box_cox_vec()`

• Lag Transformation: `lag_vec()`

• Differencing Transformation: `diff_vec()`

• Rolling Window Transformation: `slidify_vec()`

• Loess Smoothing Transformation: `smooth_vec()`

• Missing Value Imputation for Time Series: `ts_impute_vec()`, `ts_clean_vec()`

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27``` ```library(tidyverse) library(timetk) options(max.print = 50) date_sequence <- tk_make_timeseries("2016-01-01", "2016-01-31", by = "hour") # --- VECTOR --- fourier_vec(date_sequence, period = 7 * 24, K = 1, type = "sin") # --- MUTATE --- tibble(date = date_sequence) %>% # Add cosine series that oscilates at a 7-day period mutate( C1_7 = fourier_vec(date, period = 7*24, K = 1, type = "cos"), C2_7 = fourier_vec(date, period = 7*24, K = 2, type = "cos") ) %>% # Visualize pivot_longer(cols = contains("_"), names_to = "name", values_to = "value") %>% plot_time_series( date, value, .color_var = name, .smooth = FALSE, .interactive = FALSE, .title = "7-Day Fourier Terms" ) ```

timetk documentation built on Jan. 19, 2021, 1:06 a.m.