lagmatrix: Create lags or leads of a matrix

View source: R/lagmatrix.R

lagmatrixR Documentation

Create lags or leads of a matrix

Description

Find a shifted version of a matrix, adjusting the time base backward (lagged) or forward (leading) by a specified number of observations for each column.

Usage

lagmatrix(x, lag)

Arguments

x

A matrix or a multivariate time series.

lag

A vector of finite integer lags (positive values) or leads (negative values) with a length equal to the number of columns of x.

Value

A matrix with the same class and size as x.

See Also

cvforecast, which uses this function to align the forecasts and the forecast errors by horizon.

Examples

set.seed(1)
x <- matrix(rnorm(20), nrow = 5, ncol = 4)

# Create lags of a matrix
lagmatrix(x, c(0, 1, 2, 3))

# Create leads of a matrix
lagmatrix(x, c(0, -1, -2, -3))


conformalForecast documentation built on Oct. 15, 2026, 9:07 a.m.