4.8.1.1.3. statsmodels.tsa.api.add_lag¶
-
statsmodels.tsa.api.
add_lag
(x, col=None, lags=1, drop=False, insert=True)[source]¶ Returns an array with lags included given an array.
Parameters: x : array
An array or NumPy ndarray subclass. Can be either a 1d or 2d array with observations in columns.
col : ‘string’, int, or None
If data is a structured array or a recarray, col can be a string that is the name of the column containing the variable. Or 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 : ndarray
Array with lags
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
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)