MARSS-package
. MARSSkf
is a small helper function to select which Kalman filter/smoother function to use based on which function was requested (in MLEobj$fun.kf
). The default function is MARSSkfas
.MARSSkf( MLEobj, only.logLik=FALSE, return.lag.one=TRUE, return.kfas.model=FALSE )
MARSSkfss( MLEobj )
MARSSkfas( MLEobj, only.logLik=FALSE, return.lag.one=TRUE, return.kfas.model=FALSE )
marssMLE
object with the par
element of estimated parameters, model
element with the model description and data, and control
element for the fitting algorithm speciMARSSkfas
. If set, only the log-likelihood is returned using the KFAS function logLik
. This is much faster if only the log-likelihood is needed.MARSSkfas
. If set to FALSE, the smoothed lag-one covariance values are not returned (Vtt1T is set to NULL). This speeds up MARSSkfas
because to return the smoothed lag-one covariance a stacked MARSS model is used with MARSSkfas
. If set to TRUE, it returns the MARSS model in KFAS model form (class SSModel
). This is useful if you want to use other KFAS functions or write your own functions to worMARSSkf
.MARSSkf
.MARSSkf
.mssm.params
and innovations.marssm
) in KFAS model form (class SSModel
). Only for MARSSkfas
.MLEobj$model$miss.value
. All parameters may be time-varying. See marssm
for a description of marssm model objects and of how time-varying parameters are specified.
The expected value of the initial state, x0, is an estimated parameter (or treated as a prior). This E(initial state) can be treated in two different ways. One can treat it as x00, meaning E(x at t=0 | y at t=0), and then compute x10, meaning E(x at t=1 | y at t=0), from x00. Or one can simply treat the initial state as x10, meaning E(x at t=1 | y at t=0). The approaches lead to the same parameter estimates, but the likelihood is written slightly differently in each case and you need your likelihood calculation to correspond to how the initial state is treated in your model (either x00 or x10). The EM algorithm in the MARSS package (MARSSkem
) follows Shumway and Stoffer's derivation and uses x00, while Ghahramani et al uses x10. The MLEobj$model$tinitx
argument specifies whether the initial states (specified with x0 and V0) is at t=0 (tinitx=0
) or t=1 (tinitx=1
).
MARSSkfss()
is a native R implementation based on the traditional Kalman filter and smoother equation as shown in Shumway and Stoffer (sec 6.2, 2006). The equations have been altered slightly to the initial state distribution to be to be specified at t=0 or t=1 (data starts at t=1) per per Ghahramani and Hinton (1996). In addition, the filter and smoother equations have been altered to allow partially deterministic models (some or all elements of the Q diagonals equal to 0), partially perfect observation models (some or all elements of the R diagonal equal to 0) and fixed (albeit unknown) initial states (some or all elements of the V0 diagonal equal to 0) (per Holmes 2012). The code includes numerous checks to alert the user if matrices are becoming ill-conditioned and the algorithm unstable.
MARSSkfas()
uses the (Fortran-based) Kalman filter and smoother function (KFS
) in the KFAS package (Helske 2012) based on the algorithms of Koopman and Durbin (2000, 2001, 2003). The Koopman and Durbin algorithm is faster and more stable since it avoids matrix inverses. Exact diffuse priors are also allowed in the KFAS Kalman filter function. The standard output from the KFAS functions do not include the lag-one covariance smoother needed for the EM algorithm. MARSSkfas
computes the smoothed lag-one covariance using the Kalman filter applied to a stacked MARSS model as described on page 321 in Shumway and Stoffer (2000). Also the KFAS model specification only has the initial state at t=1 (as x(1) conditioned on y(0), which is missing). When the initial state is specified at t=0 (as x(0) conditioned on y(0), which is missing), MARSSkfas
computes the required E(x(1)|y(0)) and var(x(1)|y(0)) using the Kalman filter equations per Ghahramani and Hinton (1996). When there are zeros on the diagonal of the R matrix, MARSSkfas
checks if the
The likelihood returned for both functions is the exact likelihood when there are missing values rather than the approximate likelihood sometimes presented in texts for the missing values case. The functions return the same filter, smoother and log-likelihood values. The differences are that MARSSkfas
is faster (and more stable) but MARSSkf
has many internal checks and error messages which can help debug numerical problems (but slow things down). Also MARSSkf
returns some output specific to the traditional filter algorithm (J and Kt).RShowDoc("EMDerivation",package="MARSS")
to open a copy.
Jouni Helske (2012). KFAS: Kalman Filter and Smoother for Exponential Family State Space Models. R package version
0.9.11. http://CRAN.R-project.org/package=KFAS
Koopman, S.J. and Durbin J. (2000). Fast filtering and smoothing for non-stationary time series models, Journal of American Statistical Assosiation, 92, 1630-38.
Koopman, S.J. and Durbin J. (2001). Time Series Analysis by State Space Methods. Oxford: Oxford University Press.
Koopman, S.J. and Durbin J. (2003). Filtering and smoothing of state vector for diffuse state space models, Journal of Time Series Analysis, Vol. 24, No. 1.
The user guide: Holmes, E. E., E. J. Ward, and M. D. Scheuerell (2012) Analysis of multivariate time-series using the MARSS package. NOAA Fisheries, Northwest Fisheries Science
Center, 2725 Montlake Blvd E., Seattle, WA 98112 Type RShowDoc("UserGuide",package="MARSS")
to open a copy.MARSS
marssm
MARSSkem
dat = t(harborSeal)
dat = dat[2:nrow(dat),]
#you can use MARSS to construct a MLEobj
#MARSS calls MARSSinits to construct default initial values
MLEobj = MARSS(dat, fit=FALSE)
#MARSSkf needs a marss MLE object with the par element set
MLEobj$par=MLEobj$start
#Compute the kf output at the params used for the inits
kfList = MARSSkfas( MLEobj )
Run the code above in your browser using DataLab