# lag_vec: Lag Transformation In timetk: A Tool Kit for Working with Time Series in R

## Description

`lag_vec()` applies a Lag Transformation.

## Usage

 ```1 2 3``` ```lag_vec(x, lag = 1) lead_vec(x, lag = -1) ```

## Arguments

 `x` A numeric vector to be lagged. `lag` Which lag (how far back) to be included in the differencing calculation. Negative lags are leads.

## Details

Benefits:

This function is `NA` padded by default so it works well with `dplyr::mutate()` operations. The function allows both lags and leads (negative lags).

Lag Calculation

A lag is an offset of `lag` periods. `NA` values are returned for the number of `lag` periods.

A negative lag is considered a lead. The only difference between `lead_vec()` and `lag_vec()` is that the `lead_vec()` function contains a starting negative value.

## Value

A numeric vector

• `recipes::step_lag()` - Recipe for adding lags in `tidymodels` modeling

• `tk_augment_lags()` - Add many lags group-wise to a data.frame (tibble)

Vectorized Transformations:

• Box Cox Transformation: `box_cox_vec()`

• Lag Transformation: `lag_vec()`

• Differencing Transformation: `diff_vec()`

• Rolling Window Transformation: `slidify_vec()`

• Loess Smoothing Transformation: `smooth_vec()`

• Fourier Series: `fourier_vec()`

• Missing Value Imputation for Time Series: `ts_impute_vec()`, `ts_clean_vec()`

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17``` ```library(dplyr) library(timetk) # --- VECTOR ---- # Lag 1:10 %>% lag_vec(lag = 1) # Lead 1:10 %>% lag_vec(lag = -1) # --- MUTATE ---- m4_daily %>% group_by(id) %>% mutate(lag_1 = lag_vec(value, lag = 1)) ```

timetk documentation built on Jan. 19, 2021, 1:06 a.m.