MARSScv: MARSScv is a wrapper for MARSS that re-fits the model with...

View source: R/MARSScv.R

MARSScvR Documentation

MARSScv is a wrapper for MARSS that re-fits the model with cross validated data.

Description

MARSScv is a wrapper for MARSS that re-fits the model with cross validated data.

Usage

MARSScv(
  y,
  model = NULL,
  inits = NULL,
  method = "kem",
  form = "marxss",
  silent = FALSE,
  control = NULL,
  fun.kf = c("MARSSkfas", "MARSSkfss"),
  fold_ids = NULL,
  future_cv = FALSE,
  n_future_cv = floor(ncol(y)/3),
  interval = "confidence",
  ...
)

Arguments

y

A n x T matrix of n time series over T time steps. Only y is required for the function. A ts object (univariate or multivariate) can be used and this will be converted to a matrix with time in the columns.

model

Model specification using a list of parameter matrix text shortcuts or matrices. See Details and MARSS.marxss() for the default form. Or better yet open the Quick Start Guide RShowDoc("Quick_Start",package="MARSS")

inits

A list with the same form as the list output by coef(fit) that specifies initial values for the parameters. See also MARSS.marxss().

method

Estimation method. MARSS provides an EM algorithm (method="kem") (see MARSSkem()) and the BFGS algorithm (method="BFGS") (see MARSSoptim()). Fast TMB fitting provided by the companion package marssTMB.

form

The equation form used in the MARSS() call. The default is "marxss". See MARSS.marxss() or MARSS.dfa()

silent

Setting to TRUE(1) suppresses printing of full error messages, warnings, progress bars and convergence information. Setting to FALSE(0) produces error output. Setting silent=2 will produce more verbose error messages and progress information.

control

Estimation options for the maximization algorithm. The typically used control options for method="kem" are below but see marssMLE for the full list of control options. Note many of these are not allowed if method="BFGS"; see MARSSoptim() for the allowed control options for this method.

fun.kf

What Kalman filter function to use. MARSS has two: MARSSkfas() which is based on the Kalman filter in the KFAS package based on Koopman and Durbin and MARSSkfss() which is a native R implementation of the Kalman filter and smoother in Shumway and Stoffer. The KFAS filter is much faster. MARSSkfas() modifies the input and output in order to output the lag-one covariance smoother needed for the EM algorithm (per page 321 in Shumway and Stoffer (2000).

fold_ids

A n x T matrix of integers, with values assigned by the user to folds. If not included, data are randomly assigned to one of 10 folds

future_cv

Whether or not to use future cross validation (defaults to FALSE), where data up to time T-1 are used to predict data at time T. Data are held out by time slices, and the fold_ids argument is ignored.

n_future_cv

Number of time slices to hold out for future cross validation. Defaults to floor(n_future_cv/3). Predictions are made for the last n_future_cv time steps

interval

uncertainty interval for prediction. Can be one of "confidence" or "prediction", and defaults to "confidence"

...

not used

Value

A list object, containing cv_pred (a matrix of predictions), cv_se (a matrix of SEs), fold_ids (a matrix of fold ids used as data), and df (a dataframe containing the original data, predictions, SEs, and folds)

Examples


dat <- t(harborSealWA)
dat <- dat[2:4, ] # remove the year row
# fit a model with 1 hidden state and 3 observation time series
# cross validation here is random, 10 folds
fit <- MARSScv(dat, model = list(
  Z = matrix(1, 3, 1),
  R = "diagonal and equal"
))

# second, demonstrate passing in pre-specified folds
fold_ids <- matrix(
  sample(1:5, size = nrow(dat) * ncol(dat), replace = TRUE),
  nrow(dat), ncol(dat)
)
fit <- MARSScv(dat, model = list(
  Z = matrix(1, 3, 1),
  R = "diagonal and equal"
), fold_ids = fold_ids)

# third, illustrate future cross validation
fit <- MARSScv(dat, model = list(
  Z = matrix(1, 3, 1),
  R = "diagonal and equal"
), future_cv = TRUE, n_future_cv = 5)


MARSS documentation built on May 29, 2024, 3:34 a.m.