lag_matrix: Function for adding lags of time series variables

View source: R/lag_matrix.R

lag_matrixR Documentation

Function for adding lags of time series variables

Description

Generates specified number of lagged variables of the given variable in the form of a tibble.

Usage

lag_matrix(variable, n = 10)

Arguments

variable

Variable to be lagged.

n

Number of lags. The default value is n = 10.

Value

A tibble.

Examples

library(dplyr)
library(tibble)
library(tidyr)
# Adding lagged variables to an existing tibble
set.seed(123)
sim_data <- tibble(x_lag_000 = runif(100)) |>
  mutate(x_lag = lag_matrix(x_lag_000, 3)) |>
  unpack(x_lag, names_sep = "_")

smimodel documentation built on April 8, 2026, 5:06 p.m.