State Space Models for Time Series Analysis and the dlm package

State space modelling is a popular technique for forecasting and smoothing time series data. There are two main components which make up state space models, an observed data and the unobserved states,

    \[ \begin{aligned} \text{observed data:  }& y_1, \dots, y_T \\ \text{unobserved data:  }& x_1, \dots, x_T \end{aligned} \]

The observed data are conditionally independent given the states. The transition probabilities of the states are defined as P(x_t|x_{t-1}) implying the Markov property of the probability of moving to the next state is only dependent on the previous state for all t=1, \dots, T. The probability of observing y at time t is given by P(y_t|x_t) implying the observed data is conditional on the current state.

Dynamic linear models are a special case of state space models where the errors of the state and observed components are normally distributed. The formulation of these models is as follows

    \[ \begin{aligned} x_0 & \sim N(m_0, C_0) \\ x_t|x_{t-1} & \sim N(G_t x_{t-1}, W_t) \\ y_t|x_t & \sim N(F_t x_t, V_t) \end{aligned} \]

Where

  • F_t is a p \times m matrix
  • G_t is a p \times p matrix
  • V_t is an m \times m matrix
  • W_t is a p \times p matrix

The formulation as a set of equations is given by

    \[ \begin{aligned} y_t &= F_t x_t + \nu_t \\ x_t &= G_t x_{t-1} + \omega_t \end{aligned} \]

where the errors are iid

    \[ \begin{aligned} \nu_t &\sim N(0, V_t) \\ \omega_t &\sim N(0, W_t) \\ x_0 &\sim N(m_0, C_0) \end{aligned} \]

Under this formulation there is huge flexibility and can specify a wide range of models such as high order polynomials, regression and arima models with seasonal components.

Time series analysis with the DLM package and the Kalman filter

The dlm package in r is fantastic. It is modular so you have the freedom to build models with multiple components for example you can specify a linear trend model with a quarterly seasonal component. In this example we will look at forecasting the co2 and air passenger data by specifying our own state space model. This will include applying a Kalman filter, Kalman smoothing, estimation of the parameters and finally forecasting the 6 years of observations.

We should all know the co2 data

plot of chunk unnamed-chunk-29

 

It’s a classic data set with a very obvious increasing trend and cyclic component. There is 1 observation for each month, therefore we’re looking at a 12 order seasonal component. Data like this doesn’t usually happen in the real world! It’s often much noisier and unclear what the underlying pattern is. Let’s make this more interesting by adding in a bit of noise.

plot of chunk unnamed-chunk-30

 

Much better! Firstly we’ll write a function to build the state space model.

 

If we know the parameters we can plug them straight into the model, however that’s not usually the case. Instead we estimate them using MLE.

 

It’s best practice to check the convergence of the MLE procedure. As with any MLE process we need to have appropriate initial starting points to ensure the algorithm will converge to the right maximum. The dlmMLE function calls the base R function optim so there is some flexibility in optimisation techniques if needed. These ML estimates are now passed to the model function to build the final parameterisation of the model.

 

Now we can apply a Kalman filter to the data using the dlmFilter function.

 

It’s worth spending some time with this output to understand what has been calculated. There are 3 main output components:

  1. a is the state forecast for one time step and is calculated by

        \[a_t = G_t m_{t-1}\]

    where

        \[x_t|y_{t-1} \sim N(a_t, R_t)\]

    and

        \[R_t = G_t C_{t-1} G_t^T + W_t\]

  2. f is the forecast step of the observations and is calculated by

        \[f_t = F_t a_t\]

    where

        \[y_t|y_{t-1} \sim N(f_t, Q_t)\]

    and

        \[Q_t = F_t R_{t-1} F_t^T + V_t\]

  3. m is the posterior and is calculated by

        \[m_t = R_t f_t Q_t^{-1}(y_t-f_t)\]

    and

        \[C_t = R_t - R_t F_t^T Q_t^{-1} F_t R_t\]

The a and m components have 13 columns where columns 3 to 13 are the seasonal components of the model. The first 2 columns are the linear trend components. This is convenient to know since we can easily plot the trend without the seasonal component if we wish. If we wish to plot the forecast observations we would use f. Just to convince ourselves that this is correct let’s calculate a from the output and check.

 

We can also add a smoother to the data as follows.

 

The main output here is s. Again there are 13 columns, the first 2 from the linear trend model and the last columns from the seasonal component. Plotting these together we can see the relationship and accuracy of the forecasting.

plot of chunk unnamed-chunk-37

 

As you can see this forecasts fairly well. The filtered and smoothed series are pretty similar. Here we have ignored the seasonal components and just plot the trend components to demonstrate it’s utility.

For reference the same data will be forecast using the a traditional ARIMA model.

plot of chunk unnamed-chunk-38

 

Interestingly it doesn’t pick up on the seasonal component like the state space model. We could formulate the ARIMA model manually to improve on this (usually auto.arima does a pretty good job though).

Air passenger data

The air passenger dataset is another classic. It has an obvious increasing trend and an annual seasonal cycle, therefore we can essentially use the SSM we build above, only re-estimating the parameters. We do need to apply a log transform to the data first to counter the heteroscedastic variance.

plot of chunk unnamed-chunk-39

 

Now applying the state space model as we did above for the co2 data. For this data we’ll forecast the last 2 years and compare with the observed data.

plot of chunk unnamed-chunk-40

 

The forecast series and the original observed series are very close. In this case the state space model and done very well in forecasting the time series.

Summary

In summary here we have

  1. Formulated state space models under the dynamic linear model framework where the errors are assumed to be normally distributed.
  2. Forecast a noisy co2 series with a seasonal and trend component and
  3. Forecast the air passenger data using the linear trend plus seasonal model.

The dlm package is very flexible to handle a number of different model specifications. What I really like about this package is that it’s not a black box, you are required to think about the best specification for your model and interpret the output in context of what you aim to achieve.

There is a lot more to explore with this package such as implementing a regression component, ARIMA model under a state space framework and MCMC approaches for parameter estimation. I aim to dive deeper in later posts.

Follow me on social media:

Leave a Reply

Your email address will not be published. Required fields are marked *