# DLM package, issues about specifying models with time-varying coefficient

I've been working on DLM package for the past few weeks. I've read the package manual and the paper written by Petris "dlm: an R package for Bayesian analysis of Dynamic Linear Models", but I am still not sure about how to setup a dlm object for my model, stated as follows:

I've got data for St, At, Wt and Xt, and I am interested in estimate three state variables (which are considered as time-varying) alpha_t, beta_t and gamma_t as well as one time-invariant coefficient c. Forget about the c*Xt part for now, and to simplify the problem, I consider alpha_t, beta_t and gamma_t as random walk. Then my R code for the model setup is:

I have four variables to be estimated, which are the variances for the observation error and state error. My first question: Is this the right specification for my model ? (ad, WOMvolume,interaction are just the data)

If so when I call fit <- dlmMLE(sale,parm=rep(0,4),build=buildFun), it returned error "Error in storage.mode(mod[[i]]) <- "double" " why is that?

I also tried another way to setup the model, like

This time, when I called fit <- dlmMLE(sale,parm=rep(0,4),build=buildFun), it return the error "Error in dlmLL(y = y, mod = mod, debug = debug) : error code 2 from Lapack routine dgesdd" why is that?

I am really not sure about how to setup my model using DLM. Any help would be highly appreciated. Thanks for your help in advance :)

(response to F. Tusell)

After some tryouts, I think I figured out how to setup my model using dlm(). Here is the code.

But when I called dlmMLE function like fit <- dlmMLE(sale,parm=rep(1,4),build=buildFun), I got an error stating "error code 2 from Lapack routine dgesdd". And I googled for the error, and it turns out that error code 1 from Lapack routine dgessdd should be a convergence problem. Is there any way get around the issue? Or does that mean my model does not have a solution? I am using R 3.2.0 on Mac OS X 10.9.5.

Any help would be most welcome!

(2nd edit in response to F.Tusell)

I am working on some equation combining both time-variant and time-invariant coefficients. Say I have this,

y, X1 and X2 are observed time-series data. Two coefficients are of interest here, one is time varying and the other is constant. Based on your previous replies, I setup my dlm like this,

And I can get the state vector by "model3Filter\$m". But it does not seem right since the second state variable I get is changing instead of constant. Anything wrong with my construction? Maybe FF or GG ?

Appreciate so much for your help.

Your second attempt defines an entirely different model than the first. In your first attempt you are creating an object in which V and W are not matrices, as they should be. Better use the constructor function dlm that will take care of those details for you, rather than using list.

• @Tusell, thanks for your comments, and I tried to use dlm(), which is dlm(m0=rep(0,4), C0=100000*diag(3), JFF=matrix(1:3,1,3), GG=diag(3), X=cbind(ad,WOMvolume, interaction), V=x[1], W=diag(c(x[2],x[3],x[4]))). But I got an error saying that FF is missing, but in my case FF is time-varying, I should use JFF instead, why is that? – Tracy Yang Jun 13 '15 at 1:31
• @Tusell, BYW, I think V should be a scalar rather than a matrix. Is that like V is designed to be a matrix? then I should set V = matrix(x[1])? – Tracy Yang Jun 13 '15 at 1:34
• After some tryouts, I think I figured out how to setup my model using dlm(). (see my question for details) – Tracy Yang Jun 13 '15 at 2:32
• You can pass it to dlm() as an scalar, dlm will take care. – F. Tusell Jun 15 '15 at 7:23
• One thing you could try is to parameterize in terms of squares (or exponentials). For instance, instead of V=x[1] above, V=exp(x[1]). You should realize that under dlm there is a call to a routine that maximizes the likelihood, but it is completely unaware of what sort of function that is. It migth well choose values of x[1] negative, which do not make sense as variances, and abort the process. Similarly for W, you must ensure it always remains non-negative definite. – F. Tusell Jun 15 '15 at 7:28

The coefficient is meant to be constant; but remember the Kalman filter performs estimation at every point in time,so the estimates of the coefficient will vary over time. This should cause no surprise: if you perform ordinary linear regression with constant coefficients with, say, the first half of the sample and then with the whole sample, your estimates of the constant coefficients will also be diferent. Similar thing happens here.

• I am working on some equation combining both time-variant and time-invariant coefficients. See the second update to my question. – Tracy Yang Aug 17 '15 at 17:42
• oh, I see. But then what should be used as estimate of beta (in a constant sense) if I only wanna one value to represent the coefficient? – Tracy Yang Aug 18 '15 at 19:50
• You probably want the last value, which makes use of the whole sample. – F. Tusell Aug 21 '15 at 10:20