Skip to contents

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

# \donttest{
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"
))
#> Success! abstol and log-log tests passed at 23 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 23 iterations. 
#> Log-likelihood: 11.92445 
#> AIC: -11.8489   AICc: -9.695058   
#>  
#>         Estimate
#> A.SJI     0.7993
#> A.EBays   0.2778
#> R.diag    0.0234
#> U.U       0.0614
#> Q.Q       0.0095
#> x0.x0     6.1032
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 21 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 21 iterations. 
#> Log-likelihood: 15.90228 
#> AIC: -19.80456   AICc: -17.80456   
#>  
#>         Estimate
#> A.SJI     0.7704
#> A.EBays   0.2357
#> R.diag    0.0198
#> U.U       0.0653
#> Q.Q       0.0106
#> x0.x0     6.0153
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 25 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 25 iterations. 
#> Log-likelihood: 14.82086 
#> AIC: -17.64172   AICc: -15.48787   
#>  
#>         Estimate
#> A.SJI    0.84059
#> A.EBays  0.32619
#> R.diag   0.02188
#> U.U      0.05740
#> Q.Q      0.00638
#> x0.x0    6.18985
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 22 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 22 iterations. 
#> Log-likelihood: 13.16553 
#> AIC: -14.33107   AICc: -12.33107   
#>  
#>         Estimate
#> A.SJI    0.77965
#> A.EBays  0.27179
#> R.diag   0.02360
#> U.U      0.06076
#> Q.Q      0.00886
#> x0.x0    6.11565
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 23 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 23 iterations. 
#> Log-likelihood: 13.26315 
#> AIC: -14.5263   AICc: -12.4263   
#>  
#>         Estimate
#> A.SJI     0.8098
#> A.EBays   0.2662
#> R.diag    0.0215
#> U.U       0.0610
#> Q.Q       0.0112
#> x0.x0     6.0855
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 22 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 22 iterations. 
#> Log-likelihood: 12.3994 
#> AIC: -12.79881   AICc: -10.69881   
#>  
#>         Estimate
#> A.SJI    0.78542
#> A.EBays  0.28216
#> R.diag   0.02439
#> U.U      0.06353
#> Q.Q      0.00768
#> x0.x0    6.11319
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 22 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 22 iterations. 
#> Log-likelihood: 13.74725 
#> AIC: -15.4945   AICc: -13.44572   
#>  
#>         Estimate
#> A.SJI     0.7850
#> A.EBays   0.2763
#> R.diag    0.0218
#> U.U       0.0607
#> Q.Q       0.0102
#> x0.x0     6.1030
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 21 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 21 iterations. 
#> Log-likelihood: 15.09518 
#> AIC: -18.19037   AICc: -16.28128   
#>  
#>         Estimate
#> A.SJI    0.79748
#> A.EBays  0.29074
#> R.diag   0.02179
#> U.U      0.06104
#> Q.Q      0.00988
#> x0.x0    6.09450
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 24 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 24 iterations. 
#> Log-likelihood: 16.07472 
#> AIC: -20.14944   AICc: -18.04944   
#>  
#>         Estimate
#> A.SJI    0.82930
#> A.EBays  0.28020
#> R.diag   0.02024
#> U.U      0.06062
#> Q.Q      0.00757
#> x0.x0    6.09232
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 23 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 23 iterations. 
#> Log-likelihood: 13.02546 
#> AIC: -14.05091   AICc: -11.95091   
#>  
#>         Estimate
#> A.SJI     0.7907
#> A.EBays   0.2512
#> R.diag    0.0220
#> U.U       0.0562
#> Q.Q       0.0106
#> x0.x0     6.1815
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 

