Skip to contents

Model

The measurement model is given by 𝐲i,t=π›Ž+πš²π›ˆi,t+𝛆i,t,with𝛆i,tβˆΌπ’©(𝟎,𝚯)\begin{equation} \mathbf{y}_{i, t} = \boldsymbol{\nu} + \boldsymbol{\Lambda} \boldsymbol{\eta}_{i, t} + \boldsymbol{\varepsilon}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\varepsilon}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Theta} \right) \end{equation} where 𝐲i,t\mathbf{y}_{i, t}, π›ˆi,t\boldsymbol{\eta}_{i, t}, and 𝛆i,t\boldsymbol{\varepsilon}_{i, t} are random variables and π›Ž\boldsymbol{\nu}, 𝚲\boldsymbol{\Lambda}, and 𝚯\boldsymbol{\Theta} are model parameters. 𝐲i,t\mathbf{y}_{i, t} represents a vector of observed random variables, π›ˆi,t\boldsymbol{\eta}_{i, t} a vector of latent random variables, and 𝛆i,t\boldsymbol{\varepsilon}_{i, t} a vector of random measurement errors, at time tt and individual ii. π›Ž\boldsymbol{\nu} denotes a vector of intercepts, 𝚲\boldsymbol{\Lambda} a matrix of factor loadings, and 𝚯\boldsymbol{\Theta} the covariance matrix of 𝛆\boldsymbol{\varepsilon}.

An alternative representation of the measurement error is given by 𝛆i,t=𝚯12𝐳i,t,with𝐳i,tβˆΌπ’©(𝟎,𝐈)\begin{equation} \boldsymbol{\varepsilon}_{i, t} = \boldsymbol{\Theta}^{\frac{1}{2}} \mathbf{z}_{i, t}, \quad \mathrm{with} \quad \mathbf{z}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \mathbf{I} \right) \end{equation} where 𝐳i,t\mathbf{z}_{i, t} is a vector of independent standard normal random variables and (𝚯12)(𝚯12)β€²=𝚯\left( \boldsymbol{\Theta}^{\frac{1}{2}} \right) \left( \boldsymbol{\Theta}^{\frac{1}{2}} \right)^{\prime} = \boldsymbol{\Theta} .

The dynamic structure is given by π›ˆi,t=𝛂+π›ƒπ›ˆi,tβˆ’1+𝛇i,t,with𝛇i,tβˆΌπ’©(𝟎,𝚿)\begin{equation} \boldsymbol{\eta}_{i, t} = \boldsymbol{\alpha} + \boldsymbol{\beta} \boldsymbol{\eta}_{i, t - 1} + \boldsymbol{\zeta}_{i, t}, \quad \mathrm{with} \quad \boldsymbol{\zeta}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \boldsymbol{\Psi} \right) \end{equation} where π›ˆi,t\boldsymbol{\eta}_{i, t}, π›ˆi,tβˆ’1\boldsymbol{\eta}_{i, t - 1}, and 𝛇i,t\boldsymbol{\zeta}_{i, t} are random variables, and 𝛂\boldsymbol{\alpha}, 𝛃\boldsymbol{\beta}, and 𝚿\boldsymbol{\Psi} are model parameters. Here, π›ˆi,t\boldsymbol{\eta}_{i, t} is a vector of latent variables at time tt and individual ii, π›ˆi,tβˆ’1\boldsymbol{\eta}_{i, t - 1} represents a vector of latent variables at time tβˆ’1t - 1 and individual ii, and 𝛇i,t\boldsymbol{\zeta}_{i, t} represents a vector of dynamic noise at time tt and individual ii. 𝛂\boldsymbol{\alpha} denotes a vector of intercepts, 𝛃\boldsymbol{\beta} a matrix of autoregression and cross regression coefficients, and 𝚿\boldsymbol{\Psi} the covariance matrix of 𝛇i,t\boldsymbol{\zeta}_{i, t}.

An alternative representation of the dynamic noise is given by 𝛇i,t=𝚿12𝐳i,t,with𝐳i,tβˆΌπ’©(𝟎,𝐈)\begin{equation} \boldsymbol{\zeta}_{i, t} = \boldsymbol{\Psi}^{\frac{1}{2}} \mathbf{z}_{i, t}, \quad \mathrm{with} \quad \mathbf{z}_{i, t} \sim \mathcal{N} \left( \mathbf{0}, \mathbf{I} \right) \end{equation} where (𝚿12)(𝚿12)β€²=𝚿\left( \boldsymbol{\Psi}^{\frac{1}{2}} \right) \left( \boldsymbol{\Psi}^{\frac{1}{2}} \right)^{\prime} = \boldsymbol{\Psi} .

