python做var模型_在Python中使用pandas statsmodels的VAR模型

我是R的狂热用户,但最近由于几个不同的原因切换到

Python.但是,我正在努力从statsmodels运行Python中的矢量AR模型.

,Q#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

我尝试使用Wes McKinney的“用于数据分析的Python”第293页的第三个较短向量ts的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

所以我将speedonly转换为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

有什么建议?

Q | 2.我的数据集中有外生特征变量,似乎对预测很有用.上面的statsmodels模型是否是最好用的?

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