Search code examples
pythonruntime-errorstatsmodels

Fit new model in statsmodel fails - RuntimeError


I have created one AR model using statsmodels . That has worked but when I tried to create new model I hav gotten RuntimeError :

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.

This is how I generated the first model:

model=AR(df['Pop'])
AR1fit=model.fit(maxlag=1)

...
AR1fit.predict(start=start,end=end)


#the 2nd model:
AR2fit=model.fit(maxlag=2)
>>>
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.

I haven't found any post about this specific error, my goal is to fit the new model .


Solution

  • You have to instantiate a new model for the AR(2) and then call the fit method:

    model2 = AR(df['Pop'])
    AR2fit = model2.fit(maxlag=2)