Reputation: 53
I have a dataset with multiple cities and I'm trying to build an ARIMA model for each city, so in my code I'm splitting the data using a for loop and finding the best model before sending those parameters to the final fitting. My question is how can I automate the process? Is there any way to extract the p, d, q value out of the best model which is returned by the ARIMACheck function?
def ARIMACheck(data):
from pmdarima import auto_arima
fit = auto_arima(data[20], trace=True)
return fit
def ARIMA(data, p, d, q):
from statsmodels.tsa.arima.model import ARIMA
x_train = data.iloc[:-200]
x_test = data.iloc[-200:]
y_test = x_test.loc[:, 20]
model = ARIMA(x_train[20], order=(p,d,q))
model = model.fit()
def Split(data):
for i in range(7):
data[i].replace(0, np.nan, inplace=True)
for i in range(7):
datatemp = data.copy()
datatemp = datatemp.dropna(subset=[i])
datamap = datatemp.copy()
datamap = datamap.loc[:, 20]
datamap.plot(figsize=(50,10))
fit = ARIMACheck(datatemp)
print(fit)
ARIMA(datatemp, 1, 1, 2)
Split(data)
Upvotes: 2
Views: 3283
Reputation: 923
First of all, the auto_arima
function returns an ARIMA object that runs on statsmodels, so you could just use the fit
from you method ARIMACheck(data)
.
If you want to create a new model with the statsmodels class, then you can use the following to extract the order from the auto_arima
fit and use it to train a new model in your ARIMA
method:
def ARIMA(data, fit):
model = ARIMA(endog=x_train[20], order=fit.get_params().get("order")).fit()
and call the method by:
ARIMA(datatemp, fit)
fit.get_params().get("order")
returns a tuple like (p, d, q) so you can access each element by fit.get_params().get("order")[<index>]
.
Upvotes: 2