# [R] Help with kalman-filterd betas using the dlm package

spencerg spencer.graves at prodsyse.com
Fri May 15 14:11:22 CEST 2009

1.  Might you look again at section "2.  Maximum likelihood
estimation" of the "dlm" vignette?  It describes how to estimate
parameters.

2.  Have you started with the code on those 2 pages, confirming
that you can make that work and understand what it does?  If yes, then
try to build code for your problem as a series of small modifications to
that example.  With luck, this will bring enlightenment.  If not, try to
express your question in terms of commented, minimal, self-contained
code that others can copy into R to replicate what you see then modify
to get it to work, as suggested in the posting guide
"www.R-project.org/posting-guide.html".  If someone reading this list
can do this in a few seconds, it will increases the chances that you

Hope this helps.
Spencer Graves

tom81 wrote:
> I have studied both the vinguette and other material I've been able to get my
> hands on and Im starting to get a better understanding. And I'm defenitly
> going to buy Petris, Petrone, and Campagnoli (2009) Dynamic Linear Models
> with R. But that's not publish yet so I 'm not getting much help there.
>
> This is the set-up i am using
> y[t] = a[t] + b*x[t] + V[t],
> a[t] = a[t-1] + W[t,a]
> b[t] = b[t-1] + W[t,b]
>
> V[t] ~ N(0,V)
> W[t] ~ N(0,W)
> W = blockdiag(W[a],W[b])
>
>
> V could be estimated from the data with a non-diagonal variance matrix of
> the returns,
> W would be the same estimated in the same way but where the effect of past
> betas in the transition taken into account. But how do I estimate that
> matrix, is that done with a MLE,SUR or some other statistical teqnique.
>
> Im also assuming in this example that a[t] are time invariant, which gives
> W[a] = 0
>
> Appriciate any guidence.
> Regards Tom
> "
>
> spencerg wrote:
>
>>       Have you worked through "vignette('dlm')"?  Vignettes are nice
>> because they provide an Adobe Acrobat Portable Document Format (pdf)
>> file with a companion R script file, which you can get as follows:
>>
>>
>> (dlm. <- vignette('dlm'))
>> Stangle(dlm.\$file)
>>
>>
>>       The first of these two lines opens the "pdf" file.  The second
>> creates a file "dlm.R" in the working directory (getwd()) containing the
>> R commands discussed in the "pdf" file.
>>
>>
>>       If I remember correctly, your question is answered in this vignette.
>>
>>
>>       You may also be interested in a book that is soon to appear about
>> this package:  Petris, Petrone, and Campagnoli (2009) Dynamic Linear
>> Models with R (Springer;
>> http://www.amazon.com/Dynamic-Linear-Models-R-Use/dp/0387772375/ref=sr_1_4?ie=UTF8&s=books&qid=1242162708&sr=1-4),
>> scheduled to ship in late June.  If you have long-term interest in this
>> subject, as I suspect you may, you might find this book interesting and
>> useful.
>>
>>
>>       Hope this helps.
>>       Spencer Graves
>> "
>> tom81 wrote:
>>
>>> Hi all R gurus out there,
>>> Im a kind of newbie to kalman-filters after some research I have found
>>> that
>>> the dlm package is the easiest to start with. So be patient if some of my
>>> questions are too basic.
>>>
>>> I would like to set up a beta estimation between an asset and a market
>>> index
>>> using a kalman-filter. Much littarture says it gives superior estimates
>>> compared to OLS estimates. So I would like to learn and to use the
>>> filter.
>>>
>>> I would like to run two types of kalman-filters, one with using a
>>> random-walk model (RW) and one with a stationary model, in other worlds
>>> the
>>> transition equition either follow a RW or AR(1) model.
>>>
>>> This is how I think it would be set up;
>>>
>>> I will have my time-series Y,X, where Y is the response variable
>>>
>>> this setup should give me a RW process if I have understood the example
>>> correctly
>>> mydlmModel = dlmModReg(X)  + dlmModPoly(order=1)
>>>
>>> and then run on the dlm model
>>> dlmFilter(Y,mydlmModel )
>>>
>>> but setting up a AR(1) process is unclear, should I use dlmModPoly or the
>>> dlmModARMA to set up the model.
>>>
>>> And at last but not the least, how do I set up a proper build function to
>>> use with dlmMLE to optimize the starting values.
>>>
>>> Regards Tom
>>>
>>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help