Skip to contents

Creates a matrix of lagged values from a time series matrix. For each variable and each lag from 1 to p, this function creates a column containing the lagged values, removing the first p rows to align the lags properly.

Usage

flagmakerMatrix(y, p)

Arguments

y

A numeric matrix, data frame, or tibble of time series data with T rows (time periods) and N columns (variables)

p

An integer specifying the number of lags to create

Value

A numeric matrix with (T-p) rows and (N*p) columns. The columns are organized as: all N variables at lag 1, then all N variables at lag 2, etc. Column names are generated as "var_name_lag_k" where applicable.

Details

The function creates a lagged matrix where each column represents a lagged version of the original variables. The lags are organized by lag order first, then by variable. For example, with 2 variables and 2 lags, the column order would be: var1_lag1, var2_lag1, var1_lag2, var2_lag2.

Examples

if (FALSE) { # \dontrun{
# Create sample data as data frame
y <- data.frame(var1 = 1:10, var2 = 11:20)

# Create 2 lags
x <- lagmakerMatrix(y, 2)

# Works with tibbles too
library(tibble)
y_tbl <- tibble(var1 = 1:10, var2 = 11:20)
x <- lagmakerMatrix(y_tbl, 2)
} # }