7
$\begingroup$

The R package dlm implements filtering and smoothing (dlmFilter and dlmSmooth) for models with regression effects, but forecasting is not (yet) available for these models:

mod <- dlmModSeas(4)+dlmModReg(cbind(rnorm(100),rnorm(100)))
fi <- dlmFilter(rnorm(100),mod)
f <- dlmForecast(fi,nAhead=12)
Error in dlmForecast(fi, nAhead = 12): 
  dlmForecast only works with constant models

How can I do this in R?

Thanks for your help!

$\endgroup$
6
$\begingroup$

Here's the solution I came up with: The trick is to add NAs to the end of the observation data. When seeing NA as a response variable the Kalman filter algorithm will simply predict the next value and not update the state vector. This is exactly what we want to make our forecast.

nAhead <- 12
mod <- dlmModSeas(4)+dlmModReg(cbind(rnorm(100+nAhead),rnorm(100+nAhead)))
fi <- dlmFilter(c(rnorm(100),rep(NA,nAhead)),mod)

Is this correct?

$\endgroup$
  • $\begingroup$ Good idea, and it basically works. I suspect that there's a tradeoff of some sort -- otherwise the dlm folks could simply use that solution to code dlmForecast -- but it does basically work. $\endgroup$ – Wayne Sep 2 '11 at 18:53
  • $\begingroup$ How do I get updated forecast for dlmModReg with new data? If I re-run dlmFilter, it will re-estimate my model parameters. $\endgroup$ – user116555 May 23 '16 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy