Time Series Modeling with ARIMA to Predict Future House Price
By: Bonnie Ma
Tags: Time Series Forecasting, House Prices, Python, Data Science, Time Series Analysis

Keep in mind that time series is just a sequence of well-defined data points measured at consistent time intervals over a period of time. Time series analysis helps us understand the hidden pattern, meaningful characteristics and statistics about the data.

Step 1: Data processing — transform raw data to time series with Time being the index

Dealing with the dataset from Zillow.com, I have to first select the city and reshape the data frame from wide to long format using pd.melt and then transform to time series data.

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

%matplotlib inline dfm.set_index('Month', inplace = True)

Step 2: Data Exploration and Visualization — Seasonal Decomposition, Autocorrelation with ACF and PCAF, etc.

With visualization, we can identify underlying trends and stories in the data. Let’s take a look at the changes in housing prices in Brooklyn over time.

An overall upward trend can be observed from the years 1996 to 2007, followed by a fluctuation from 2008 to mid-2010. Starting in 2011, the house price became more stable and continued to rise again. We can say pretty confidently that the house market crash in 2008 was the cause of this fluctuation and we do want to skip this period to have a more accurate prediction of the future.

Trends more stable post 2011

There are three important characteristics we care about: Stationarity, Seasonality, and Autocorrelation.

Stationarity

Most time series models work on the assumption that the time series are stationary which means its statistical properties such as mean, variance, etc. remain constant over time. Ideally, we want to have a stationary time series for modeling. Dickey-Fuller test can be used to test if a time series is stationary or not. Note that the null hypothesis is: time series is not stationary.

from statsmodels.tsa.stattools import adfuller dftest = adfuller(ts)

Seasonality

Seasonality refers to the periodic changes and patterns that repeat within a fixed period. Sometimes, seasonality can be combined with an increasing or decreasing trend. Seasonal decomposition is always helpful to detect seasonality, trend and any noise in the dataset. Take Brooklyn housing data for example:

from statsmodels.tsa.seasonal import seasonal_decompose decomposition = sm.tsa.seasonal_decompose(month_avg, model='additive') trend = decomposition.trend

seasonal = decomposition.seasonal

residual = decomposition.resid # Plot gathered statistics

plt.figure(figsize=(12,8))

plt.subplot(411)

plt.plot(month_avg, label='Original', color='blue')

plt.legend(loc='best')

plt.subplot(412)

plt.plot(trend, label='Trend', color='blue')

plt.legend(loc='best')

plt.subplot(413)

plt.plot(seasonal,label='Seasonality', color='blue')

plt.legend(loc='best')

plt.subplot(414)

plt.plot(residual, label='Residuals', color='blue')

plt.legend(loc='best')

plt.tight_layout()

Seasonal decomposition of monthly average Brooklyn house price

We can clearly see from the chart above that there is an upward trend along with yearly seasonality. The next step is to test our residuals with Dickey-Fuller test.

Autocorrelation

Autocorrelation helps us study how each time series observation is related to its recent (or not so recent) past. It is easy to get that tomorrow’s house price is very likely to be related to today’s house price. ACF (Autocorrelation Function) and PACF (Partial Autocorrelation Function) are two powerful tools. ACF represents autocorrelation of a time series as a function of the time lag. PACF seeks to remove the indirect correlations that exist in the autocorrelation for an observation and an observation at a prior time step.m

Check the ACF and PACF of the monthly average price of all zip codes.