python实现var模型_Python中带有pandas+statsmodels的VAR模型

我是R的狂热用户,但最近因为几个不同的原因切换到Python。然而,我正在努力从statsmodels运行Python中的vector AR模型。

问题1。当我运行这个时,我得到一个错误,我怀疑它与我的向量类型有关。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 "", line 1, in

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

我试着用第三个,更短的向量,t s,从Wes McKinney的“Python for Data Analysis”,第293页的VAR模型,但是它不起作用。

好吧,现在我想这是因为向量是不同的类型:>>> speedonly.head()

speed

0 559.984

1 559.984

2 559.984

3 559.984

4 559.984

>>> type(speedonly)

#DOESN'T WORK

>>> type(data)

#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)

#DOESN'T WORK

所以我把速度转换成ndarray。。。但还是不行。但这次我又犯了一个错误:>>> nda_speedonly = np.array(speedonly)

>>> results = statsmodels.tsa.api.VAR(nda_speedonly)

Traceback (most recent call last):

File "", line 1, in

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

有什么建议吗?

问题2。我的数据集中有一些外生的特征变量,它们似乎对预测有用。上面的statsmodels模型是最好的吗?

你可能感兴趣的:(python实现var模型)