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)

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_cos6_K1 date_sin6_K2 date_cos6_K2
#>    <fct> <date>     <dbl>        <dbl>        <dbl>        <dbl>        <dbl>
#>  1 M1    1976-06-01  8000       -0.571      -0.821        0.938         0.347
#>  2 M1    1976-07-01  8350       -0.999       0.0506      -0.101        -0.995
#>  3 M1    1976-08-01  8570       -0.455       0.890       -0.811         0.585
#>  4 M1    1976-09-01  7700        0.543       0.840        0.912         0.410
#>  5 M1    1976-10-01  7080        1.00       -0.0169      -0.0338       -0.999
#>  6 M1    1976-11-01  6520        0.485      -0.874       -0.849         0.529
#>  7 M1    1976-12-01  6070       -0.485      -0.874        0.849         0.529
#>  8 M1    1977-01-01  6650       -1.00       -0.0169       0.0338       -0.999
#>  9 M1    1977-02-01  6830       -0.515       0.857       -0.882         0.470
#> 10 M1    1977-03-01  5710        0.394       0.919        0.725         0.689
#> # ℹ 1,564 more rows
#> # ℹ 4 more variables: date_sin12_K1 <dbl>, date_cos12_K1 <dbl>,
#> #   date_sin12_K2 <dbl>, date_cos12_K2 <dbl>