Time Series Modeling Using Auto Arima With Python
By: Ulku Guneysu
Tags: Python, Real Estate, Python3, Programming, Data Science

Modeling

We subset time series from 2012 to 2018 for modeling.

Austin, Texas Plotly Express Scatter Map

Stationarity check

To have a better understanding of how to set the auto_arima parameter limits, we inspect the time series for trends, seasonality, decomposition, residuals, autocorrelation, and partial autocorrelation.

Check for stationarity using rolling statistics and Dickey-Fuller test

Rolling statistics provide visual support while the Dickey-Fuller test calculates test statistic, p-value, critical values, number of lags, and observations used. The null hypothesis for the Dickey-Fuller test is that the time series is not stationary. If the test statistic is less than the critical value and the p-value is lower than alpha (usually 0.05), we reject the null hypothesis and say the series is stationary.

If the time series is not stationary, different kinds of methods can be applied in order to deal with this problem: log transformation, differencing, subtracting the rolling mean, taking the square root, etc.

2. Apply decomposition with seasonal_decomposition() from statsmodels.tsa.seasonal to visualize trends, seasonality, and residuals. We proceed with a stationarity check of the residuals to see if our times series is stationary after the trends and seasonality are removed.

3. Plot autocorrelation and partial autocorrelation graphs. These plots provide information on (p, d, q) which are the essential order for modeling.

Regarding a stationarity check, we confirm that our data is not stationary. We apply log transformation before we start modeling.

Austin, Texas Property Values 2012 — 2018

Train-test split

The train-test split may vary depending on the time series size. We set out the train-test size 0.8/0.2.

Auto_arima model

We fit the model to train the set using the auto_arima function.

model = auto_arima(train, start_p=0, start_q=0)

model.summary()

The summary property provides all the information regarding our model. We see that our model used (4, 1, 0) for (p, d, q) order. We see the coefficients, p-value, AIC, and BIC, which provide information on whether the model is good.

Plot diagnostics for standardized residuals using plot_diagnostics() . This will provide information on:

Standardized residuals over time

Histogram plus estimated density of standardized residuals, along with a Normal (0,1) density plotted for reference

Normal Q-Q plot, with Normal reference line

Correlogram

model.plot_diagnostics()

Model forecast

Use get_forecast() and conf_int() built-in methods to forecast our model. The forecast_to_df function will take model and steps as input and return a data frame with predictions, showing the lower confidence interval and upper confidence interval as columns.

# Returns a dataframe

pred_df = forecast_to_df(model, steps = len(test))

Model validation

For model validation, we use mean squared error (MSE is an optional selection). Depending on the MSE value, we can see how well our model is fitting.

pred = pred_df['pred'] # Calculate MSE

mse = mean_squared_error(pred, test)

Plot train-test data with predictions and confidence intervals to visualize how good our predictions are.

Auto Arima Model Validation

Future predictions

For future predictions, fit the model above to our time series.

Get summary() and plot_diagnostics() for themodel’s statistics and standardized residuals. Use the given forecast_to_df function above to make future predictions. (Determine the steps parameter depending on how many months into the future we would like to forecast.) Use plot_train_test_pred function to visualize train-test splits with 36 months future predictions with confidence intervals.

Auto Arima Forecast for 36-Month Period