• MARSS Package Manual
  • Preface
    • Installation
    • Authors
    • Citation
    • Acknowledgments
  • Part 1. Overview
  • 1 Overview
    • 1.1 MARSS model form
    • 1.2 Example model
    • 1.3 Notes
  • 2 Quick Start
    • 2.1 Model specification
    • 2.2 Linear constraints
    • 2.3 Time-varying parameters
    • 2.4 Inputs c and d
    • 2.5 Tips and Tricks
  • 3 Data format
    • 3.1 ts objects
    • 3.2 tsibble objects
  • 4 Model specification
    • 4.1 General matrix specification
    • 4.2 Linear constraints
    • 4.3 Time-varying parameters
    • 4.4 Examples for each parameter
  • 5 Covariates format
  • Part 2. Short Examples
  • 6 Common output for fits
  • 7 Univariate Models
    • 7.1 Random walk with drift
    • 7.2 Random walk with time-varying parameters
    • 7.3 AR(1) observed with error
    • 7.4 Linear regression (LR)
    • 7.5 LR with AR(1) errors
    • 7.6 LR with AR(1) errors and independent errors
    • 7.7 LR with AR(1) errors driven by covariate
    • 7.8 Flat level model
    • 7.9 Linear trend model
    • 7.10 Stochastic level model
    • 7.11 Stochastic slope model
  • 8 Multivariate Models
    • 8.1 RW observed with multiple time series
    • 8.2 Multiple time series each observing a RW
    • 8.3 Different time series observing a RW
    • 8.4 Trend observed with AR(1) error
    • 8.5 Dynamic Factor Model with 3 trends
    • 8.6 Linear constraints
  • Part 3. Outputs
  • 9 Parameter estimates
  • 10 States and smoothed estimates
  • 11 Model fits
  • 12 Residuals
  • 13 Confidence Intervals
  • 14 Predictions and forecasts
  • Part 4. Tips and Tricks
  • 15 Troubleshooting
  • 16 Algorithm notes and cautions
    • 16.1 Properly constrained models
    • 16.2 Notes on the Kalman filter
    • 16.3 Notes on the EM algorithm
    • 16.4 Bias in variance estimates
    • 16.5 Careful if specifying a prior on initial conditions
    • 16.6 State-space form of ARMA(p,q) models
  • 17 Other related packages
  • References
  • Published with bookdown

MARSS R Package

8.5 Dynamic Factor Model with 3 trends

Dynamic Factor Modeling is akin to PCA (Principal Components Analysis) for time series data. This is a large topic. See the chapter on DFAs in the ATSA book. MARSS has a short-cut for fitting DFAs. See ?MARSS.dfa but we will fit manually by specifying all the matrices.

We will fit a DFA with 2 trends. \[\begin{equation} \begin{gathered} \begin{bmatrix}x_1\\x_2\end{bmatrix}_t = \begin{bmatrix}x_1\\x_2\end{bmatrix}_{t-1} + \begin{bmatrix}w_1\\ w_2\end{bmatrix}, \textrm{ } \mathbf{w}_t \sim \,\text{MVN}\begin{pmatrix}\begin{bmatrix}0\\0\end{bmatrix}, \begin{bmatrix}1&0\\0&1\end{bmatrix} \end{pmatrix}\\ \begin{bmatrix}y_1\\y_2\\y_3\\y_4\\y_5\end{bmatrix}_t = \begin{bmatrix}z_1&0\\z_2&z_6\\z_3&z_7\\z_4&z_8\\z_5&z_9\end{bmatrix} \begin{bmatrix}x_1\\x_2\end{bmatrix}_t + \begin{bmatrix}0\\0\\0\\0\\0\end{bmatrix} + \begin{bmatrix}v_1\\ v_2\\ v_3 \\ v_4 \\ v_5\end{bmatrix}, \textrm{ } \mathbf{v}_t \sim \,\text{MVN}\begin{pmatrix}\begin{bmatrix}0\\0\\0\\0\\0\end{bmatrix}, \begin{bmatrix} r&0&0&0&0\\ 0&r&0&0&0\\ 0&0&r&0&0\\ 0&0&0&r&0\\ 0&0&0&0&r \end{bmatrix} \end{pmatrix} \end{gathered} \tag{8.6} \end{equation}\]

Create set up the model:

Q <- diag(1, 2)
R <- "diagonal and equal"
Z <- matrix(list(0), 5, 2)
Z[, ] <- paste0("z", 1:10)
Z[1, 2] <- 0
A <- "zero"
U <- "zero"

Fit with MARSS(). We need to demean the data for a DFA. These models take a long time to converge. This one works better with the BFGS algorithm.

yt <- t(harborSealWA)[2:6, ]
fit <- MARSS(zscore(yt, mean.only = TRUE), model = list(R = R, 
    Q = Q, Z = Z, A = A, U = U), method = "BFGS")