statsmodels.tsa.tsatools.lagmat

statsmodels.tsa.tsatools.lagmat(x, maxlag, trim='forward', original='ex', use_pandas=False)[source]

Create 2d array of lags

Parameters:
  • x (array_like, 1d or 2d) – data; if 2d, observation in rows and variables in columns
  • maxlag (int) – all lags from zero to maxlag are included
  • trim (str {'forward', 'backward', 'both', 'none'} or None) –
    • ‘forward’ : trim invalid observations in front
    • ’backward’ : trim invalid initial observations
    • ’both’ : trim invalid observations on both sides
    • ’none’, None : no trimming of observations
  • original (str {'ex','sep','in'}) –
    • ‘ex’ : drops the original array returning only the lagged values.
    • ’in’ : returns the original array and the lagged values as a single array.
    • ’sep’ : returns a tuple (original array, lagged values). The original
      array is truncated to have the same number of rows as the returned lagmat.
  • use_pandas (bool, optional) – If true, returns a DataFrame when the input is a pandas Series or DataFrame. If false, return numpy ndarrays.
Returns:

  • lagmat (2d array) – array with lagged observations
  • y (2d array, optional) – Only returned if original == ‘sep’

Examples

>>> from statsmodels.tsa.tsatools import lagmat
>>> import numpy as np
>>> X = np.arange(1,7).reshape(-1,2)
>>> lagmat(X, maxlag=2, trim="forward", original='in')
array([[ 1.,  2.,  0.,  0.,  0.,  0.],
   [ 3.,  4.,  1.,  2.,  0.,  0.],
   [ 5.,  6.,  3.,  4.,  1.,  2.]])
>>> lagmat(X, maxlag=2, trim="backward", original='in')
array([[ 5.,  6.,  3.,  4.,  1.,  2.],
   [ 0.,  0.,  5.,  6.,  3.,  4.],
   [ 0.,  0.,  0.,  0.,  5.,  6.]])
>>> lagmat(X, maxlag=2, trim="both", original='in')
array([[ 5.,  6.,  3.,  4.,  1.,  2.]])
>>> lagmat(X, maxlag=2, trim="none", original='in')
array([[ 1.,  2.,  0.,  0.,  0.,  0.],
   [ 3.,  4.,  1.,  2.,  0.,  0.],
   [ 5.,  6.,  3.,  4.,  1.,  2.],
   [ 0.,  0.,  5.,  6.,  3.,  4.],
   [ 0.,  0.,  0.,  0.,  5.,  6.]])

Notes

When using a pandas DataFrame or Series with use_pandas=True, trim can only be ‘forward’ or ‘both’ since it is not possible to consistently extend index values.