Good afternoon,

I am attempting to fit a state space model of the form: $$ (S_t- \mu) = G*(S_{t-1} - \mu) + E_t $$ $$ Y = F*S_t + v_t $$ Where $Y$ is nx1, $G$ is 3x3, $S_t$ is 3x1, $\mu$ is 3x1, and $F$ is 17x3. For the life of me I cannot figure out how to define this in the DLM package in R.

I have tried making $G$ 6X6 (and therefore $S_t$ 6x1) and including in the appropriate column and row to subtract off the $\mu$ and then selecting the appropriate variable with $F$ but I am getting nonsensical results after filtering. i.e all the $\mu$s are 0; whereas they ought to have absolute values some distance from 0.

Any insights would be appreciated.


Solving for the parameters: $$ (S_t- \mu) = G*(S_{t-1} - \mu) + E_t $$ $$ S_t = \mu + G*(S_{t-1} - \mu) + E_T $$ $$ S_t = \mu - G* \mu + G*S_{t-1} + E_T $$ $$ S_t = (I - G) *\mu + G*S_{t-1} + E_T $$

Combining into one single G matrix: $$ \begin{bmatrix} g_{11} & g_{12} & g_{13} & (1-g_{11}) & -g_{12} & -g_{13}\\ g_{21} & g_{22} & g_{23} & -g_{21} & (1-g_{22}) & -g_{23}\\ g_{31} & g_{32} & g_{33} & -g_{31} & -g_{32} & (1-g_{33})\\ 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 \\ \end{bmatrix} $$

And: $$ S_t = \begin{bmatrix} S_{1t}\\ S_{2t}\\ S_{3t}\\ \mu_1\\ \mu_2\\ \mu_3\\ \end{bmatrix} $$


#Build the DLM model
model <- function(param){

#Define the F Matrix
F.mat <-matrix(rep(0,6*17),nr=17)
F.mat[,1] <-1
dimf <-dim(F.mat)
for (i in 1:dim(F.mat)[1]){
for (j in 2:3){
F.mat[i,j] <- f_fun(i,j,param[1])}
#F.mat[,4:6] = F.mat[,1:3]

#V matrix
V.mat <- diag(var_rest(param[2:18]))

#Now G. We need to define the intercepts here
G.mat <- matrix(rep(0,6*6),nr=6)
G.mat[4,4] <- 1
G.mat[5,5] <- 1
G.mat[6,6] <- 1

param[19] <-coef_rest(param[19])
G.mat[1:3, 1:3] <- matrix(param[19:27],nrow =3, ncol = 3, byrow = TRUE)
G.mat[1:3, 4:6] <- -G.mat[1:3,1:3]

#Finally, W
W.mat <-matrix(rep(0,6*6),nrow=6)
param[c(28,31,33)] <- var_rest(param[c(28,31,33)])
W.mat[1,1:3] <-param[28:30]
W.mat[2,1:3] <-param[c(29,31,32)]
W.mat[3,1:3] <-param[c(30,32,33)]
W.mat[4,4] <- 1e-7
W.mat[5,5] <- 1e-7
W.mat[6,6] <- 1e-7

#And now the initial states
m0.mat <- matrix(rep(0,6),nrow=6)

#C0.mat <- matrix(rep(0,36),nrow=6)
C0.mat <- diag(rep(1e-7,6))
C0.mat[1:3,1:3] <- 10^7

The variance restiction is exponential, and the param restriction is ensuring that it's stable. The helper function for F just calculates a bunch of values based upon the parameter.

Which is then passed to dlmMLE with starting parameters. This is the latest iteration but L-BFGS-B complains about finite values.

  • $\begingroup$ How is $S_t$ 3x1? Do you only have 3 observations? $\endgroup$
    – Aksakal
    Apr 15, 2014 at 18:35
  • $\begingroup$ St is from the transition equation whereing 3 equations are defined. F is 17x3, so it amounts to a dynamic factor model where Y is the dependent variable with dimensions 17x1. I ought to have specified! $\endgroup$ Apr 15, 2014 at 18:56
  • $\begingroup$ It seems straight forward, can you paste your code? $\endgroup$
    – Aksakal
    Apr 15, 2014 at 18:58
  • $\begingroup$ Who or what is TIA? Is this information relevant to the question? $\endgroup$ Mar 1, 2015 at 17:21

1 Answer 1


If you set the last three terms of m0 equal to zero and the variances in C0 and W equal to 10^-7, you don't give $\mu$ much of a chance to take off from zero. Not surprising that they come out as zero.

  • $\begingroup$ Thank you very kindly Mr. Tussell. I've tried modifying those paramaters as suggested but still am not obtaining the necessary results. $\endgroup$ Apr 16, 2014 at 1:56
  • $\begingroup$ So what are the symptoms, now? $\endgroup$
    – F. Tusell
    Apr 16, 2014 at 7:52
  • $\begingroup$ If I set their respective elements of C0 to 1e7, mu now comes out at values resembling what I am expecting - but in the process all other values are pushed far away from what they ought to be. It is worth mentioning that I had G wrong, from my understanding G[4:6] = Identity - G[1:3] to solve properly for the coefficients. $\endgroup$ Apr 16, 2014 at 14:56
  • 1
    $\begingroup$ G[4:6] is rows 4:6 and columns 4:6 of G? It is up to you to fix G; you could endow your system with a random walk dynamics, an AR dynamics or whatever. Nothing is wrong "a priori". You might want to print your matrices, rather than give the code, and state your assumptions, so other people can check whether assumptions and specification match. $\endgroup$
    – F. Tusell
    Apr 16, 2014 at 15:46
  • 1
    $\begingroup$ OK. If the $mu$'s are the means over time of the factor series (this looks like a dynamic factor analysis) and you could center your observations, so everything has zero mean, your life would be easier. Other than that, the only thing I can suggest without looking at the data is to try different initial values for the parameters. The likelihood of these models is frequently plagued with local maxima and/or maxima at the boundaries of the feasible region. Very easy to end nowhere if you start at the wrong place. $\endgroup$
    – F. Tusell
    Apr 16, 2014 at 18:12

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.