Skip to contents

A handy function for adding multiple fourier series to a data frame. Works with dplyr groups too.

Usage

tk_augment_fourier(.data, .date_var, .periods, .K = 1, .names = "auto")

Arguments

.data

A tibble.

.date_var

A date or date-time column used to calculate a fourier series

.periods

One or more periods for the fourier series

.K

The maximum number of fourier orders.

.names

A vector of names for the new columns. Must be of same length as the number of output columns. Use "auto" to automatically rename the columns.

Value

Returns a tibble object describing the timeseries.

Details

Benefits

This is a scalable function that is:

  • Designed to work with grouped data using dplyr::group_by()

  • Add multiple differences by adding a sequence of differences using the .periods argument (e.g. lags = 1:20)

See also

Augment Operations:

Underlying Function:

  • fourier_vec() - Underlying function that powers tk_augment_fourier()

Examples

library(dplyr)
library(timetk)

m4_monthly %>%
    group_by(id) %>%
    tk_augment_fourier(date, .periods = c(6, 12), .K = 2)
#> # A tibble: 1,574 × 11
#> # Groups:   id [4]
#>    id    date       value date_sin6_K1 date_co…¹ date_…² date_…³ date_…⁴ date_…⁵
#>    <fct> <date>     <dbl>        <dbl>     <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#>  1 M1    1976-06-01  8000       -0.571   -0.821   0.938    0.347   0.954  -0.299
#>  2 M1    1976-07-01  8350       -0.999    0.0506 -0.101   -0.995   0.689  -0.725
#>  3 M1    1976-08-01  8570       -0.455    0.890  -0.811    0.585   0.234  -0.972
#>  4 M1    1976-09-01  7700        0.543    0.840   0.912    0.410  -0.283  -0.959
#>  5 M1    1976-10-01  7080        1.00    -0.0169 -0.0338  -0.999  -0.713  -0.701
#>  6 M1    1976-11-01  6520        0.485   -0.874  -0.849    0.529  -0.968  -0.251
#>  7 M1    1976-12-01  6070       -0.485   -0.874   0.849    0.529  -0.968   0.251
#>  8 M1    1977-01-01  6650       -1.00    -0.0169  0.0338  -0.999  -0.713   0.701
#>  9 M1    1977-02-01  6830       -0.515    0.857  -0.882    0.470  -0.267   0.964
#> 10 M1    1977-03-01  5710        0.394    0.919   0.725    0.689   0.201   0.980
#> # … with 1,564 more rows, 2 more variables: date_sin12_K2 <dbl>,
#> #   date_cos12_K2 <dbl>, and abbreviated variable names ¹​date_cos6_K1,
#> #   ²​date_sin6_K2, ³​date_cos6_K2, ⁴​date_sin12_K1, ⁵​date_cos12_K1