How to use DLM with Kalman filtering for forecasting Could someone walk me through an example on how to use DLM Kalman filtering in R on a time series. Say I have a these values (quarterly values with yearly seasonality); how would you use DLM to predict the next values? And BTW, do I have enough historical data (what is the minimum)?
89  2009Q1  
82  2009Q2  
89  2009Q3  
131 2009Q4  
97  2010Q1  
94  2010Q2  
101 2010Q3  
151 2010Q4  
100 2011Q1  
?   2011Q2

I'm looking for a R code cookbook-style how-to step-by-step type of answer. Accuracy of the prediction is not my main goal, I just want to learn the sequence of code that gives me a number for 2011Q2, even if I don't have enough data.
 A: I suggest you read the dlm vignette http://cran.r-project.org/web/packages/dlm/vignettes/dlm.pdf especially the chapter 3.3
A: The paper at JSS 39-02 compares 5 different Kalman filtering R packages and gives sample code.
A: DLMs are cool, but they are not as simple as, say, ARIMA or other methods. In other methods, you plug in your data and then tweak some parameters of the algorithm, perhaps referring to various diagnostics to guide your settings.
With a DLM, you are creating a state space machine, which consists of several matrices that basically implement something like a Hidden Markov Model. Some packages (sspir I think, among others) expect that you understand the concept and what the matrices do. I'd highly recommend that you start with the dlm package, and as @RockScience recommends, walk through the vignette.
With dlm you're going to basically take several steps:


*

*What kinds of components describe my series? A trend? Seasonality? Exogenous variables? You will use dlm tools like dlmModPoly to implement these components, using the + operator to join them together into one model.

*Create an R subroutine that takes however many parameters are required by this model, creates the components with those parameters, then adds them together and returns the resulting model.

*Use dlmMLE to do an search/optimization to find the appropriate parameters (using MLE, which is basically optimization, with the pitfalls that can occur in optimization). dlmMLE repeatedly calls your R subroutine with candidate parameters to create models, then tests them.

*Create your final model, using the R subroutine you created plus the parameters you found in step 3.

*Filter your data with dlmFilter, then perhaps smooth with dlmSmooth.

*If you use dlmModReg or do anything that causes the model to have time-variant parameters, you can't use dlmForecast to forecast your series. If you do end up with a time-variant model, you'll want to fill out your input data with NA's and let the dlmFilter fill in the NA's for you (a poor man's forecast), since dlmForecast does not work with time-varying parameters.

*If you want to examine the components individually (say the trend, separately from the seasonal), you'll need to understand the matrices and what's in each column, plus understand a bit of how dlm puts them together (order matters!).
There's another package, whose name escapes me, which tries to create a front end that can use several of these packages (including dlm as the back end). Unfortunately, I've never gotten it to work well, but that might just be me.
I'd really recommend getting a book on DLMs. I got a couple of them and played a lot with dlm to get to where I am, and I'm not the expert by any means.
