# Access Kalman filter generated process state estimates online (mxExpectationStateSpace)

Posted on

janneadolf
Joined: 02/06/2015

Forums

I am using the Kalman filter implementation (mxExpectationStateSpace) to fit a time-discrete vector auto-regressive model to single subjects' multivariate time series. If I'm not mistaken, the Kalman filter generates (predicted and updated) process state estimates for each but the first time point. Is it possible to pull these state estimates out online while the data are being filtered?

This would seem to provide the opportunity to include interactions between the (latent) processes over time. Currently, I am allowing the parameters of my bivariate process model (e.g., the auto- and cross-regressive effects) to be moderated by a third observed time-varying variable which I include as fixed. Therefore, temporal dependencies in this third variable, measurement error etc. cannot be accounted for. It would be much more convenient if it could be treated as a (latent) random variable.

This would seem to provide the opportunity to include interactions between the (latent) processes over time. Currently, I am allowing the parameters of my bivariate process model (e.g., the auto- and cross-regressive effects) to be moderated by a third observed time-varying variable which I include as fixed. Therefore, temporal dependencies in this third variable, measurement error etc. cannot be accounted for. It would be much more convenient if it could be treated as a (latent) random variable.

Thanks, Janne

PS: Looking forward to the implementation of thresholds for discrete data!

## Options

I'm glad to see this is getting some use by people other than me! You understand correctly that the Kalman filter is internally producing a predicted and updated latent state estimate for each time point. This is including the first time point; that's why you specify the latent state at the zeroth time point.

Interactions could be handled in one of several ways. First, interactions could exist to the extent allowed by the linear mapping. That is, if you have two latent states and one predicts the other at the next time point then that is a kind of interaction or coupling. This first kind of interaction is handled as cross-regression coefficients: off-diagonal elements of the A (dynamics) matrix. Second, interactions could exist as the contemporaneous version of the coupling just described. This entails off-diagonal elements of the latent residual matrix Q. Third, an interaction could be an actual product of latent variables.

x1[t] = (.5 * x2[t-1]) * x1[t-1]

x2[t] = .1 * x1[t-1] + .8 * x2[t-1]

Here the autoregressive parameter for x1 depends on x2. The mapping for x2 is linear (a linear combination of x1 and x2), but the mapping for x1 is nonlinear. So it's a nonlinear state space model. A nonlinear state space model and requires an Extended Kalman Filter (or an unscented Kalman filter), not the classical Kalman filter that I've implemented. The extended Kalman filter is a feature I plan on implementing and I have an R code prototype that works, but it may be a few months before I get to it.

So, the first two ways of handling interactions work fine. The third way requires a nonlinear state space model which is not yet implemented.

The extended Kalman filter can be very handy for time-varying parameters. A quick note about your situation and one like it is attached.

Log in or register to post comments

In reply to Options by mhunter

## clarification and initial request

thanks for your quick and pardon my slow reply:

I am especially interested in the non-linear variant of an interaction/potential multiplicative effects to get at time-variability in model parameters coupled to a third variable (we have been emailing about this).

I figure that if time-variability in model parameters is a) deterministic in the sense that it entirely depends on a time-varying covariate without the need to include a stochastic error term (like in equation 2 above and unlike in Molenaar, Sinclair, Rovine, Ram, & Corneal, 2009) and if b) the time-varying covariate is known/fixed, it is technically relatively simple to account for the non-linearity and render the model linear again. My current approach involves conditioning upon the time-varying covariate by including it as a definition variable which potentially moderates the model parameters.

I then was uncertain to which extent we can flexibilize the notion of a *fixed* covariate in the context of this approach and, specifically, whether we can treat the covariate as both fixed and random at the same. The vague idea was that the Kalman filter generated state-estimates could re-enter the model as fixed values of a definition variable. But apparently, the situation as formalized in the above equations requires a more sophisticated technical approach (and I'm afraid I do not really follow the notes you attached).

Anyway, is it possible to pull out the generated state estimates on- or offline?

Greetings.

Log in or register to post comments

In reply to clarification and initial request by janneadolf

## Yep

You can get Kalman predicted, updated, and smoothed latent state estimates from mxKalmanScores() in the latest *source* version of OpenMx. These will also be available in the next binary release of OpenMx.

We could go over any details of the notes via video chat if you wanted. It would really be no trouble.

The point of the notes I attached previously was just to demarcate the line between was is currently possible with linear Kalman filters and what would require a nonlinear Kalman filter (e.g. the extended or unscented Kalman filter). You can have the dynamics be equal to a covariate, or any linear combination of covariates. You can have the dynamics be the result of a regression with freely estimated parameters. In the one-dimensional case, you can define

A = b0 + b1*u1

where A is a time-varying dynamics matrix, b0 is the intercept term in a regression, b1 is a slope term in a regression, and u1 is a time-varying covariate. This is possible with the current implementation in OpenMx and does not violate any assumptions (provided that the residuals in the regression do not correlate with the latent states and do not have an autocorrelation structure of their own).

If, on the other hand, u1 is not a measured covariate, but rather is a latent variable, then the same model would require a nonlinear Kalman filter. The rule is if latent variables multiply together then a nonlinear filter is needed.

Log in or register to post comments