Returns both the predicted and filtered values for various linear state space models; it also evaluates the likelihood at the given parameter values. This script replaces Kfilter0, Kfilter1, and Kfilter2
Kfilter(y, A, mu0, Sigma0, Phi, sQ, sR, Ups =NULL, Gam =NULL, input =NULL, S =NULL, version =1)
Arguments
y: data matrix (n x q), vector or time series, n = number of observations. Use NA or zero (0) for missing data.
A: can be constant or an array with dimension dim=c(q,p,n) if time varying (see details). Use NA or zero (0) for missing data.
mu0: initial state mean vector (p x 1)
Sigma0: initial state covariance matrix (p x p)
Phi: state transition matrix (p x p)
sQ: state error pre-matrix (see details)
sR: observation error pre-matrix (see details)
Ups: state input matrix (p x r); leave as NULL (default) if not needed
Gam: observation input matrix (q x r); leave as NULL (default) if not needed
input: NULL (default) if not needed or a matrix (n x r) of inputs having the same row dimension (n) as y
S: covariance matrix between the (not premultiplied) state and observation errors; not necessary to specify if not needed and only used if version=2. See details for more information.
version: either 1 (default) or 2; version 2 allows for correlated errors
Details
This script replaces Kfilter0, Kfilter1, and Kfilter2 by combining all cases. The major difference is how to specify the covariance matrices; in particular, sQ = t(cQ) and sR = t(cR) where cQ and cR were used in Kfilter0-1-2 scripts.
The states xt are p-dimensional, the data yt are q-dimensional, and the inputs ut are r-dimensional for t=1,…,n. The initial state is x0∼N(μ0,Σ0).
The measurement matrices At can be constant or time varying. If time varying, they should be entered as an array of dimension dim = c(q,p,n). Otherwise, just enter the constant value making sure it has the appropriate q×p dimension.
where wt⊥vt. Consequently the state noise covariance matrix is Q=sQsQ′ and the observation noise covariance matrix is R=sRsR′ and sQ,sR do not have to be square as long as everything is conformable. Notice the specification of the state and observation covariances has changed from the original scripts.
NOTE: If it is easier to model in terms of Q and R, simply input the square root matrices sQ = Q %^% .5 and sR = R %^% .5.
Version 2 (correlated errors): The general model is
The webpages for the texts and some help on using R for time series analysis can be found at https://nickpoison.github.io/.
Note
Note that Kfilter is similar to Kfilter-0-1-2 except that only the essential values need to be entered (and come first in the statement); the optional values such as input are set to NULL by default if they are not needed. This version is faster than the older versions. The biggest change was to how the covarainces are specified. For example, if you have code that used Kfilter1, just use sQ = t(cQ) and sR = t(cR) here.
NOTE: If it is easier to model in terms of Q and R, simply input the square root matrices sQ = Q%^%.5 and sR = R%^%.5.
Author(s)
D.S. Stoffer
See Also
Ksmooth
Examples
# generate some data set.seed(1) sQ =1; sR =3; n =100 mu0 =0; Sigma0 =10; x0 = rnorm(1,mu0,Sigma0) w = rnorm(n); v = rnorm(n) x = c(x0 + sQ*w[1]); y = c(x[1]+ sR*v[1])# initializefor(t in2:n){ x[t]= x[t-1]+ sQ*w[t] y[t]= x[t]+ sR*v[t]}# run and plot the filter run = Kfilter(y, A=1, mu0, Sigma0, Phi=1, sQ, sR)tsplot(cbind(y,run$Xf), spaghetti=TRUE, type='o', col=c(4,6), pch=c(1,NA), margins=1)# CRAN tests need extra white space :( so margins=1 above is not necessary otherwiselegend('topleft', legend=c("y(t)","Xf(t)"), lty=1, col=c(4,6), bty="n", pch=c(1,NA))