Understanding questions regarding the Kalman filter I have a few questions about the Kalman filter in R (dlm package):

*

*Given the function dlmFilter, there is the output time series m that contains the filtered values. Why is the time series longer than the number of observations by one value? And why, on the other hand, is the output time series a, which contains the predictions, identical in number to the number of observations?


*Why is the initial value of the state variable not corrected when I consider the output time series m of the filtered values?


*If I have 100 observations and want to calculate a prediction for time 101. What is the best way to do this? Unfortunately, I cannot use dlmForecast because I am using a time-varying matrix (JFF).
Would be great if someone could help me.
 A: I'll concentrate on the last question since you said the others were resolved.
The structure of linear Gaussian state space models allows for very easy computation of forecasts. Say we have the usual state space model:
$$\begin{align}
\theta_t &= G_t \theta_{t-1} + w_t, \quad w_t \sim \mathcal{N}(0,W_t)\\
y_t &= F_t \theta_t + v_t, \quad v_t \sim \mathcal{N}(0, V_t)
\end{align}$$
From the observation equation, since $F_t$ is known for all time, the $h$-ahead point forecast is just:
$$\mathbb{E}(y_{T+h}|y_1,...,y_T) = F_{T+h} \mathbb{E}(\theta_{T+h}|y_1,...,y_T)$$
And, from the state equation:
$$\begin{align}
\mathbb{E}(\theta_{T+h}|y_1,...,y_T) 
&= G_{T+h} \mathbb{E}(\theta_{T+h-1}|y_1,...,y_T)\\
&= G_{T+h} G_{T+h-1} \mathbb{E}(\theta_{T+h-2}|y_1,...,y_T)\\
&= ... \\
&= G_{T+h} G_{T+h-1} ... G_{T+1}\mathbb{E}(\theta_{T}|y_1,...,y_T)\\
\end{align}$$
Now, $\mathbb{E}(\theta_{T}|y_1,...,y_T)$ is just the filter mean that you obtain from the Kalman filter at the last time point.
So, all you need to do is:

*

*Take the final filtered mean from the Kalman filter

*Move the state forward by applying the transition matrices $G_{T+i}$ until you get to $T+h$

*Apply the observation matrix $F_{T+h}$ to the forecasted state

You can similarly compute the variance and derive prediction intervals and so on, if you need them.
