tk_augment_lags: Add many lags to the data

Description Usage Arguments Details Value See Also Examples

View source: R/augment-tk_augment_lags.R

Description

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

Usage

1
2
3
tk_augment_lags(.data, .value, .lags = 1, .names = "auto")

tk_augment_leads(.data, .value, .lags = -1, .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)

.names

A vector of names for the new columns. Must be of same length as .lags.

Details

Lags vs Leads

A negative lag is considered a lead. The tk_augment_leads() function is identical to tk_augment_lags() with the exception that the automatic naming convetion (.names = 'auto') will convert column names with negative lags to leads.

Benefits

This is a scalable function that is:

Value

Returns a tibble object describing the timeseries.

See Also

Augment Operations:

Underlying Function:

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
library(tidyverse)
library(timetk)

# Lags
m4_monthly %>%
    group_by(id) %>%
    tk_augment_lags(contains("value"), .lags = 1:20)

# Leads
m4_monthly %>%
    group_by(id) %>%
    tk_augment_leads(value, .lags = 1:-20)

timetk documentation built on Nov. 16, 2021, 9:26 a.m.