Search code examples
pythontime-seriesforecasting

ARIMA prediction in a loop Python


I am new to Python. I am using statsmodels for predicting through ARIMA. I have 3 questions. First of all, I am writing the following code

dates = pd.date_range('2012-07-09','2012-07-30')
series = [43.,32.,63.,98.,65.,78.,23.,35.,78.,56.,45.,45.,56.,6.,63.,45.,64.,34.,76.,34.,14.,54.]
res = Series(series, index=dates)
r = ARIMA(res,(1,2,0))
pred = r.predict(params = ? , start = ?, end = ? , typ='levels')

Here, what would be the params, start and end ? In the documentation I understood that start and end means the start and end of the predicting values but what about params. I do not know what i have to put in params.

Secondly, I have almost 1000 time series of items. So, for each item i want to recognize optimal (p,d,q) values and apply ARIMA each time in a loop so that at the end I get the result in a dictionary containing item name and its predicted value.Please help me how can do this ?

Third, Now If I am able to get ARIMA forecast for each time series it will not be just a forecast, instead it will contain lots of values like coeff, p-values, etc so how can I directly access the coeffients only and use them somewhere as values.

I'll be extremely thankful If someone can help me out in this. Many Thanks in advance.


Solution

  • Start and end are the starting and ending points you wish to forecast. So this might be start = '2012-07-31' and end = '2012-09-01'.

    Regarding params - when .fit() is called, an ARIMAResults class is returned. This class' predict method does not require the params argument: start and end should be all that's needed.

    For your second question, this answer should help. I was not actually able to get that code to work for myself, but I'm sure you could get a AIC/BIC grid search to work in that or a similar way. An alternative would be switching to R and using the auto.arima function, which also selects the best (p,d,q) order based on AIC/BIC (which is definitely more advisable than selecting based on p-values).

    You should be able to get the coefficients from your fitted model using r.params