前提
問題点1
ARモデルをAICによって決定する
# importは通常通りの実装
model = ar_model.AR(y)
for i in range(20):
results = model.fit(maxlag=i+1)
では、以下のエラーが発生する
RuntimeError:
Model has been fit using maxlag=1, method=cmle, ic=None, trend=c. These
cannot be changed in subsequent calls to `fit`. Instead, use a new instance of
AR.
試したこと
コード
model = ar_model.AR(y)
results = model.fit(maxlag=1)
print(results.aic)
results = model.fit(maxlag=2)
print(results.aic)
出力結果
10.623349835083612
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-30-956416629a7e> in <module>
2 results = model.fit(maxlag=1)
3 print(results.aic)
----> 4 results = model.fit(maxlag=2)
5 print(results.aic)
/opt/conda/envs/timeseries/lib/python3.8/site-packages/statsmodels/tsa/ar_model.py in fit(self, maxlag, method, ic, trend, transparams, start_params, solver, maxiter, full_output, disp, callback, **kwargs)
1349 fit_params = (maxlag, method, ic, trend)
1350 if self._fit_params is not None and self._fit_params != fit_params:
-> 1351 raise RuntimeError(REPEATED_FIT_ERROR.format(*self._fit_params))
1352 if maxlag is None:
1353 maxlag = int(round(12 * (nobs / 100.0) ** (1 / 4.0)))
RuntimeError:
Model has been fit using maxlag=1, method=cmle, ic=None, trend=c. These
cannot be changed in subsequent calls to `fit`. Instead, use a new instance of
AR.
解決策1
コード
for i in range(20):
model = ar_model.AR(y_diff)
results = model.fit(maxlag=i+1)
print(f'lag = {i + 1}\taic : {results.aic}')
期待通りの結果が得られる