B: Create Lagged or Lead Variables for Time Series Regression

View source: R/dynamicStuff.R

BR Documentation

Create Lagged or Lead Variables for Time Series Regression

Description

'B()' acts as a backshift operator and creates lagged (past values) or lead (future values) versions of a variable for use in regression formulas. This function is designed to work within R formula syntax, similar to how 'I()' or 'log()' work.

Usage

B(x, k, ...)

Arguments

x

A numeric vector or time series variable to be lagged or lead

k

An integer specifying the lag order:

  • Positive values (e.g., 'k = 1') create lags (past values)

  • Negative values (e.g., 'k = -1') create leads (future values)

  • Zero ('k = 0') returns the original variable unchanged

...

Parameters passed to 'xregExpander()'.

Details

The function calls for the 'xregExpander()' to create lags/leads. So, you can pass additional parameters to it via ellipsis.

When 'k > 0' (lag), the function shifts values forward in time, so 'B(x, 1)' at time 't' contains the value of 'x' at time 't-1'.

When 'k < 0' (lead), the function shifts values backward in time, so 'B(x, -1)' at time 't' contains the value of 'x' at time 't+1'.

Value

A numeric vector of the same length as 'x'. The missing values are treated by the 'xregExpander()'. By default they are extrapolated (gaps="auto").

See Also

, xregExpander for data frame lag operations lag for time series lag (different behavior)

Examples

# Create sample time series data
y = rnorm(10)

# Create lags
B(y, 1)

# Create leads
B(y, -1)



greybox documentation built on Oct. 2, 2026, 5:08 p.m.