Use Kalman Filtering to find the (Gaussian) log-likelihood, or for forecasting or smoothing.
KalmanLike(y, mod, nit = 0L, update = FALSE)
KalmanRun(y, mod, nit = 0L, update = FALSE)
KalmanSmooth(y, mod, nit = 0L)
KalmanForecast(n.ahead = 10L, mod, update = FALSE)makeARIMA(phi, theta, Delta, kappa = 1e6,
SSinit = c("Gardner1980", "Rossignol2011"),
tol = .Machine$double.eps)
a univariate time series.
a list describing the state-space model: see ‘Details’.
the time at which the initialization is computed.
nit = 0L implies that the initialization is for a one-step
prediction, so Pn should not be computed at the first step.
if TRUE the update mod object will be
returned as attribute "mod" of the result.
the number of steps ahead for which prediction is required.
numeric vectors of length \(\ge 0\) giving AR and MA parameters.
vector of differencing coefficients, so an ARMA model is
fitted to y[t] - Delta[1]*y[t-1] - ….
the prior variance (as a multiple of the innovations variance) for the past observations in a differenced model.
a string specifying the algorithm to compute the
Pn part of the state-space initialization; see
‘Details’.
tolerance eventually passed to solve.default
when SSinit = "Rossignol2011".
For KalmanLike, a list with components Lik (the
log-likelihood less some constants) and s2, the estimate of
\(\kappa\).
For KalmanRun, a list with components values, a vector
of length 2 giving the output of KalmanLike, resid (the
residuals) and states, the contemporaneous state estimates,
a matrix with one row for each observation time.
For KalmanSmooth, a list with two components.
Component smooth is a n by p matrix of state
estimates based on all the observations, with one row for each time.
Component var is a n by p by p array of
variance matrices.
For KalmanForecast, a list with components pred, the
predictions, and var, the unscaled variances of the prediction
errors (to be multiplied by s2).
For makeARIMA, a model list including components for
its arguments.
These functions are designed to be called from other functions which check the validity of the arguments passed, so very little checking is done.
These functions work with a general univariate state-space model with state vector a, transitions a <- T a + R e, \(e \sim {\cal N}(0, \kappa Q)\) and observation equation y = Z'a + eta, \((eta\equiv\eta), \eta \sim {\cal N}(0, \kappa h)\). The likelihood is a profile likelihood after estimation of \(\kappa\).
The model is specified as a list with at least components
Tthe transition matrix
Zthe observation coefficients
hthe observation variance
VRQR'
athe current state estimate
Pthe current estimate of the state uncertainty matrix \(Q\)
Pnthe estimate at time \(t-1\) of the state
uncertainty matrix \(Q\) (not updated by KalmanForecast).
KalmanSmooth is the workhorse function for tsSmooth.
makeARIMA constructs the state-space model for an ARIMA model,
see also arima.
The state-space initialization has used Gardner et al's method
(SSinit = "Gardner1980"), as only method for years. However,
that suffers sometimes from deficiencies when close to non-stationarity.
For this reason, it may be replaced as default in the future and only
kept for reproducibility reasons. Explicit specification of
SSinit is therefore recommended, notably also in
arima().
The "Rossignol2011" method has been proposed and partly
documented by Raphael Rossignol, Univ. Grenoble, on 2011-09-20 (see
PR#14682, below), and later been ported to C by Matwey V. Kornilov.
It computes the covariance matrix of
\((X_{t-1},...,X_{t-p},Z_t,...,Z_{t-q})\)
by the method of difference equations (page 93 of Brockwell and Davis),
apparently suggested by a referee of Gardner et al (see p.314 of
their paper).
Durbin, J. and Koopman, S. J. (2001). Time Series Analysis by State Space Methods. Oxford University Press.
Gardner, G, Harvey, A. C. and Phillips, G. D. A. (1980). Algorithm AS 154: An algorithm for exact maximum likelihood estimation of autoregressive-moving average models by means of Kalman filtering. Applied Statistics, 29, 311--322. 10.2307/2346910.
R bug report PR#14682 (2011-2013) https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=14682.
# NOT RUN {
## an ARIMA fit
fit3 <- arima(presidents, c(3, 0, 0))
predict(fit3, 12)
## reconstruct this
pr <- KalmanForecast(12, fit3$model)
pr$pred + fit3$coef[4]
sqrt(pr$var * fit3$sigma2)
## and now do it year by year
mod <- fit3$model
for(y in 1:3) {
pr <- KalmanForecast(4, mod, TRUE)
print(list(pred = pr$pred + fit3$coef["intercept"],
se = sqrt(pr$var * fit3$sigma2)))
mod <- attr(pr, "mod")
}
# }
Run the code above in your browser using DataLab