具有外生变量矩阵的statsmodels SARIMAX是不同的大小

时间:2018-01-16 20:37:28

标签: python statsmodels arima

我正在运行SARIMAX模型但遇到了指定外生变量的问题。在第一个代码块(下面)中,我指定了一个外生变量lesdata ['LESpost'],模型运行没有问题。但是,当我添加另一个外生变量时,我最终会收到一条错误消息(请参阅堆栈跟踪)。

ar = (1,0,1)      #  AR(1 3)
ma = (0)  #  No MA terms
mod1 = sm.tsa.statespace.SARIMAX(lesdata['emadm'], exog= (lesdata['LESpost'],lesdata['QOF']), trend='c', order=(ar,0,ma), mle_regression=True)

Traceback (most recent call last):

  File "<ipython-input-129-d1300aeaeffc>", line 4, in <module>
    mle_regression=True)

  File "C:\Users\danie\Anaconda2\lib\site-packages\statsmodels\tsa\statespace\sarimax.py", line 510, in __init__
    endog, exog=exog, k_states=k_states, k_posdef=k_posdef, **kwargs

  File "C:\Users\danie\Anaconda2\lib\site-packages\statsmodels\tsa\statespace\mlemodel.py", line 84, in __init__
    missing='none')

  File "C:\Users\danie\Anaconda2\lib\site-packages\statsmodels\tsa\base\tsa_model.py", line 43, in __init__
    super(TimeSeriesModel, self).__init__(endog, exog, missing=missing)

  File "C:\Users\danie\Anaconda2\lib\site-packages\statsmodels\base\model.py", line 212, in __init__
    super(LikelihoodModel, self).__init__(endog, exog, **kwargs)

  File "C:\Users\danie\Anaconda2\lib\site-packages\statsmodels\base\model.py", line 63, in __init__
    **kwargs)

  File "C:\Users\danie\Anaconda2\lib\site-packages\statsmodels\base\model.py", line 88, in _handle_data
    data = handle_data(endog, exog, missing, hasconst, **kwargs)

  File "C:\Users\danie\Anaconda2\lib\site-packages\statsmodels\base\data.py", line 630, in handle_data
    **kwargs)

  File "C:\Users\danie\Anaconda2\lib\site-packages\statsmodels\base\data.py", line 80, in __init__
    self._check_integrity()

  File "C:\Users\danie\Anaconda2\lib\site-packages\statsmodels\base\data.py", line 496, in _check_integrity
    super(PandasData, self)._check_integrity()

  File "C:\Users\danie\Anaconda2\lib\site-packages\statsmodels\base\data.py", line 403, in _check_integrity
    raise ValueError("endog and exog matrices are different sizes")

ValueError: endog and exog matrices are different sizes

我有什么明显的遗失吗?这些变量都具有相同的长度,并且没有丢失的数据。

感谢您阅读并希望您能提供帮助!

1 个答案:

答案 0 :(得分:1)

在应用numpy.asarray之后,二维数据需要在行和变量中进行观察。

scanf

将asarray应用于此元组会将变量放入行中,这些行是C源的numpy默认值,而不是statsmodels想要的。

DataFrames已经以适当的方式成形,因此一个选项是使用具有所需列的DataFrame

exog = (lesdata['LESpost'],lesdata['QOF'])

array_likes的列表或元组的另一个选项是使用exog = lesdata[['LESpost', 'QOF']],例如

numpy.column_stack