Get startedGet started for free

Fitting time series models

1. Fitting time series models

In the last chapter you learned about the basics of time series data and ARMA models. In this chapter you will get hands on with fitting these models to real world data and making forecasts.

2. Creating a model

We had a peek at fitting time series models in the last chapter but lets have a closer look. To fit these models we first import the ARIMA model class from the statsmodels package. An ARIMA model is a special kind of ARMA model, but we'll come to that later. We create the model object and define the model order. Remember that the order for an ARMA model is p, the number of autoregressive lags, and q, the number of moving average lags. The ARIMA model has one extra order parameter in the middle. If we set this to zero we just get an ARMA model. We must also feed in the training data. The data can be a pandas DataFrame, a pandas Series or a NumPy array.

3. Creating AR and MA models

To fit an AR model we can simply use the ARIMA class with q equal to zero. To fit an MA model, we set p equal to zero.

4. Fitting the model and fit summary

To fit the model we use the model's dot-fit method. This returns an ARIMA results object. We can print the summary of the model fit using the results' dot-summary method.

5. Fit summary

The printed summary will look like this. Let's take a closer look at the top of this table.

6. Fit summary

The top section includes useful information such as the order of the model that we fit, the number of observations or data points and the name of the time series.

7. Fit summary

The next section of the summary shows the fitted model parameters. Here we fitted an ARMA(2,1) model, so the model has AR-lag-1 and lag-2 coefficients. In the table these are the AR-dot-L1-dot-y and AR-dot-L2-dot-y rows. The lag-1 MA coefficient is in the second last row. sigma2 is the variance of the shock terms. The first column shows the model coefficients whilst the second column shows the standard error in these coefficients. This is the uncertainty on the fitted coefficient values.

8. Introduction to ARMAX models

One possible extension to the ARMA model is to use exogenous inputs to create the ARMAX model. This means that we model the time series using other independent variables as well as the time series itself. This is like a combination between an ARMA model and a normal linear regression model.

9. ARMAX equation

The equations for two simple ARMA and ARMAX models are shown here. The only difference is one extra term. We add a new independent variable z-t multiplied by its coefficient x-one. Let's think of an example where ARMAX might be useful.

10. ARMAX example

Imagine you wanted to model your own daily personal productivity. This could reasonably be an ARMA model as your productivity on previous days may have an effect on your productivity today. You could be overworked or just on a roll.

11. ARMAX example

A useful exogenous variable could be the amount of sleep you got the night before since this might affect your productivity. Here z-one would be hours slept, and if more sleep made you more productive then the coefficient x-one would be positive.

12. Fitting ARMAX

We can fit an ARMAX model using the same ARIMA model class we used before. The only difference is that we will now feed in our exogenous variable using the exog keyword. The model order and the fitting procedure are just the same.

13. ARMAX summary

The summary now includes information on the x-one row for the exogenous variable parameter.

14. Let's practice!

Now lets go practice.