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:
  • x (array_like) – An array or NumPy ndarray subclass. Can be either a 1d or 2d array with observations in columns.

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

  • lags (int) – The number of lags desired.

  • drop (bool) – Whether to keep the contemporaneous variable for the data.

  • insert (bool or int) – If True, inserts the lagged values after col. If False, appends the data. If int inserts the lags at int.

Returns:

array – Array with lags

Return type:

ndarray

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)

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