3

我正在运行 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

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

感谢阅读,希望对您有所帮助!

4

1 回答 1

6

二维数据在应用 numpy.asarray 后需要在行中有观察值,在列中有变量。

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

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

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

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

array_likes 的列表或元组的另一个选项是使用numpy.column_stack,例如

exog = np.column_stack((lesdata['LESpost'].values,lesdata['QOF'].values))

于 2018-01-16T23:35:10.937 回答