I'm having a bit of difficulty estimating parameters in DLM in R and I was wondering if I could get a bit of help with it. I have a system of equations given as:

$p_{t} = m_{t} + s_{t}$

$m_{t} = m_{t-1} + w_{t}$

$s_{t} = \phi_{1}s_{t-1} + \phi_{2}s_{t-2} + \epsilon_{t}$

which can be written in state space form as follows:

Observation Equation:

$p_{t} = \begin{bmatrix} 1 & 1 & 0 \end{bmatrix} \begin{bmatrix} m_{t}\\ s_{t} \\ s_{t-1}\end{bmatrix}$

State Equation:

$ \begin{bmatrix} m_{t}\\ s_{t} \\ s_{t-1}\end{bmatrix} = \begin{bmatrix} 1 & 0 & 0 \\ 0 & \phi_{1} & \phi_{2}\\ 0 & 1 & 0\end{bmatrix}\begin{bmatrix} m_{t-1}\\ s_{t-1} \\ s_{t-2}\end{bmatrix} + \begin{bmatrix} 1 & 0\\ 0 & 1 \\ 0 & 0\end{bmatrix}\begin{bmatrix} w_{t} \\ \epsilon_{t}\end{bmatrix}$

And I would like to find the parameters $\phi_{1}, \phi_{2}, \sigma^{2}_{w}, \sigma^{2}_\epsilon$

To this end, I've written the following code in DLM package in R:

buildModelDLM = function(p) {

     # This part evaluates the four parameters

     phi1 <- p[[1]] # This evaluates phi values in T matrix
     phi2 <- p[[2]] # This evaluates phi values in T matrix
     dW1 <- exp(p[3]) # This evaluates the variance of the first error term
     dW2 <- exp(p[4]) # This evaluates the variance of the second error term

     # This part defines the remaining matrices and initial conditions

     GG = matrix(c(1,0,0,0,phi1,phi2,0,1,0),3,3,byrow=T)
     W = diag(c(dW1, dW2))
     FF = matrix(c(1,1,0),1,3)
     C0 = 10^7 *diag(3)

     return( list( m0=rep(0,4),C0=C0, FF=FF, GG=GG, W=W, V=0) )

mle <- dlmMLE(HFTsubset$lmid, parm=rep(0,4), build = buildModelDLM, method="BFGS")

However, the maximum likelihood estimator only seems to be estimating one parameter and I'm not sure what I'm doing wrong, so if someone could help me with this I'd really appreciate it, thanks in advance. I also don't mind if there's another way to do this in another R package, but I would prefer to work with DLM (or maybe MARSS) if possible

Here's a test dataset:



Try this. You had a few matrices of the wrong size.

buildModelDLM <- function(p) {
  phi1 <- p[1]
  phi2 <- p[2]
  dW1 <- exp(p[3])
  dW2 <- exp(p[4])

  GG <- matrix(c(1,0,0,0,phi1,phi2,0,1,0),3,3,byrow=T)
  W <- diag(c(dW1, dW2, 0))
  FF <- matrix(c(1,1,0),1,3)
  C0 <- 10^7 *diag(3)

  return( list(m0=rep(0,3),C0=C0, FF=FF, GG=GG, W=W, V=0) )

Warning: The function dlmMLE calls dlmLL to evaluate the log-likelihood. The documentation to dlmLL warns: "[t]he observation variance V in mod must be nonsingular." Yours is singular, so you should use another function.

  • $\begingroup$ Thanks, I've fixed up a few of the things you mentioned now. I'd like to work out the two variances in the W matrix (which is basically the matrix that has the two error terms) so I put that into my function (the two variances are denoted by dW1 and dW2). Sorry, could you explain what you mean by the second argument needing to be a vector? Isn't rep(0,4) a vector already? Thanks! $\endgroup$ – ThePlowKing Oct 12 '18 at 2:45
  • 1
    $\begingroup$ @ThePlowKing I could probably figure it out a little quicker if you posted a little test data set. I haven't used dlm in a while, so I'm a little fuzzy on the notation $\endgroup$ – Taylor Oct 12 '18 at 4:53
  • $\begingroup$ Thanks, I've added a test dataset to my original topic now :) $\endgroup$ – ThePlowKing Oct 12 '18 at 5:03
  • $\begingroup$ Sorry, I just updated the dataset to hopefully be a bit better now $\endgroup$ – ThePlowKing Oct 12 '18 at 5:11
  • 1
    $\begingroup$ Yes, I do, but I think there might be a problem. The function dlmMLE calls dlmLL to evaluate the log-likelihood. The documentation to dlmLL warns: "[t]he observation variance V in mod must be nonsingular." Yours is singular, so you should use another function. I picked up on this after I added the debug=TRUE argument to dlmMLE. $\endgroup$ – Taylor Oct 17 '18 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.