# dlmSmooth increases fluctuations

I am currently working with State Space models for the first time and am trying to estimate an error correction model with an unobserved I(2) process, $$\mu_t$$. I have specified a model in R using the package dlm. My model has the form:

$$\Delta s_t = \alpha_1 s_{t-1} + \alpha_2 p_{t-1} + \mu_{t} + \kappa_0 \Delta p_{t} + \kappa_1 \Delta p_{t-1} + \omega_1 \Delta s_{t-1} + \epsilon_t$$

$$\mu_{t} = \mu_{t-1} + \Delta \mu_{t}$$

$$\Delta \mu_t = \Delta \mu_{t-1} + \eta_t$$

where $$\Delta s_t, s_{t-1}, p_{t-1}, \Delta p_t, \Delta p_{t-1}, \Delta s_{t-1}$$ are data-observations, $$\alpha_1, \alpha_2, \kappa_0, \kappa_1, \omega_1$$ are constant unknown parameters and $$\mu_t$$ is an unobserved I(2) process.

I am mainly interested in estimating the unknown parameter values $$\alpha_1$$ and $$\alpha_2$$ and the unobservered process $$\mu_t$$.

Implementing the model in R, I first specify the model by the following code and obtain the filtered parameter values. Hereafter I obtain the smoothed values.

dlmmodel2 <- function(alfa) {dlm(
FF=matrix(c(1,1,-1,1,1,1,0),nr = 1),
GG=matrix(c(1,0,0,0,0,0,0,  0,1,0,0,0,0,0,  0,0,1,0,0,0,0,  0,0,0,1,0,0,0,  0,0,0,0,1,0,0,  0,0,0,0,0,1,0,  0,0,1,0,0,0,1), nr = 7),
V = alfa[1],
W =matrix(c(0,0,0,0,0,0,0,   0,0,0,0,0,0,0,  0,0,0,0,0,0,0,  0,0,0,0,0,0,0,   0,0,0,0,0,0,0,  0,0,0,0,0,0,0,  0,0,0,0,0,0,alfa[2]), nr = 7),
m0 = matrix(c(1,1,1,1,1,1,1),nr = 7),
C0 = matrix(c(0,0,0,0,0,0,0,   0,0,0,0,0,0,0,  0,0,5,0,0,0,1,  0,0,0,0,0,0,0,   0,0,0,0,0,0,0,  0,0,0,0,0,0,0,  0,0,1,0,0,0,5), nr = 7),
X=F_matrix, # F_Matrix contains the data observations (col 3 and 7 are empty)
JFF=matrix(c(1,2,0,4,5,6,0), nr=1)
)
}

#initial values
init1 <- c(2.1,1)

#model
model <- dlmMLE(y_vector,init1,dlmmodel2) #yvector contains Delta_s_t
model
parameter_results2 <- dlmmodel2(model$par) #filter and smooth filtered<- dlmFilter(y_vector, parameter_results2) smoothed <- dlmSmooth(filtered)  My problem arise as I try to obtain the smoothed values. The obtained values range from 3 e+209 to -1 e+191 though my filtered values are in a much lower range. My question is: why is my smoothed values so (numerically) large? (see output below) > plot(filtered$$m) > filtered$$m Time Series: Start = 1999 End = 2017 Frequency = 1 [,1] [,2] [,3] [,4] [,5] [,6] [,7] 1999 1.0000000 1 1.00000000 1 1.0000000 1.000000 1.00000000 2000 1.0283483 1 1.22242633 1 0.9961995 1.012915 0.54457382 2001 0.9213248 1 0.21543516 1 0.8005188 1.007066 -0.35293323 2002 0.9299682 1 0.53160130 1 0.8180312 1.007189 0.02261221 2003 0.9328907 1 1.01496822 1 0.8239033 1.007241 0.26599504 2004 0.9324971 1 1.15266894 1 0.8231204 1.007232 0.19794457 2005 0.9319144 1 0.94703263 1 0.8219661 1.007218 -0.01607025 2006 0.9314988 1 0.30847832 1 0.8211416 1.007209 -0.34454848 2007 0.9315001 1 -0.03187562 1 0.8211442 1.007209 -0.34235386 2008 0.9313951 1 -0.14680378 1 0.8210130 1.007191 -0.26428162 2009 0.9310889 1 -0.72624693 1 0.8205843 1.007147 -0.48839884 2010 0.9319146 1 0.43896849 1 0.8217332 1.007266 0.37800888 2011 0.9317131 1 0.17669692 1 0.8214527 1.007237 0.02991360 2012 0.9316768 1 -0.19409593 1 0.8214027 1.007232 -0.18156016 2013 0.9316803 1 -0.60983462 1 0.8214079 1.007232 -0.30928727 2014 0.9316748 1 -0.54629135 1 0.8214003 1.007231 -0.10740577 2015 0.9316805 1 -0.25928590 1 0.8214087 1.007232 0.10678342 2016 0.9316611 1 0.52341814 1 0.8213816 1.007229 0.48246605 2017 0.9316605 1 0.84370534 1 0.8213807 1.007229 0.39923137 > > smoothed$s
Time Series:
Start = 1999
End = 2017
Frequency = 1
[,1]           [,2]           [,3]           [,4]           [,5]           [,6]
1999   1.000000e+00   1.000000e+00 -9.466652e+231   1.000000e+00   1.000000e+00   1.000000e+00
2000  3.439619e+209  6.190332e+197  1.701729e+213  3.255033e+196  1.000790e+212 -2.029970e+211
2001 -1.013215e+191 -1.003954e+177 -7.431703e+191 -5.630027e+175 -2.226889e+191 -7.003334e+189
2002 -9.117334e+175 -6.378055e+161  1.064459e+176 -5.483632e+160 -1.467380e+176 -7.720378e+174
2003  3.317407e+159 -1.130269e+146  4.784756e+161 -1.657647e+144  6.221140e+159  9.835280e+157
2004  6.630620e+145 -2.177203e+132  9.128879e+147 -2.182155e+130  6.581938e+145  7.359108e+143
2005  7.807999e+133  1.845376e+120  4.883447e+135  5.325280e+117  9.771700e+133  1.344280e+133
2006  3.443877e+120 -1.712517e+108 -2.018727e+123 -2.481098e+105  2.988215e+120  1.008803e+120
2007 -1.940769e+108  -3.597062e+95 -7.471470e+111  -6.241203e+94 -3.494084e+108 -2.497683e+107
2008  -4.174798e+95  -2.506133e+83  -3.241887e+98   6.105502e+82  -2.704219e+96  -2.974656e+95
2009   5.170978e+84  -1.197459e+72   1.458495e+87  -5.988552e+70   6.800202e+84   9.838138e+83
2010  -1.007168e+72  -2.888476e+60   2.190879e+75   1.197968e+59  -2.637143e+72  -1.420613e+71
2011   5.463719e+60  -7.203329e+48  -6.079742e+63   5.655312e+47   3.452379e+60   2.101814e+60
2012   3.870811e+48   9.011218e+36  -7.816310e+51   3.313523e+33   6.043987e+48  -3.248771e+48
2013   1.559516e+38   7.271353e+27  -1.127496e+43  -1.021232e+26   6.176949e+38  -6.562388e+37
2014  -2.202694e+27   1.420512e+16  -2.248650e+31  -2.071048e+14   2.786972e+27  -6.745094e+26
2015   2.775447e+16   1.235690e+04   5.069440e+19  -6.768663e+02   4.426600e+16   3.470204e+15
2016  -4.283332e+04   1.000000e+00   1.373019e+05   1.000000e+00  -1.779131e+04  -4.499139e+03
2017   9.316605e-01   1.000000e+00   8.437053e-01   1.000000e+00   8.213807e-01   1.007229e+00
[,7]
1999 -6.761894e+231
2000  1.136702e+213
2001 -1.183163e+191
2002  2.204284e+175
2003  2.729704e+161
2004  3.381939e+147
2005  1.976811e+135
2006 -9.824642e+122
2007 -5.034451e+111
2008  -8.683889e+98
2009   1.006174e+87
2010   1.349258e+75
2011   1.247445e+64
2012   2.416943e+52
2013  -4.594289e+42
2014  -8.923618e+30
2015   2.568447e+19
2016  -4.045394e+04
2017   3.992314e-01
>


