fks {FKF} | R Documentation |
Fast Kalman Smoother
Description
This function can be run after running fkf
to produce
"smoothed" estimates of the state variable .
Unlike the output of the filter, these estimates are conditional
on the entire set of
data points rather than only the past, see details.
Usage
fks(FKFobj)
Arguments
FKFobj |
An S3-object of class "fkf", returned by |
Details
The following notation is taken from the fkf
function descriptions
and is close to the one of Koopman et al. The smoother estimates
based on the outputs of the forward filtering pass performed by fkf
.
The formulation of Koopman and Durbin is used which evolves the two values
and
to avoid inverting the covariance matrix.
Iteration:
If there are no missing values the iteration proceeds as follows:
Initialisation: Set , with
and
.
Evolution equations:
Updating equations:
Next iteration: Set and goto “Evolution equations”.
Value
An S3-object of class "fks" which is a list with the following elements:
ahatt
A -matrix containing the
smoothed state variables, i.e. ahatt[,t] =
Vt
A -array
containing the variances of
ahatt
, i.e. Vt[,,t] =
References
Koopman, S. J. and Durbin, J. (2000). Fast filtering and smoothing for multivariate state space models Journal of Time Series Analysis Vol. 21, No. 3
Examples
## <--------------------------------------------------------------------------->
## Example: Local level model for the Nile's annual flow.
## <--------------------------------------------------------------------------->
## Transition equation:
## alpha[t+1] = alpha[t] + eta[t], eta[t] ~ N(0, HHt)
## Measurement equation:
## y[t] = alpha[t] + eps[t], eps[t] ~ N(0, GGt)
y <- Nile
y[c(3, 10)] <- NA # NA values can be handled
## Set constant parameters:
dt <- ct <- matrix(0)
Zt <- Tt <- matrix(1)
a0 <- y[1] # Estimation of the first year flow
P0 <- matrix(100) # Variance of 'a0'
## Estimate parameters:
fit.fkf <- optim(c(HHt = var(y, na.rm = TRUE) * .5,
GGt = var(y, na.rm = TRUE) * .5),
fn = function(par, ...)
-fkf(HHt = matrix(par[1]), GGt = matrix(par[2]), ...)$logLik,
yt = rbind(y), a0 = a0, P0 = P0, dt = dt, ct = ct,
Zt = Zt, Tt = Tt)
## Filter Nile data with estimated parameters:
fkf.obj <- fkf(a0, P0, dt, ct, Tt, Zt, HHt = matrix(fit.fkf$par[1]),
GGt = matrix(fit.fkf$par[2]), yt = rbind(y))
## Smooth the data based on the filter object
fks.obj <- fks(fkf.obj)
## Plot the flow data together with local levels:
plot(y, main = "Nile flow")
lines(ts(fkf.obj$att[1, ], start = start(y), frequency = frequency(y)), col = "blue")
lines(ts(fks.obj$ahatt[1,], start = start(y), frequency = frequency(y)), col = "red")
legend("top", c("Nile flow data", "Local level (fkf)","Local level (fks)"),
col = c("black", "green", "blue", "red"), lty = 1)