Data Generation

Notation

Let t=1000t = 1000 be the number of time points and n=5n = 5 be the number of individuals.

Let the measurement model intecept vector π›Ž\boldsymbol{\nu} be given by

π›Ž=(000).\begin{equation} \boldsymbol{\nu} = \left( \begin{array}{c} 0 \\ 0 \\ 0 \\ \end{array} \right) . \end{equation}

Let the factor loadings matrix 𝚲\boldsymbol{\Lambda} be given by

𝚲=(100010001).\begin{equation} \boldsymbol{\Lambda} = \left( \begin{array}{ccc} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \\ \end{array} \right) . \end{equation}

Let the measurement error covariance matrix 𝚯\boldsymbol{\Theta} be given by

𝚯=(0.20000.20000.2).\begin{equation} \boldsymbol{\Theta} = \left( \begin{array}{ccc} 0.2 & 0 & 0 \\ 0 & 0.2 & 0 \\ 0 & 0 & 0.2 \\ \end{array} \right) . \end{equation}

Let the initial condition π›ˆ0\boldsymbol{\eta}_{0} be given by

π›ˆ0βˆΌπ’©(π›π›ˆβˆ£0,πšΊπ›ˆβˆ£0)\begin{equation} \boldsymbol{\eta}_{0} \sim \mathcal{N} \left( \boldsymbol{\mu}_{\boldsymbol{\eta} \mid 0}, \boldsymbol{\Sigma}_{\boldsymbol{\eta} \mid 0} \right) \end{equation}

π›π›ˆβˆ£0=(000)\begin{equation} \boldsymbol{\mu}_{\boldsymbol{\eta} \mid 0} = \left( \begin{array}{c} 0 \\ 0 \\ 0 \\ \end{array} \right) \end{equation}

πšΊπ›ˆβˆ£0=(10.20.20.210.20.20.21).\begin{equation} \boldsymbol{\Sigma}_{\boldsymbol{\eta} \mid 0} = \left( \begin{array}{ccc} 1 & 0.2 & 0.2 \\ 0.2 & 1 & 0.2 \\ 0.2 & 0.2 & 1 \\ \end{array} \right) . \end{equation}

Let the constant vector 𝛂\boldsymbol{\alpha} be given by

𝛂=(000).\begin{equation} \boldsymbol{\alpha} = \left( \begin{array}{c} 0 \\ 0 \\ 0 \\ \end{array} \right) . \end{equation}

Let the transition matrix 𝛃\boldsymbol{\beta} be given by

𝛃=(0.7000.50.60βˆ’0.10.40.5).\begin{equation} \boldsymbol{\beta} = \left( \begin{array}{ccc} 0.7 & 0 & 0 \\ 0.5 & 0.6 & 0 \\ -0.1 & 0.4 & 0.5 \\ \end{array} \right) . \end{equation}

Let the dynamic process noise 𝚿\boldsymbol{\Psi} be given by

𝚿=(0.10000.10000.1).\begin{equation} \boldsymbol{\Psi} = \left( \begin{array}{ccc} 0.1 & 0 & 0 \\ 0 & 0.1 & 0 \\ 0 & 0 & 0.1 \\ \end{array} \right) . \end{equation}

R Function Arguments

