Skip to contents

A handy function for adding multiple lagged difference values to a data frame. Works with dplyr groups too.

Usage

tk_augment_differences(
  .data,
  .value,
  .lags = 1,
  .differences = 1,
  .log = FALSE,
  .names = "auto"
)

Arguments

.data

A tibble.

.value

One or more column(s) to have a transformation applied. Usage of tidyselect functions (e.g. contains()) can be used to select multiple columns.

.lags

One or more lags for the difference(s)

.differences

The number of differences to apply.

.log

If TRUE, applies log-differences.

.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 .lags argument (e.g. lags = 1:20)

See also

Augment Operations:

Underlying Function:

  • diff_vec() - Underlying function that powers tk_augment_differences()

Examples

library(tidyverse)
library(timetk)

m4_monthly %>%
    group_by(id) %>%
    tk_augment_differences(value, .lags = 1:20)
#> # A tibble: 1,574 × 23
#> # Groups:   id [4]
#>    id    date       value value_lag1_d…¹ value…² value…³ value…⁴ value…⁵ value…⁶
#>    <fct> <date>     <dbl>          <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#>  1 M1    1976-06-01  8000             NA      NA      NA      NA      NA      NA
#>  2 M1    1976-07-01  8350            350      NA      NA      NA      NA      NA
#>  3 M1    1976-08-01  8570            220     570      NA      NA      NA      NA
#>  4 M1    1976-09-01  7700           -870    -650    -300      NA      NA      NA
#>  5 M1    1976-10-01  7080           -620   -1490   -1270    -920      NA      NA
#>  6 M1    1976-11-01  6520           -560   -1180   -2050   -1830   -1480      NA
#>  7 M1    1976-12-01  6070           -450   -1010   -1630   -2500   -2280   -1930
#>  8 M1    1977-01-01  6650            580     130    -430   -1050   -1920   -1700
#>  9 M1    1977-02-01  6830            180     760     310    -250    -870   -1740
#> 10 M1    1977-03-01  5710          -1120    -940    -360    -810   -1370   -1990
#> # … with 1,564 more rows, 14 more variables: value_lag7_diff1 <dbl>,
#> #   value_lag8_diff1 <dbl>, value_lag9_diff1 <dbl>, value_lag10_diff1 <dbl>,
#> #   value_lag11_diff1 <dbl>, value_lag12_diff1 <dbl>, value_lag13_diff1 <dbl>,
#> #   value_lag14_diff1 <dbl>, value_lag15_diff1 <dbl>, value_lag16_diff1 <dbl>,
#> #   value_lag17_diff1 <dbl>, value_lag18_diff1 <dbl>, value_lag19_diff1 <dbl>,
#> #   value_lag20_diff1 <dbl>, and abbreviated variable names ¹​value_lag1_diff1,
#> #   ²​value_lag2_diff1, ³​value_lag3_diff1, ⁴​value_lag4_diff1, …
#> # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names