Skip to contents

Takes a numeric vector(s) or date and will return a tibble of one of the following:

  • "sin"

  • "cos"

  • "sincos"

  • c("sin","cos","sincos")

Usage

hai_fourier_augment(
  .data,
  .value,
  .period,
  .order,
  .names = "auto",
  .scale_type = c("sin", "cos", "sincos")
)

Arguments

.data

The data being passed that will be augmented by the function.

.value

This is passed rlang::enquo() to capture the vectors you want to augment.

.period

The number of observations that complete a cycle

.order

The fourier term order

.names

The default is "auto"

.scale_type

A character of one of the following: "sin","cos", or sincos" All can be passed by setting the param equal to c("sin","cos","sincos")

Value

A augmented tibble

Details

Takes a numeric vector or date and will return a vector of one of the following:

  • "sin"

  • "cos"

  • "sincos"

  • c("sin","cos","sincos")

This function is intended to be used on its own in order to add columns to a tibble.

Author

Steven P. Sanderson II, MPH

Examples

suppressPackageStartupMessages(library(dplyr))

len_out <- 10
by_unit <- "month"
start_date <- as.Date("2021-01-01")

data_tbl <- tibble(
  date_col = seq.Date(from = start_date, length.out = len_out, by = by_unit),
  a = rnorm(len_out),
  b = runif(len_out)
)

hai_fourier_augment(data_tbl, b, .period = 12, .order = 1, .scale_type = "sin")
#> # A tibble: 10 × 4
#>    date_col         a      b fourier_b_sin
#>    <date>       <dbl>  <dbl>         <dbl>
#>  1 2021-01-01 -0.496  0.304         0.158 
#>  2 2021-02-01 -1.42   0.160         0.0837
#>  3 2021-03-01 -0.885  0.197         0.103 
#>  4 2021-04-01  1.68   0.0704        0.0368
#>  5 2021-05-01 -0.0517 0.748         0.382 
#>  6 2021-06-01  0.234  0.431         0.224 
#>  7 2021-07-01 -0.661  0.0389        0.0204
#>  8 2021-08-01 -1.17   0.265         0.138 
#>  9 2021-09-01 -0.449  0.660         0.339 
#> 10 2021-10-01  0.276  0.300         0.156 
hai_fourier_augment(data_tbl, b, .period = 12, .order = 1, .scale_type = "cos")
#> # A tibble: 10 × 4
#>    date_col         a      b fourier_b_cos
#>    <date>       <dbl>  <dbl>         <dbl>
#>  1 2021-01-01 -0.496  0.304          0.987
#>  2 2021-02-01 -1.42   0.160          0.996
#>  3 2021-03-01 -0.885  0.197          0.995
#>  4 2021-04-01  1.68   0.0704         0.999
#>  5 2021-05-01 -0.0517 0.748          0.924
#>  6 2021-06-01  0.234  0.431          0.975
#>  7 2021-07-01 -0.661  0.0389         1.00 
#>  8 2021-08-01 -1.17   0.265          0.990
#>  9 2021-09-01 -0.449  0.660          0.941
#> 10 2021-10-01  0.276  0.300          0.988