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(dplyr)

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_diff1 value_lag2_diff1 value_lag3_diff1
#>    <fct> <date>     <dbl>            <dbl>            <dbl>            <dbl>
#>  1 M1    1976-06-01  8000               NA               NA               NA
#>  2 M1    1976-07-01  8350              350               NA               NA
#>  3 M1    1976-08-01  8570              220              570               NA
#>  4 M1    1976-09-01  7700             -870             -650             -300
#>  5 M1    1976-10-01  7080             -620            -1490            -1270
#>  6 M1    1976-11-01  6520             -560            -1180            -2050
#>  7 M1    1976-12-01  6070             -450            -1010            -1630
#>  8 M1    1977-01-01  6650              580              130             -430
#>  9 M1    1977-02-01  6830              180              760              310
#> 10 M1    1977-03-01  5710            -1120             -940             -360
#> # ℹ 1,564 more rows
#> # ℹ 17 more variables: value_lag4_diff1 <dbl>, value_lag5_diff1 <dbl>,
#> #   value_lag6_diff1 <dbl>, 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>