statsmodels.tsa.tsatools.lagmat#
- statsmodels.tsa.tsatools.lagmat(x, maxlag, trim='forward', original='ex', use_pandas=False, *, result_object=None)[source]#
Create 2d array of lags
- Parameters:
- xarray_like
Data; if 2d, observation in rows and variables in columns.
- maxlag
intor array_likeofint The lags to be applied.
int : All lags from zero to maxlag are included.
- array_likeAll lags associated to the values in the array.
Must contain non-negative integers.
- trim{‘forward’, ‘backward’, ‘both’, ‘none’,
None},optional 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’},
optional 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,
optional If true, returns a DataFrame when the input is a pandas Series or DataFrame. If false, return numpy ndarrays.
- result_objectbool,
optional Flag controlling whether a
LagmatResultis returned. Whenoriginal="sep"aLagmatResultis always returned. For other values oforiginala bare array is returned unlessresult_object=True, which additionally yields aLagmatResultwithleadsset toNone.
- Returns:
LagmatResult,ndarray,orDataFrameWhen
original="sep"(orresult_object=True), aLagmatResultwith fields:- lagsndarray or DataFrame
The array with lagged observations.
- leadsndarray, DataFrame, or None
The original (unlagged) array, truncated to have the same number of rows as
lags.Nonefor other values oforiginal, where the original series was either excluded (“ex”) or folded intolags(“in”).
For other values of
originala bare array (or, whenuse_pandas=Trueand x is a pandas object, a DataFrame) of lagged observations is returned instead.
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.]])
>>> lagmat(X, maxlag=[1, 3], trim="forward", original='ex') array([[ 1., 2., 0., 0., 0., 0.], [ 3., 4., 1., 2., 0., 0.], [ 5., 6., 3., 4., 1., 2.]])