Description Usage Arguments Details Value References Examples

This function can be run after running `fkf`

to produce
"smoothed" estimates of the state variable *alpha(t)*.
Unlike the output of the filter, these estimates are conditional
on the entire set of *n* data points rather than only the past, see details.

1 | ```
fks(FKFobj)
``` |

`FKFobj` |
An S3-object of class "fkf", returned by |

The following notation is taken from the `fkf`

function descriptions
and is close to the one of Koopman et al. The smoother estimates

*a(t|n)=E[alpha(t)|y(1),...,y(n)]*

*P(t|n)=Var[alpha(t)|y(1),...,y(n)]*

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
*r(t) in R^m* and *N(t) in R^{m x m}*
to avoid inverting the covariance matrix.

**Iteration:**

If there are no missing values the iteration proceeds as follows:

Initialisation: Set *t=n*, with *r(t)=0* and *N(t)=0*.

Updating equations:

*a(t|n) = a(t|t) + P(t|t)r(t)*

*P(t|n) = P(t|t) - P(t|t)N(t)P(t|t)*

Evolution:

*L = T(t) - K(t)Z(t)*

*r(t-1) = Z(t)' F(t)^{-1} v(t) + L'r(t)*

*N(t-1) = Z(t)' F(t)^{-1} Z(t) + L' N(t) L*

Next iteration: Set *t=t-1* and goto “Updating equations”.

An S3-object of class "fks" which is a list with the following elements:

`ahatt`

A *m * n*-matrix containing the
smoothed state variables, i.e. ahatt[,t] = *a(t|n)*

`Vt`

A *m * m * n*-array
containing the variances of `ahatt`

, i.e. Vt[,,t] = *P(t|n)*

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | ```
## <--------------------------------------------------------------------------->
## 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)
``` |

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.