1
$\begingroup$

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:

c(0.0511682865744,0.0222506089348,0.0976710271734,0.0511682865744,0.1548647105609,0.0222506089348,0.1021052229372,0.1333063673082,0.0464063728142,0.0976710271734,0.0700856075667,0.0511682865744,0.0560021901153,0.1200027923947,0.1548647105609,0.1021052229372,0.0511682865744,0.1548647105609,0.0747363461140,0.1333063673082,0.0464063728142,0.0174469136037,0.1066996186591,0.0222506089348,0.0560021901153,0.0271286673883,0.0511682865744,0.0606246218164,0.1506588762851,0.0464063728142,0.0511682865744,0.0954010847633,0.0653194661206,0.0319830458531,0.0271286673883,0.0511682865744,0.0700856075667,0.0560021901153,0.1419334958887,0.0464063728142,0.0793655553807,0.0606246218164,0.0976710271734,0.1506588762851,0.0222506089348,0.0222506089348,0.1021052229372,0.0747363461140,0.0700856075667,0.0074720148387,0.0536355491659,0.0464063728142,0.0560021901153,0.0464063728142,0.0416216746908,0.1506588762851,0.1462625069783,0.0838814839807,0.0606246218164,0.0606246218164,0.0271286673883,0.0511682865744,0.0653194661206,0.1506588762851,0.0369103540201,0.1548647105609,0.0464063728142,0.0511682865744,0.0885601769795,0.0271286673883,0.1419334958887,0.0724136805090,0.0511682865744,0.1066996186591,0.0124225199986,0.0511682865744,0.0511682865744,0.0653194661206,0.1200027923947,0.1066996186591,0.0747363461140,0.0271286673883,0.0511682865744,0.0222506089348,0.0606246218164,0.0998453349697,0.0511682865744,0.0511682865744,0.0464063728142,0.0464063728142,0.1021052229372,0.1333063673082,0.0653194661206,0.0511682865744,0.1506588762851,0.0931259779895,0.0369103540201,0.0606246218164,0.1548647105609,0.0222506089348)

$\endgroup$

1 Answer 1

1
$\begingroup$

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.

$\endgroup$
7
  • $\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$ Commented Oct 12, 2018 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
    Commented Oct 12, 2018 at 4:53
  • $\begingroup$ Thanks, I've added a test dataset to my original topic now :) $\endgroup$ Commented Oct 12, 2018 at 5:03
  • $\begingroup$ Sorry, I just updated the dataset to hopefully be a bit better now $\endgroup$ Commented Oct 12, 2018 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
    Commented Oct 17, 2018 at 3:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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