statsmodels.tsa.tsatools.add_lag

statsmodels.tsa.tsatools.add_lag(x, col=None, lags=1, drop=False, insert=True)[source]

Returns an array with lags included given an array.

Parameters:
xarray_like

An array or NumPy ndarray subclass. Can be either a 1d or 2d array with observations in columns.

colint or None

col can be an int of the zero-based column index. If it’s a 1d array col can be None.

lagsint

The number of lags desired.

dropbool

Whether to keep the contemporaneous variable for the data.

insertbool or int

If True, inserts the lagged values after col. If False, appends the data. If int inserts the lags at int.

Returns:
arrayndarray

Array with lags

Notes

Trims the array both forward and backward, so that the array returned so that the length of the returned array is len(X) - lags. The lags are returned in increasing order, ie., t-1,t-2,…,t-lags

Examples

>>> import statsmodels.api as sm
>>> data = sm.datasets.macrodata.load()
>>> data = data.data[['year','quarter','realgdp','cpi']]
>>> data = sm.tsa.add_lag(data, 'realgdp', lags=2)

Last update: Mar 18, 2024