n
#> [1] 5
time
#> [1] 1000
mu0
#> [1] 0 0 0
sigma0
#>      [,1] [,2] [,3]
#> [1,]  1.0  0.2  0.2
#> [2,]  0.2  1.0  0.2
#> [3,]  0.2  0.2  1.0
sigma0_l # sigma0_l <- t(chol(sigma0))
#>      [,1]      [,2]      [,3]
#> [1,]  1.0 0.0000000 0.0000000
#> [2,]  0.2 0.9797959 0.0000000
#> [3,]  0.2 0.1632993 0.9660918
alpha
#> [1] 0 0 0
beta
#>      [,1] [,2] [,3]
#> [1,]  0.7  0.0  0.0
#> [2,]  0.5  0.6  0.0
#> [3,] -0.1  0.4  0.5
psi
#>      [,1] [,2] [,3]
#> [1,]  0.1  0.0  0.0
#> [2,]  0.0  0.1  0.0
#> [3,]  0.0  0.0  0.1
psi_l # psi_l <- t(chol(psi))
#>           [,1]      [,2]      [,3]
#> [1,] 0.3162278 0.0000000 0.0000000
#> [2,] 0.0000000 0.3162278 0.0000000
#> [3,] 0.0000000 0.0000000 0.3162278
nu
#> [1] 0 0 0
lambda
#>      [,1] [,2] [,3]
#> [1,]    1    0    0
#> [2,]    0    1    0
#> [3,]    0    0    1
theta
#>      [,1] [,2] [,3]
#> [1,]  0.2  0.0  0.0
#> [2,]  0.0  0.2  0.0
#> [3,]  0.0  0.0  0.2
theta_l # theta_l <- t(chol(theta))
#>           [,1]      [,2]      [,3]
#> [1,] 0.4472136 0.0000000 0.0000000
#> [2,] 0.0000000 0.4472136 0.0000000
#> [3,] 0.0000000 0.0000000 0.4472136

Visualizing the Dynamics Without Process Noise (n = 5 with Different Initial Condition)

Using the SimSSMFixed Function from the simStateSpace Package to Simulate Data

library(simStateSpace)
sim <- SimSSMFixed(
  n = n,
  time = time,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  alpha = alpha,
  beta = beta,
  psi_l = psi_l,
  nu = nu,
  lambda = lambda,
  theta_l = theta_l,
  type = 0
)
data <- as.data.frame(sim)
head(data)
#>   id time          y1          y2         y3
#> 1  1    0 -0.68686529 -0.23269186  0.5864176
#> 2  1    1 -0.24266216 -0.01684359 -0.2646096
#> 3  1    2  0.92818305 -0.05859969 -1.0290302
#> 4  1    3  0.03836036  0.57871750 -0.2909122
#> 5  1    4  0.14986876  0.76497073  0.8038175
#> 6  1    5 -0.10242480  0.63283996  0.2144810
summary(data)
#>        id         time             y1                  y2          
#>  Min.   :1   Min.   :  0.0   Min.   :-2.315283   Min.   :-2.59732  
#>  1st Qu.:2   1st Qu.:249.8   1st Qu.:-0.416008   1st Qu.:-0.48676  
#>  Median :3   Median :499.5   Median : 0.014868   Median : 0.03318  
#>  Mean   :3   Mean   :499.5   Mean   : 0.007133   Mean   : 0.01971  
#>  3rd Qu.:4   3rd Qu.:749.2   3rd Qu.: 0.422484   3rd Qu.: 0.52048  
#>  Max.   :5   Max.   :999.0   Max.   : 2.134734   Max.   : 2.82920  
#>        y3          
#>  Min.   :-2.35741  
#>  1st Qu.:-0.41852  
#>  Median : 0.03167  
#>  Mean   : 0.02834  
#>  3rd Qu.: 0.47214  
#>  Max.   : 2.55393
plot(sim)

Model Fitting

Prepare Data

dynr_data <- dynr::dynr.data(
  data = data,
  id = "id",
  time = "time",
  observed = c("y1", "y2", "y3")
)

Prepare Initial Condition

dynr_initial <- dynr::prep.initial(
  values.inistate = mu0,
  params.inistate = c("mu0_1_1", "mu0_2_1", "mu0_3_1"),
  values.inicov = sigma0,
  params.inicov = matrix(
    data = c(
      "sigma0_1_1", "sigma0_2_1", "sigma0_3_1",
      "sigma0_2_1", "sigma0_2_2", "sigma0_3_2",
      "sigma0_3_1", "sigma0_3_2", "sigma0_3_3"
    ),
    nrow = 3
  )
)

Prepare Measurement Model

dynr_measurement <- dynr::prep.measurement(
  values.load = diag(3),
  params.load = matrix(data = "fixed", nrow = 3, ncol = 3),
  state.names = c("eta_1", "eta_2", "eta_3"),
  obs.names = c("y1", "y2", "y3")
)

Prepare Dynamic Process