# 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)
#> Success! abstol and log-log tests passed at 23 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 23 iterations. 
#> Log-likelihood: 13.36878 
#> AIC: -14.73756   AICc: -12.33756   
#>  
#>         Estimate
#> A.SJI    0.79203
#> A.EBays  0.23808
#> R.diag   0.02046
#> U.U      0.05710
#> Q.Q      0.00828
#> x0.x0    6.21249
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 24 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 24 iterations. 
#> Log-likelihood: 10.93944 
#> AIC: -9.878872   AICc: -7.668346   
#>  
#>         Estimate
#> A.SJI     0.7911
#> A.EBays   0.2713
#> R.diag    0.0237
#> U.U       0.0674
#> Q.Q       0.0108
#> x0.x0     6.0100
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 25 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 25 iterations. 
#> Log-likelihood: 11.41053 
#> AIC: -10.82105   AICc: -8.550785   
#>  
#>         Estimate
#> A.SJI    0.80315
#> A.EBays  0.26160
#> R.diag   0.02316
#> U.U      0.05675
#> Q.Q      0.00956
#> x0.x0    6.17885
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 28 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 28 iterations. 
#> Log-likelihood: 11.88965 
#> AIC: -11.7793   AICc: -9.379304   
#>  
#>         Estimate
#> A.SJI    0.79974
#> A.EBays  0.27369
#> R.diag   0.02162
#> U.U      0.06183
#> Q.Q      0.00948
#> x0.x0    6.09562
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 27 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 27 iterations. 
#> Log-likelihood: 11.47508 
#> AIC: -10.95015   AICc: -8.325152   
#>  
#>         Estimate
#> A.SJI    0.79874
#> A.EBays  0.31003
#> R.diag   0.02063
#> U.U      0.06061
#> Q.Q      0.00947
#> x0.x0    6.08725
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 

# 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)
#> Success! abstol and log-log tests passed at 21 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 21 iterations. 
#> Log-likelihood: 14.91458 
#> AIC: -17.82916   AICc: -15.87567   
#>  
#>         Estimate
#> A.SJI    0.80143
#> A.EBays  0.28885
#> R.diag   0.02367
#> U.U      0.06778
#> Q.Q      0.00652
#> x0.x0    6.10448
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 27 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 27 iterations. 
#> Log-likelihood: 16.33737 
#> AIC: -20.67474   AICc: -18.57474   
#>  
#>         Estimate
#> A.SJI    0.79451
#> A.EBays  0.30766
#> R.diag   0.02326
#> U.U      0.07594
#> Q.Q      0.00345
#> x0.x0    6.11259
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 32 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 32 iterations. 
#> Log-likelihood: 17.44618 
#> AIC: -22.89236   AICc: -20.62209   
#>  
#>         Estimate
#> A.SJI    0.80612
#> A.EBays  0.33869
#> R.diag   0.02147
#> U.U      0.08159
#> Q.Q      0.00269
#> x0.x0    6.09125
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 32 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 32 iterations. 
#> Log-likelihood: 15.81173 
#> AIC: -19.62347   AICc: -17.15288   
#>  
#>         Estimate
#> A.SJI    0.79297
#> A.EBays  0.34634
#> R.diag   0.02173
#> U.U      0.08298
#> Q.Q      0.00297
#> x0.x0    6.08537
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
#> Success! abstol and log-log tests passed at 29 iterations.
#> Alert: conv.test.slope.tol is 0.5.
#> Test with smaller values (<0.1) to ensure convergence.
#> 
#> MARSS fit is
#> Estimation method: kem 
#> Convergence test: conv.test.slope.tol = 0.5, abstol = 0.001
#> Estimation converged in 29 iterations. 
#> Log-likelihood: 16.2755 
#> AIC: -20.55101   AICc: -17.84133   
#>  
#>         Estimate
#> A.SJI    0.79445
#> A.EBays  0.38057
#> R.diag   0.01893
#> U.U      0.08406
#> Q.Q      0.00393
#> x0.x0    6.05363
#> Initial states (x0) defined at t=0
#> 
#> Standard errors have not been calculated. 
#> Use MARSSparamCIs to compute CIs and bias estimates.
#> 
# }