Is statsmodels lagmatrix function "wrong" ? (adds zeros to lagged array)
Question: **Example:**

Python

    
    
    lagmatrix([1 2 3])
    returns [0 1 2]
    

This is obviously not correct if I want to regress Y against the lagged values
of Y (i.e. an AR process).

I want to run a regress of Y and the lag values of Y using statsmodel.OLS but
if I put NaN in the lagged verison of Y, OLS complains and doesn't run.

Is there a way to run the regression without regressing `Y[1:-1]` against
`lagmatrix(Y)[1:-1]` ?

If I have more lags this can get annoying.

How does the AR function in statsmodels find lags?


Answer: I don't know what your `lagmatrix` is.

I would recommend using pandas, which has a lag method and has nan handling.

statsmodels has two functions that are used internally to create the lag
matrices for autoregressive and vector autoregressive models and for related
hypothesis tests, which have a `trim` option to choose how to treat initial
and trailing observations.

    
    
    >>> from statsmodels.tsa.tsatools import lagmat, lagmat2ds
    >>> x = np.arange(10)
    >>> y_lagged, y = lagmat(x, maxlag=2, trim="forward", original='sep')
    >>> y_lagged
    array([[ 0.,  0.],
           [ 0.,  0.],
           [ 1.,  0.],
           [ 2.,  1.],
           [ 3.,  2.],
           [ 4.,  3.],
           [ 5.,  4.],
           [ 6.,  5.],
           [ 7.,  6.],
           [ 8.,  7.]])
    >>> y
    array([[0],
           [1],
           [2],
           [3],
           [4],
           [5],
           [6],
           [7],
           [8],
           [9]])
    >>> y_lagged, y = lagmat(x, maxlag=2, trim="both", original='sep')
    >>> y_lagged
    array([[ 1.,  0.],
           [ 2.,  1.],
           [ 3.,  2.],
           [ 4.,  3.],
           [ 5.,  4.],
           [ 6.,  5.],
           [ 7.,  6.],
           [ 8.,  7.]])
    >>> y
    array([[2],
           [3],
           [4],
           [5],
           [6],
           [7],
           [8],
           [9]])
    

