VAR model with pandas + statsmodels in Python

I am an avid user of R, but recently switched to Python for several reasons. However, I'm struggling a bit to run the vector AR model in Python from statsmodels.

Q # 1. I get an error when I run it, and I have a suspicion that it has something to do with the type of my vector.

    import numpy as np
    import statsmodels.tsa.api
    from statsmodels import datasets
    import datetime as dt
    import pandas as pd
    from pandas import Series
    from pandas import DataFrame
    import os

    df = pd.read_csv('myfile.csv')
    speedonly = DataFrame(df['speed'])
    results = statsmodels.tsa.api.VAR(speedonly)

    Traceback (most recent call last):
    File "<pyshell#14>", line 1, in <module>
      results = statsmodels.tsa.api.VAR(speedonly)
    File "C:\Python27\lib\site-packages\statsmodels\tsa\vector_ar\var_model.py", line 336, in __init__
      super(VAR, self).__init__(endog, None, dates, freq)
    File "C:\Python27\lib\site-packages\statsmodels\tsa\base\tsa_model.py", line 40, in __init__
      self._init_dates(dates, freq)
    File "C:\Python27\lib\site-packages\statsmodels\tsa\base\tsa_model.py", line 54, in _init_dates
      raise ValueError("dates must be of type datetime")
    ValueError: dates must be of type datetime

Now, interestingly, when I run the VAR example from here https://github.com/statsmodels/statsmodels/blob/master/docs/source/vector_ar.rst#id5 , it works great.

I am trying to use the VAR model with the third, short vector, ts, from Wes McKinney "Python for Data Analysis", page 293 and does not work.

Ok, now I think about it because vectors are different types:

    >>> speedonly.head()
         speed
    0  559.984
    1  559.984
    2  559.984
    3  559.984
    4  559.984
    >>> type(speedonly)
    <class 'pandas.core.frame.DataFrame'> #DOESN'T WORK

    >>> type(data)
    <type 'numpy.ndarray'> #WORKS

    >>> ts
    2011-01-02   -0.682317
    2011-01-05    1.121983
    2011-01-07    0.507047
    2011-01-08   -0.038240
    2011-01-10   -0.890730
    2011-01-12   -0.388685
    >>> type(ts)
    <class 'pandas.core.series.TimeSeries'> #DOESN'T WORK

speedonly ndarray... . :

   >>> nda_speedonly = np.array(speedonly)
   >>> results = statsmodels.tsa.api.VAR(nda_speedonly)

   Traceback (most recent call last):
   File "<pyshell#47>", line 1, in <module>
     results = statsmodels.tsa.api.VAR(nda_speedonly)
   File "C:\Python27\lib\site-packages\statsmodels\tsa\vector_ar\var_model.py", line 345, in __init__
     self.neqs = self.endog.shape[1]
   IndexError: tuple index out of range

?

Q # 2. , . statsmodels ?

+5
1

pandas , , - . ( ).

ValueError: Given a pandas object and the index does not contain dates

1d VAR. VAR , . , , . , . AR , , sm.tsa.ARMA. , ARMA.predict . , .

+3

All Articles