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=1000n = 1000 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] 1000
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.0   Min.   :  0.0   Min.   :-3.573703   Min.   :-3.702841  
#>  1st Qu.: 250.8   1st Qu.:249.8   1st Qu.:-0.423299   1st Qu.:-0.497459  
#>  Median : 500.5   Median :499.5   Median : 0.001152   Median : 0.001192  
#>  Mean   : 500.5   Mean   :499.5   Mean   : 0.001166   Mean   : 0.001209  
#>  3rd Qu.: 750.2   3rd Qu.:749.2   3rd Qu.: 0.426030   3rd Qu.: 0.499596  
#>  Max.   :1000.0   Max.   :999.0   Max.   : 3.590387   Max.   : 3.752081  
#>        y3           
#>  Min.   :-3.239996  
#>  1st Qu.:-0.461231  
#>  Median :-0.000183  
#>  Mean   :-0.000281  
#>  3rd Qu.: 0.461016  
#>  Max.   : 3.178021
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.0003181193 -7.837462e-05 -0.000499533 0.6985586 
#> 0.001367556 -0.0004034378 0.496182 0.6022024 -0.001364295 -0.1011867 0.4016231 
#> 0.4988828 -2.300048 0.004318334 0.0004479543 -2.298005 -0.0003334026 -2.287675 
#> -1.613045 -1.610018 -1.618032 -0.001660465 -0.03157746 -0.04499351 -0.03443094 
#> 0.1758896 0.2157081 -0.04608466 0.1310545 -0.1381093 
#> 
#> Transformed fitted parameters:  0.0003181193 -7.837462e-05 -0.000499533 
#> 0.6985586 0.001367556 -0.0004034378 0.496182 0.6022024 -0.001364295 -0.1011867 
#> 0.4016231 0.4988828 0.100254 0.0004329303 4.490922e-05 0.100461 -3.329939e-05 
#> 0.1015022 0.1992798 0.1998839 0.1982886 -0.001660465 -0.03157746 -0.04499351 
#> 0.9661551 0.1699366 0.2084075 0.9848512 0.1618086 0.9323604 
#> 
#> Doing end processing
#> Warning in sqrt(diag(iHess)): NaNs produced
#> Warning in sqrt(diag(x$inv.hessian)): NaNs produced
#> Warning: These parameters may have untrustworthy standard errors: sigma0_1_1,
#> sigma0_3_1, sigma0_2_2.
#> Total Time: 17.19489 
#> Backend Time: 17.19471

Summary

summary(results)
#> Coefficients:
#>              Estimate Std. Error t value   ci.lower   ci.upper Pr(>|t|)    
#> alpha_1_1   3.181e-04  3.444e-04   0.924 -3.569e-04  9.932e-04   0.1778    
#> alpha_2_1  -7.837e-05  4.259e-04  -0.184 -9.132e-04  7.564e-04   0.4270    
#> alpha_3_1  -4.995e-04  4.311e-04  -1.159 -1.345e-03  3.455e-04   0.1233    
#> beta_1_1    6.986e-01  2.958e-03 236.188  6.928e-01  7.044e-01   <2e-16 ***
#> beta_1_2    1.368e-03  1.844e-03   0.742 -2.246e-03  4.982e-03   0.2291    
#> beta_1_3   -4.034e-04  1.353e-03  -0.298 -3.056e-03  2.249e-03   0.3828    
#> beta_2_1    4.962e-01  3.071e-03 161.590  4.902e-01  5.022e-01   <2e-16 ***
#> beta_2_2    6.022e-01  2.403e-03 250.630  5.975e-01  6.069e-01   <2e-16 ***
#> beta_2_3   -1.364e-03  1.709e-03  -0.798 -4.714e-03  1.985e-03   0.2123    
#> beta_3_1   -1.012e-01  2.269e-03 -44.604 -1.056e-01 -9.674e-02   <2e-16 ***
#> beta_3_2    4.016e-01  2.225e-03 180.498  3.973e-01  4.060e-01   <2e-16 ***
#> beta_3_3    4.989e-01  2.143e-03 232.847  4.947e-01  5.031e-01   <2e-16 ***
#> psi_1_1     1.003e-01  9.607e-04 104.354  9.837e-02  1.021e-01   <2e-16 ***
#> psi_2_1     4.329e-04  3.423e-04   1.265 -2.379e-04  1.104e-03   0.1030    
#> psi_3_1     4.491e-05  3.234e-04   0.139 -5.890e-04  6.788e-04   0.4448    
#> psi_2_2     1.005e-01  7.942e-04 126.495  9.890e-02  1.020e-01   <2e-16 ***
#> psi_3_2    -3.330e-05  3.215e-04  -0.104 -6.634e-04  5.968e-04   0.4588    
#> psi_3_3     1.015e-01  9.652e-04 105.164  9.961e-02  1.034e-01   <2e-16 ***
#> theta_1_1   1.993e-01  8.091e-04 246.293  1.977e-01  2.009e-01   <2e-16 ***
#> theta_2_2   1.999e-01  7.788e-04 256.670  1.984e-01  2.014e-01   <2e-16 ***
#> theta_3_3   1.983e-01  9.693e-04 204.573  1.964e-01  2.002e-01   <2e-16 ***
#> mu0_1_1    -1.660e-03  2.791e-02  -0.059 -5.637e-02  5.305e-02   0.4763    
#> mu0_2_1    -3.158e-02  3.999e-02  -0.790 -1.099e-01  4.679e-02   0.2148    
#> mu0_3_1    -4.499e-02  3.312e-02  -1.359 -1.099e-01  1.992e-02   0.0871 .  
#> sigma0_1_1  9.662e-01        NaN      NA        NaN        NaN       NA    
#> sigma0_2_1  1.699e-01  4.711e-02   3.608  7.761e-02  2.623e-01   0.0002 ***
#> sigma0_3_1  2.084e-01        NaN      NA        NaN        NaN       NA    
#> sigma0_2_2  9.849e-01        NaN      NA        NaN        NaN       NA    
#> sigma0_3_2  1.618e-01  3.716e-02   4.354  8.898e-02  2.346e-01   <2e-16 ***
#> sigma0_3_3  9.324e-01  7.385e-02  12.625  7.876e-01  1.077e+00   <2e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> -2 log-likelihood value at convergence = 5309559.72
#> AIC = 5309619.72
#> BIC = 5309974.18

Parameter Estimates

alpha_hat
#> [1]  3.181193e-04 -7.837462e-05 -4.995330e-04
beta_hat
#>            [,1]        [,2]          [,3]
#> [1,]  0.6985586 0.001367556 -0.0004034378
#> [2,]  0.4961820 0.602202378 -0.0013642954
#> [3,] -0.1011867 0.401623119  0.4988828070
psi_hat
#>              [,1]          [,2]          [,3]
#> [1,] 1.002540e-01  4.329303e-04  4.490922e-05
#> [2,] 4.329303e-04  1.004610e-01 -3.329939e-05
#> [3,] 4.490922e-05 -3.329939e-05  1.015022e-01
theta_hat
#>           [,1]      [,2]      [,3]
#> [1,] 0.1992798 0.0000000 0.0000000
#> [2,] 0.0000000 0.1998839 0.0000000
#> [3,] 0.0000000 0.0000000 0.1982886
mu0_hat
#> [1] -0.001660465 -0.031577461 -0.044993506
sigma0_hat
#>           [,1]      [,2]      [,3]
#> [1,] 0.9661551 0.1699366 0.2084075
#> [2,] 0.1699366 0.9848512 0.1618086
#> [3,] 0.2084075 0.1618086 0.9323604

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/