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, optional

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

lagsint, optional

The number of lags desired.

dropbool, optional

Whether to keep the contemporaneous variable for the data.

insertbool or int, optional

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 length of the returned array is len(X) - lags. The lags are returned in increasing order, i.e., 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)