dynr_dynamics <- dynr::prep.formulaDynamics(
  formula = list(
    eta_1 ~ alpha_1_1 * 1 + beta_1_1 * eta_1 + beta_1_2 * eta_2 + beta_1_3 * eta_3,
    eta_2 ~ alpha_2_1 * 1 + beta_2_1 * eta_1 + beta_2_2 * eta_2 + beta_2_3 * eta_3,
    eta_3 ~ alpha_3_1 * 1 + beta_3_1 * eta_1 + beta_3_2 * eta_2 + beta_3_3 * eta_3
  ),
  startval = c(
    alpha_1_1 = alpha[1], alpha_2_1 = alpha[2], alpha_3_1 = alpha[3],
    beta_1_1 = beta[1, 1], beta_1_2 = beta[1, 2], beta_1_3 = beta[1, 3],
    beta_2_1 = beta[2, 1], beta_2_2 = beta[2, 2], beta_2_3 = beta[2, 3],
    beta_3_1 = beta[3, 1], beta_3_2 = beta[3, 2], beta_3_3 = beta[3, 3]
  ),
  isContinuousTime = FALSE
)

Prepare Process Noise

dynr_noise <- dynr::prep.noise(
  values.latent = psi,
  params.latent = matrix(
    data = c(
      "psi_1_1", "psi_2_1", "psi_3_1",
      "psi_2_1", "psi_2_2", "psi_3_2",
      "psi_3_1", "psi_3_2", "psi_3_3"
    ),
    nrow = 3
  ),
  values.observed = theta,
  params.observed = matrix(
    data = c(
      "theta_1_1", "fixed", "fixed",
      "fixed", "theta_2_2", "fixed",
      "fixed", "fixed", "theta_3_3"
    ),
    nrow = 3
  )
)

Prepare the Model

model <- dynr::dynr.model(
  data = dynr_data,
  initial = dynr_initial,
  measurement = dynr_measurement,
  dynamics = dynr_dynamics,
  noise = dynr_noise,
  outfile = "ssm.c"
)

Fit the Model

results <- dynr::dynr.cook(
  model,
  debug_flag = TRUE,
  verbose = FALSE
)
#> [1] "Get ready!!!!"
#> using C compiler: β€˜gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0’
#> Optimization function called.
#> Starting Hessian calculation ...
#> Finished Hessian calculation.
#> Original exit flag:  3 
#> Modified exit flag:  3 
#> Optimization terminated successfully: ftol_rel or ftol_abs was reached. 
#> Original fitted parameters:  0.001801436 0.005185958 0.007157565 0.6877616 
#> -0.01275751 0.003318878 0.4483465 0.6314418 -0.01385958 -0.04895223 0.3635247 
#> 0.4829981 -2.20681 0.06791994 -0.1341011 -2.255885 0.02301437 -2.260819 
#> -1.66862 -1.652846 -1.656658 0.6836268 -0.7058184 0.6046042 -1.203163 0.3947267 
#> -0.02842039 0.2048591 0.2410451 -1.554114 
#> 
#> Transformed fitted parameters:  0.001801436 0.005185958 0.007157565 0.6877616 
#> -0.01275751 0.003318878 0.4483465 0.6314418 -0.01385958 -0.04895223 0.3635247 
#> 0.4829981 0.1100512 0.00747467 -0.01475799 0.1052885 0.001409103 0.1062996 
#> 0.188507 0.191504 0.1907754 0.6836268 -0.7058184 0.6046042 0.3002431 0.118514 
#> -0.008533026 1.274133 0.292479 0.2829316 
#> 
#> Doing end processing
#> Successful trial
#> Total Time: 4.844532 
#> Backend Time: 4.834731

Summary