If I run the same code on a larger sample (1968-2017 instead of 2000-2017), I can estimate the filtered values but get the following error code when trying to obtain the smoothed values.

> smoothed <- dlmSmooth(filtered)
Error in dlmSmooth.dlmFiltered(filtered) :
error code 6 from Lapack routine dgesdd


Does anyone know whether this can be fixed in any other way than reducing the time span?

Any help would be greatly appreciated.

I cannot quite relate your code to your problem statement. When I print your GG matrix I get:

> matrix(c(1,0,0,0,0,0,0,  0,1,0,0,0,0,0,  0,0,1,0,0,0,0,  0,0,0,1,0,0,0,  0,0,0,0,1,0,0,  0,0,0,0,0,1,0,  0,0,1,0,0,0,1), nr = 7)
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    1    0    0    0    0    0    0
[2,]    0    1    0    0    0    0    0
[3,]    0    0    1    0    0    0    1
[4,]    0    0    0    1    0    0    0
[5,]    0    0    0    0    1    0    0
[6,]    0    0    0    0    0    1    0
[7,]    0    0    0    0    0    0    1


It looks like $$\mu_t$$ is in the third position of the state vector and $$\Delta\mu_t$$ in the seventh, but why there is a $$-1$$ in the third position in FF rather than a 1? It wouldn't matter (a change of sign in the latent process), but make me fear that I am misunderstanding something.

You set only two variances in V and W, but make no provision to ensure that they are non-negativa an remain so along the maximum likelihood estimation: try to replace by something like exp(alpha[1]) or alpha[2]^2 (and modify the initial values accordingly).

Don't you say that you have five unknown parameters (other than the variances of the noises that I gather are alpha[1] and alpha[2])? If you want to estimate these parameters --which you need to in order to run your filter-- you should replace them in the proper places inside function dlmmodel2. If you intend these five parameters to be the five elements of the state vector not already taken by $$\mu_t$$ and $$\Delta\mu_t$$, you should initialize them properly: it seems that you are giving them initial value 1 (in m0) and variance 0 (in C0). Is that what you intend? How were the non-zero elements in C0 chosen?

I seem to recall that I have had some trouble passing time series to some function in dlm. Try to pass plain matrices.

Thank you so much for your response.

Yes you are right that the third position in FF should be a 1 in order to match the model I have presented. This is a mistake on my behalf as I have simplified the model slightly in order to present it here.

So far, my estimates of the two variances have been positive, hence, I have not felt the need to restrict the parameters but on your recommendation I have now done so.

With regards to my five unknown parameters, you are right that I would like to obtain estimates of these. I have been unsure of how to include them properly in dlmmodel2 and hence I have chosen to include them as element 1,2,4,5,6 in my state vector. In this example I initialize them to 1, but I am continuously trying different initial values.

I see now that I have misinterpreted c0, and as my parameters are unknown c0 should in fact have a strictly positive diagonal. Changing this in my R code seems to do the trick and the two errors I previously got are now gone.