statsmodels.tsa.tsatools.lagmat

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

Create 2d array of lags.

Parameters:
xarray_like

Data; if 2d, observation in rows and variables in columns.

maxlagint

All lags from zero to maxlag are included.

trim{‘forward’, ‘backward’, ‘both’, ‘none’, None}

The trimming method to use.

  • ‘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{‘ex’,’sep’,’in’}

How the original is treated.

  • ‘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_pandasbool

If true, returns a DataFrame when the input is a pandas Series or DataFrame. If false, return numpy ndarrays.

Returns:
lagmatndarray

The array with lagged observations.

yndarray, optional

Only returned if original == ‘sep’.

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.

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.]])

Last update: Feb 14, 2024