Skip to content
Prev 269878 / 398502 Next

Setting up a State Space Model in dlm

I just saw this old post, but it seems that nobody replied, so let me try.

If you can assume that also U[t] evelves as a random walk, I would build a
DLM by taking the state vector to be 

x[t] = (U[t], UN[t], pi[t])'

By plugging in the equation for pi[t] the random walk expressions for U[t]
and UN[t] you get the system equation of the DLM. The observation matrix F
will just be the 2 by 3 matrix that extracts components 2 and 3 from the
3-dimensional state. Set the observation variance V to a tiny multiple of
the 2 by 2 identity matrix, so that it is invertible but practically
negligible. 

I have tried some code to implement this model - here it is:

myMod <- dlm(FF = matrix(c(0, 1, 0, 0, 0, 1), 2, 3, TRUE),
             GG = matrix(c(1, 0, 0, 0, 1, 0, NA, NA, NA), 3, 3, TRUE),
             W = diag(3), # will change this in 'build' function
             V = diag(1e-7, 2),
             m0 = rep(0, 3),
             C0 = diag(1e8, 3))
R <- matrix(c(1, 0, 0, 0, 1, 0, NA, NA, 1), 3, 3, TRUE)
buildFun <- function(theta) {
    ## theta[1] : 'b'
    ## theta[2] : 'a'
    ## theta[3] : log innovation std dev of U
    ## theta[4] : log innovation std dev of UN
    ## theta[5] : log innovation std dev of pi
    GG(myMod)[3, ] <- theta[c(1, 1, 2)] * c(1, -1, 1)
    R[3, 1 : 2] <- theta[1] * c(1, -1)
    dd <- exp(theta[3 : 5])
    W(myMod) <- tcrossprod(R * rep(dd, each = 3))
    return(myMod)
}

outMLE <- dlmMLE(y = tvnairu[, c("pi", "u")], parm = c(1, 1, 0, 0, 0),
                 build = buildFun, lower = c(-Inf, -Inf, rep(-8, 3)),
                 upper = c(Inf, Inf, rep(12, 3)), 
                 control = list(trace = 1, REPORT = 5, maxit = 1000))
outMLE$par

In the estimates I get, the 'b' parameter is tiny, but this may be a local
optimum - you need to try different starting values for the optimizer.

Best,
Giovanni Petris
Michael Ash-2 wrote:
--
View this message in context: http://r.789695.n4.nabble.com/Setting-up-a-State-Space-Model-in-dlm-tp3580664p3771129.html
Sent from the R help mailing list archive at Nabble.com.