Last data update: 2014.03.03

R: Disturbance Smoother for State Space Models
DSR Documentation

Disturbance Smoother for State Space Models

Description

These functions run the disturbance smoother upon the output from the Kalman filter and smoother.

Usage

DS(y, ss, kf, ks)
DS.deriv(ss, ksd)

Arguments

y

a numeric time series or vector.

ss

a list containing the matrices of the state space model.

kf

a list containing the output returned by the function KF.

ks

a list containing the output returned by the function KS.

ksd

a list containing the output returned by the function KS.deriv.

Details

See the details section and the section ‘state space representation’ in KF.

Value

DS returns a list containing the following elements:

epshat

smoothed estimate of the disturbance term in the observation equation.

vareps

error variance of epshat.

etahat

smoothed estimate of the disturbance term(s) in the state equation.

vareta

error variance of etahat.

DS.deriv returns a list containing the derivatives of the elements above named respectively depshat, dvareps, detahat and dvareta. The derivatives are summed over all the observations.

References

Durbin, J. and Koopman, S. J. (2001). Time Series Analysis by State Space Methods. Oxford University Press.

Harvey, A. C. (1989). Forecasting, Structural Time Series Models and the Kalman Filter. Cambridge University Press.

See Also

KF, KS; char2numeric in package stsm.

Examples

# local level plus seasonal model with arbitrary parameter values
# for the 'JohnsonJohnson' time series
m <- stsm::stsm.model(model = "llm+seas", y = JohnsonJohnson, 
  pars = c("var1" = 2, "var2" = 15, "var3" = 30))
ss <- stsm::char2numeric(m)

kf <- KF(m@y, ss)
ks <- KS(m@y, ss, kf)
ds <- DS(m@y, ss, kf, ks)
acf(ds$epshat, main = "ACF of smoothed disturbance")

kfd <- KF.deriv(m@y, ss)
ksd <- KS.deriv(m@y, ss, kfd)
dsd <- DS.deriv(ss, ksd)

# compare analytical and numerical derivatives
fcn <- function(x, model, type, i = 1)
{
  m <- stsm::set.pars(model, x)
  ss <- stsm::char2numeric(m)
  kf <- KF(m@y, ss)
  ks <- KS(m@y, ss, kf)
  ds <- DS(m@y, ss, kf, ks)
  
  switch(type,
    "epshat" = sum(ds$epshat),
    "vareps" = sum(ds$vareps))
}

d <- numDeriv::grad(func = fcn, x = m@pars, model = m, type = "epshat")
all.equal(d, dsd$depshat)

d <- numDeriv::grad(func = fcn, x = m@pars, model = m, type = "vareps")
all.equal(d, dsd$dvareps)

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(KFKSDS)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/KFKSDS/DS.Rd_%03d_medium.png", width=480, height=480)
> ### Name: DS
> ### Title: Disturbance Smoother for State Space Models
> ### Aliases: DS DS.deriv
> ### Keywords: ts, model
> 
> ### ** Examples
> 
> # local level plus seasonal model with arbitrary parameter values
> # for the 'JohnsonJohnson' time series
> m <- stsm::stsm.model(model = "llm+seas", y = JohnsonJohnson, 
+   pars = c("var1" = 2, "var2" = 15, "var3" = 30))
> ss <- stsm::char2numeric(m)
> 
> kf <- KF(m@y, ss)
> ks <- KS(m@y, ss, kf)
> ds <- DS(m@y, ss, kf, ks)
> acf(ds$epshat, main = "ACF of smoothed disturbance")
> 
> kfd <- KF.deriv(m@y, ss)
> ksd <- KS.deriv(m@y, ss, kfd)
> dsd <- DS.deriv(ss, ksd)
> 
> # compare analytical and numerical derivatives
> fcn <- function(x, model, type, i = 1)
+ {
+   m <- stsm::set.pars(model, x)
+   ss <- stsm::char2numeric(m)
+   kf <- KF(m@y, ss)
+   ks <- KS(m@y, ss, kf)
+   ds <- DS(m@y, ss, kf, ks)
+   
+   switch(type,
+     "epshat" = sum(ds$epshat),
+     "vareps" = sum(ds$vareps))
+ }
> 
> d <- numDeriv::grad(func = fcn, x = m@pars, model = m, type = "epshat")
> all.equal(d, dsd$depshat)
[1] "Mean relative difference: 9.623615e-06"
> 
> d <- numDeriv::grad(func = fcn, x = m@pars, model = m, type = "vareps")
> all.equal(d, dsd$dvareps)
[1] TRUE
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>