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.
TIA
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} $$
Code:
#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.