Rev 89613 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/stats/man/KalmanLike.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2025 R Core Team% Distributed under GPL 2 or later\name{KalmanLike}\alias{KalmanLike}%-> ../R/Kalman.R\alias{KalmanRun}\alias{KalmanSmooth}\alias{KalmanForecast}\alias{makeARIMA}%-> ../R/arima.R\title{\I{Kalman} Filtering}\description{Use \I{Kalman} Filtering to find the (Gaussian) log-likelihood, or forforecasting or smoothing.}\usage{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)}\arguments{\item{y}{a univariate time series.}\item{mod}{a list describing the state-space model: see \sQuote{Details}.}\item{nit}{the time at which the initialization is computed.\code{nit = 0L} implies that the initialization is for a one-stepprediction, so \code{Pn} should not be computed at the first step.}\item{update}{if \code{TRUE} the update \code{mod} object will bereturned as attribute \code{"mod"} of the result.}\item{n.ahead}{the number of steps ahead for which prediction isrequired.}\item{phi, theta}{numeric vectors of length \eqn{\ge 0} giving ARand MA parameters.}\item{Delta}{vector of differencing coefficients, so an ARMA model isfitted to \code{y[t] - Delta[1]*y[t-1] - \dots}.}\item{kappa}{the prior variance (as a multiple of the innovationsvariance) for the past observations in a differenced model.}\item{SSinit}{a string specifying the algorithm to compute the\code{Pn} part of the state-space initialization; see\sQuote{Details}.}\item{tol}{tolerance eventually passed to \code{\link{solve.default}}when \code{SSinit = "Rossignol2011"}.}}\details{These functions work with a general univariate state-space modelwith state vector \samp{a}, transitions \samp{a <- T a + R e},\eqn{e \sim {\cal N}(0, \kappa Q)}{e ~ N(0, kappa Q)} and observationequation \samp{y = Z'a + eta},\eqn{(eta\equiv\eta), \eta \sim {\cal N}(0, \kappa h)}{eta ~ N(0, kappa h)}.The likelihood is a profile likelihood after estimation of\eqn{\kappa}.The model is specified as a list with at least components\describe{\item{\code{T}}{the transition matrix}\item{\code{Z}}{the observation coefficients}\item{\code{h}}{the observation variance}\item{\code{V}}{\samp{RQR'}}\item{\code{a}}{the current state estimate}\item{\code{P}}{the current estimate of the state uncertainty matrix \eqn{Q}}\item{\code{Pn}}{the estimate at time \eqn{t-1} of the stateuncertainty matrix \eqn{Q} (not updated by \code{KalmanForecast}).}}\code{KalmanSmooth} is the workhorse function for \code{\link{tsSmooth}}.\code{makeARIMA} constructs the state-space model for an ARIMA model,see also \code{\link{arima}}.The state-space initialization has used Gardner \abbr{et al.}'s method(\code{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 onlykept for reproducibility reasons. Explicit specification of\code{SSinit} is therefore recommended, notably also in\code{\link{arima}()}.The \code{"Rossignol2011"} method has been proposed and partlydocumented by \I{Raphael Rossignol}, Univ. Grenoble, on 2011-09-20(\PR{14682}), and later been ported to C by \I{Matwey V. Kornilov}.It computes the covariance matrix of\eqn{(X_{t-1},...,X_{t-p},Z_t,...,Z_{t-q})}by the method of difference equations(page 93 of \bibcitet{R:Brockwell+Davis:1991}),apparently suggested by a referee of Gardner \abbr{et al.}\sspace(see p.314 oftheir paper).}\value{For \code{KalmanLike}, a list with components \code{Lik} (thelog-likelihood less some constants) and \code{s2}, the estimate of\eqn{\kappa}.For \code{KalmanRun}, a list with components \code{values}, a vectorof length 2 giving the output of \code{KalmanLike}, \code{resid} (theresiduals) and \code{states}, the contemporaneous state estimates,a matrix with one row for each observation time.For \code{KalmanSmooth}, a list with two components.Component \code{smooth} is a \code{n} by \code{p} matrix of stateestimates based on all the observations, with one row for each time.Component \code{var} is a \code{n} by \code{p} by \code{p} array ofvariance matrices.For \code{KalmanForecast}, a list with components \code{pred}, thepredictions, and \code{var}, the unscaled variances of the predictionerrors (to be multiplied by \code{s2}).For \code{makeARIMA}, a model list including components forits arguments.}\section{Warning}{These functions are designed to be called from other functions whichcheck the validity of the arguments passed, so very little checking isdone.}\references{\bibshow{*,R:Durbin+Koopman:2001,R:Gardner+Harvey+Phillips:1980}R bug report \PR{14682} (2011--2013)}\seealso{\code{\link{arima}}, \code{\link{StructTS}}. \code{\link{tsSmooth}}.}\examples{## an ARIMA fitfit3 <- arima(presidents, c(3, 0, 0))predict(fit3, 12)## reconstruct thispr <- KalmanForecast(12, fit3$model)pr$pred + fit3$coef[4]sqrt(pr$var * fit3$sigma2)## and now do it year by yearmod <- fit3$modelfor(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")}}\keyword{ts}