summary(results)
#> Coefficients:
#>             Estimate Std. Error t value  ci.lower  ci.upper Pr(>|t|)    
#> alpha_1_1   0.001801   0.005091   0.354 -0.008178  0.011780   0.3617    
#> alpha_2_1   0.005186   0.005841   0.888 -0.006263  0.016635   0.1873    
#> alpha_3_1   0.007158   0.006087   1.176 -0.004773  0.019088   0.1198    
#> beta_1_1    0.687762   0.043305  15.882  0.602885  0.772638   <2e-16 ***
#> beta_1_2   -0.012758   0.027195  -0.469 -0.066060  0.040545   0.3195    
#> beta_1_3    0.003319   0.022159   0.150 -0.040113  0.046750   0.4405    
#> beta_2_1    0.448347   0.039622  11.316  0.370690  0.526003   <2e-16 ***
#> beta_2_2    0.631442   0.031404  20.107  0.569891  0.692993   <2e-16 ***
#> beta_2_3   -0.013860   0.025050  -0.553 -0.062956  0.035237   0.2900    
#> beta_3_1   -0.048952   0.031022  -1.578 -0.109754  0.011850   0.0573 .  
#> beta_3_2    0.363525   0.031258  11.630  0.302260  0.424790   <2e-16 ***
#> beta_3_3    0.482998   0.033827  14.278  0.416698  0.549298   <2e-16 ***
#> psi_1_1     0.110051   0.014477   7.602  0.081677  0.138426   <2e-16 ***
#> psi_2_1     0.007475   0.004869   1.535 -0.002068  0.017018   0.0624 .  
#> psi_3_1    -0.014758   0.004649  -3.175 -0.023869 -0.005647   0.0008 ***
#> psi_2_2     0.105288   0.010777   9.770  0.084166  0.126411   <2e-16 ***
#> psi_3_2     0.001409   0.004614   0.305 -0.007634  0.010452   0.3800    
#> psi_3_3     0.106300   0.014846   7.160  0.077202  0.135397   <2e-16 ***
#> theta_1_1   0.188507   0.011923  15.811  0.165139  0.211875   <2e-16 ***
#> theta_2_2   0.191504   0.010345  18.512  0.171228  0.211780   <2e-16 ***
#> theta_3_3   0.190775   0.014597  13.070  0.162166  0.219385   <2e-16 ***
#> mu0_1_1     0.683627   0.294780   2.319  0.105868  1.261385   0.0102 *  
#> mu0_2_1    -0.705818   0.525690  -1.343 -1.736152  0.324515   0.0897 .  
#> mu0_3_1     0.604604   0.299845   2.016  0.016918  1.192291   0.0219 *  
#> sigma0_1_1  0.300243   0.265479   1.131 -0.220085  0.820572   0.1291    
#> sigma0_2_1  0.118514   0.341029   0.348 -0.549890  0.786918   0.3641    
#> sigma0_3_1 -0.008533   0.192546  -0.044 -0.385916  0.368850   0.4823    
#> sigma0_2_2  1.274133   0.891749   1.429 -0.473662  3.021928   0.0766 .  
#> sigma0_3_2  0.292479   0.376718   0.776 -0.445874  1.030832   0.2188    
#> sigma0_3_3  0.282932   0.286111   0.989 -0.277836  0.843699   0.1614    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> -2 log-likelihood value at convergence = 26307.71
#> AIC = 26367.71
#> BIC = 26563.22

Parameter Estimates

alpha_hat
#> [1] 0.001801436 0.005185958 0.007157565
beta_hat
#>             [,1]        [,2]         [,3]
#> [1,]  0.68776163 -0.01275751  0.003318878
#> [2,]  0.44834653  0.63144183 -0.013859581
#> [3,] -0.04895223  0.36352474  0.482998125
psi_hat
#>             [,1]        [,2]         [,3]
#> [1,]  0.11005119 0.007474670 -0.014757988
#> [2,]  0.00747467 0.105288487  0.001409103
#> [3,] -0.01475799 0.001409103  0.106299593
theta_hat
#>          [,1]     [,2]      [,3]
#> [1,] 0.188507 0.000000 0.0000000
#> [2,] 0.000000 0.191504 0.0000000
#> [3,] 0.000000 0.000000 0.1907754
mu0_hat
#> [1]  0.6836268 -0.7058184  0.6046042
sigma0_hat
#>              [,1]     [,2]         [,3]
#> [1,]  0.300243120 0.118514 -0.008533026
#> [2,]  0.118513969 1.274133  0.292478961
#> [3,] -0.008533026 0.292479  0.282931613

References

Chow, S.-M., Ho, M. R., Hamaker, E. L., & Dolan, C. V. (2010). Equivalence and differences between structural equation modeling and state-space modeling techniques. Structural Equation Modeling: A Multidisciplinary Journal, 17(2), 303–332. https://doi.org/10.1080/10705511003661553
Ou, L., Hunter, M. D., & Chow, S.-M. (2019). What’s for dynr: A package for linear and nonlinear dynamic modeling in R. The R Journal, 11(1), 91. https://doi.org/10.32614/rj-2019-012
R Core Team. (2024). R: A language and environment for statistical computing. R Foundation for Statistical Computing. https://www.R-project.org/