Python 3.5中的时间序列 - 适合ARMA模型

时间:2016-04-18 19:18:35

标签: python pandas time-series statsmodels

我正在尝试在我的IPython Notebook中使用ARMA模型。对于以下代码:

from statsmodels.tsa.arima_model import ARMA
arma_mod20 = sm.tsa.ARMA(dta, (2,0)).fit()
print (arma_mod20.params)

我收到以下错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-43-8c1d7770f953> in <module>()
  1 from statsmodels.tsa.arima_model import ARMA
----> 2 arma_mod20 = sm.tsa.ARMA(dta, (2,0)).fit()
  3 print (arma_mod20.params)

C:\Users\rajadm\Desktop\Anaconda3\lib\site-packages\statsmodels\tsa\arima_model.py in fit(self, start_params, trend, method, transparams, solver, maxiter, full_output, disp, callback, **kwargs)
917 
918         else:  # estimate starting parameters
--> 919             start_params = self._fit_start_params((k_ar, k_ma, k), method)
920 
921         if transparams:  # transform initial parameters to ensure invertibility

C:\Users\rajadm\Desktop\Anaconda3\lib\site-packages\statsmodels\tsa\arima_model.py in _fit_start_params(self, order, method)
554             func = lambda params: -self.loglike_css(params)
555             #start_params = [.1]*(k_ar+k_ma+k_exog) # different one for k?
--> 556             start_params = self._fit_start_params_hr(order)
557             if self.transparams:
558                 start_params = self._invtransparams(start_params)

C:\Users\rajadm\Desktop\Anaconda3\lib\site-packages\statsmodels\tsa\arima_model.py in _fit_start_params_hr(self, order)
490         if k != 0:
491             ols_params = GLS(endog, exog).fit().params
--> 492             start_params[:k] = ols_params
493             endog -= np.dot(exog, ols_params).squeeze()
494         if q != 0:

ValueError: could not broadcast input array from shape (2) into shape (1)

有任何建议的修复方法吗?

由于

1 个答案:

答案 0 :(得分:1)

看起来您需要调整输入格式。这有效:

import statsmodels as sm
from statsmodels.tsa.arima_model import ARMA
from datetime import date

dta = sm.datasets.sunspots.load_pandas().data
dta.index = pd.date_range(start=date(1700,12, 31), end=date(2008,12,31), freq='A')
dta = dta.drop('YEAR', axis=1)

dta.info()

DatetimeIndex: 309 entries, 1700-12-31 to 2008-12-31
Freq: A-DEC
Data columns (total 1 columns):
SUNACTIVITY    309 non-null float64
dtypes: float64(1)
memory usage: 4.8 KB


arma_mod20 = ARMA(dta, (2, 0)).fit()
arma_mod20.params

const                49.659542
ar.L1.SUNACTIVITY     1.390656
ar.L2.SUNACTIVITY    -0.688571
dtype: float64