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 dπ›ˆi,t=𝚽(π›ˆi,tβˆ’π›)dt+𝚺12d𝐖i,t\begin{equation} \mathrm{d} \boldsymbol{\eta}_{i, t} = \boldsymbol{\Phi} \left( \boldsymbol{\eta}_{i, t} - \boldsymbol{\mu} \right) \mathrm{d}t + \boldsymbol{\Sigma}^{\frac{1}{2}} \mathrm{d} \mathbf{W}_{i, t} \end{equation} where 𝛍\boldsymbol{\mu} is the long-term mean or equilibrium level, 𝚽\boldsymbol{\Phi} is the rate of mean reversion, determining how quickly the variable returns to its mean, 𝚺\boldsymbol{\Sigma} is the matrix of volatility or randomness in the process, and d𝐖\mathrm{d}\boldsymbol{W} is a Wiener process or Brownian motion, which represents random fluctuations.

Data Generation

Notation

Let t=100t = 100 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 long-term mean vector 𝛍\boldsymbol{\mu} be given by

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

Let the rate of mean reversion matrix 𝚽\boldsymbol{\Phi} be given by

𝚽=(βˆ’0.357000.771βˆ’0.5110βˆ’0.450.729βˆ’0.693).\begin{equation} \boldsymbol{\Phi} = \left( \begin{array}{ccc} -0.357 & 0 & 0 \\ 0.771 & -0.511 & 0 \\ -0.45 & 0.729 & -0.693 \\ \end{array} \right) . \end{equation}

Let the dynamic process noise covariance matrix 𝚺\boldsymbol{\Sigma} be given by

𝚺=(0.24455560.0220159βˆ’0.05004760.02201590.0706780.0153946βˆ’0.05004760.01539460.0755306).\begin{equation} \boldsymbol{\Sigma} = \left( \begin{array}{ccc} 0.2445556 & 0.0220159 & -0.0500476 \\ 0.0220159 & 0.070678 & 0.0153946 \\ -0.0500476 & 0.0153946 & 0.0755306 \\ \end{array} \right) . \end{equation}

Let Ξ”t=0.1\Delta t = 0.1.

R Function Arguments

n
#> [1] 5
time
#> [1] 100
delta_t
#> [1] 0.1
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
mu
#> [1] 0 0 0
phi
#>        [,1]   [,2]   [,3]
#> [1,] -0.357  0.000  0.000
#> [2,]  0.771 -0.511  0.000
#> [3,] -0.450  0.729 -0.693
sigma
#>             [,1]       [,2]        [,3]
#> [1,]  0.24455556 0.02201587 -0.05004762
#> [2,]  0.02201587 0.07067800  0.01539456
#> [3,] -0.05004762 0.01539456  0.07553061
sigma_l # sigma_l <- t(chol(sigma))
#>             [,1]      [,2]     [,3]
#> [1,]  0.49452559 0.0000000 0.000000
#> [2,]  0.04451917 0.2620993 0.000000
#> [3,] -0.10120330 0.0759256 0.243975
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 Measurement Error and Process Noise (n = 5 with Different Initial Condition)

Using the SimSSMOUFixed Function from the simStateSpace Package to Simulate Data

library(simStateSpace)
sim <- SimSSMOUFixed(
  n = n,
  time = time,
  delta_t = delta_t,
  mu0 = mu0,
  sigma0_l = sigma0_l,
  mu = mu,
  phi = phi,
  sigma_l = sigma_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  0.29937539 -1.37581548 1.3779071
#> 2  1  0.1 -0.98770381 -0.03632195 0.8363080
#> 3  1  0.2  0.33221051 -0.40321664 1.2054318
#> 4  1  0.3 -0.09485392 -0.82030556 1.0272653
#> 5  1  0.4 -1.50322069 -0.36841853 0.1821731
#> 6  1  0.5 -0.75049839  0.35752476 0.2862544
summary(data)
#>        id         time             y1                 y2         
#>  Min.   :1   Min.   :0.000   Min.   :-1.58018   Min.   :-1.3758  
#>  1st Qu.:2   1st Qu.:2.475   1st Qu.:-0.35872   1st Qu.:-0.1613  
#>  Median :3   Median :4.950   Median : 0.03898   Median : 0.2119  
#>  Mean   :3   Mean   :4.950   Mean   : 0.06844   Mean   : 0.1827  
#>  3rd Qu.:4   3rd Qu.:7.425   3rd Qu.: 0.48802   3rd Qu.: 0.5598  
#>  Max.   :5   Max.   :9.900   Max.   : 2.48831   Max.   : 1.9520  
#>        y3          
#>  Min.   :-2.61894  
#>  1st Qu.:-0.25033  
#>  Median : 0.11593  
#>  Mean   : 0.09403  
#>  3rd Qu.: 0.46923  
#>  Max.   : 1.63714
plot(sim)

Model Fitting

Prepare Data

dynr_data <- dynr::dynr.data(
  dataframe = 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 ~ (phi_1_1 * (eta_1 - mu_1_1)) + (phi_1_2 * (eta_2 - mu_2_1)) + (phi_1_3 * (eta_3 - mu_3_1)),
    eta_2 ~ (phi_2_1 * (eta_1 - mu_1_1)) + (phi_2_2 * (eta_2 - mu_2_1)) + (phi_2_3 * (eta_3 - mu_3_1)),
    eta_3 ~ (phi_3_1 * (eta_1 - mu_1_1)) + (phi_3_2 * (eta_2 - mu_2_1)) + (phi_3_3 * (eta_3 - mu_3_1))
  ),
  startval = c(
    mu_1_1 = mu[1], mu_2_1 = mu[2], mu_3_1 = mu[3],
    phi_1_1 = phi[1, 1], phi_1_2 = phi[1, 2], phi_1_3 = phi[1, 3],
    phi_2_1 = phi[2, 1], phi_2_2 = phi[2, 2], phi_2_3 = phi[2, 3],
    phi_3_1 = phi[3, 1], phi_3_2 = phi[3, 2], phi_3_3 = phi[3, 3]
  ),
  isContinuousTime = TRUE
)

Prepare Process Noise

dynr_noise <- dynr::prep.noise(
  values.latent = sigma,
  params.latent = matrix(
    data = c(
      "sigma_1_1", "sigma_2_1", "sigma_3_1",
      "sigma_2_1", "sigma_2_2", "sigma_3_2",
      "sigma_3_1", "sigma_3_2", "sigma_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 = "ou.c"
)

Add lower and upper bounds to aid in the optimization.

model$lb[
  c(
    "phi_1_1",
    "phi_1_2",
    "phi_1_3",
    "phi_2_1",
    "phi_2_2",
    "phi_2_3",
    "phi_3_1",
    "phi_3_2",
    "phi_3_3"
  )
] <- -1.5
model$ub[
  c(
    "phi_1_1",
    "phi_1_2",
    "phi_1_3",
    "phi_2_1",
    "phi_2_2",
    "phi_2_3",
    "phi_3_1",
    "phi_3_2",
    "phi_3_3"
  )
] <- +1.5

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.05485509 0.1965643 0.1128741 -0.260079 
#> -0.2739031 0.02599305 0.7412146 -1.002955 0.2049504 -0.9824315 1.5 -1.091145 
#> -2.16558 0.1369349 0.1205415 -3.228083 0.03768187 -3.136258 -1.525 -1.617376 
#> -1.617815 0.007096181 -0.08813474 0.1935501 -1.572231 -0.3089997 0.7340702 
#> -0.8959006 -1.792513 -19.44445 
#> 
#> Transformed fitted parameters:  0.05485509 0.1965643 0.1128741 -0.260079 
#> -0.2739031 0.02599305 0.7412146 -1.002955 0.2049504 -0.9824315 1.5 -1.091145 
#> 0.1146834 0.01570416 0.01382411 0.04178386 0.003386464 0.04516772 0.2176211 
#> 0.1984187 0.1983315 0.007096181 -0.08813474 0.1935501 0.2075814 -0.06414261 
#> 0.1523794 0.4280598 -0.7788602 1.423573 
#> 
#> Doing end processing
#> Successful trial
#> Total Time: 42.79215 
#> Backend Time: 42.04188

Summary

summary(results)
#> Coefficients:
#>             Estimate Std. Error t value  ci.lower  ci.upper Pr(>|t|)    
#> mu_1_1      0.054855   0.106865   0.513 -0.154596  0.264307   0.3040    
#> mu_2_1      0.196564   0.096117   2.045  0.008178  0.384951   0.0207 *  
#> mu_3_1      0.112874   0.081013   1.393 -0.045909  0.271657   0.0821 .  
#> phi_1_1    -0.260079   0.290736  -0.895 -0.829911  0.309752   0.1857    
#> phi_1_2    -0.273903   0.339769  -0.806 -0.939837  0.392031   0.2103    
#> phi_1_3     0.025993   0.245872   0.106 -0.455907  0.507893   0.4579    
#> phi_2_1     0.741215   0.208182   3.560  0.333185  1.149244   0.0002 ***
#> phi_2_2    -1.002955   0.236381  -4.243 -1.466254 -0.539657   <2e-16 ***
#> phi_2_3     0.204950   0.195716   1.047 -0.178646  0.588546   0.1478    
#> phi_3_1    -0.982432   0.271613  -3.617 -1.514783 -0.450080   0.0002 ***
#> phi_3_2     1.500000   0.314911   4.763  0.882785  2.117215   <2e-16 ***
#> phi_3_3    -1.091145   0.262306  -4.160 -1.605255 -0.577035   <2e-16 ***
#> sigma_1_1   0.114683   0.046116   2.487  0.024297  0.205070   0.0066 ** 
#> sigma_2_1   0.015704   0.020912   0.751 -0.025284  0.056692   0.2265    
#> sigma_3_1   0.013824   0.028109   0.492 -0.041269  0.068917   0.3115    
#> sigma_2_2   0.041784   0.020256   2.063  0.002083  0.081485   0.0198 *  
#> sigma_3_2   0.003386   0.020189   0.168 -0.036184  0.042956   0.4334    
#> sigma_3_3   0.045168   0.039914   1.132 -0.033062  0.123398   0.1292    
#> theta_1_1   0.217621   0.016239  13.401  0.185792  0.249450   <2e-16 ***
#> theta_2_2   0.198419   0.013585  14.606  0.171793  0.225045   <2e-16 ***
#> theta_3_3   0.198331   0.014620  13.566  0.169677  0.226986   <2e-16 ***
#> mu0_1_1     0.007096   0.230800   0.031 -0.445263  0.459455   0.4877    
#> mu0_2_1    -0.088135   0.311038  -0.283 -0.697758  0.521489   0.3885    
#> mu0_3_1     0.193550   0.550317   0.352 -0.885051  1.272151   0.3626    
#> sigma0_1_1  0.207581   0.167343   1.240 -0.120405  0.535568   0.1077    
#> sigma0_2_1 -0.064143   0.176737  -0.363 -0.410541  0.282256   0.3584    
#> sigma0_3_1  0.152379   0.319638   0.477 -0.474099  0.778857   0.3169    
#> sigma0_2_2  0.428060   0.321404   1.332 -0.201880  1.058000   0.0918 .  
#> sigma0_3_2 -0.778860   0.533228  -1.461 -1.823968  0.266247   0.0724 .  
#> sigma0_3_3  1.423573   0.968975   1.469 -0.475582  3.322729   0.0712 .  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> -2 log-likelihood value at convergence = 2088.54
#> AIC = 2148.54
#> BIC = 2274.97
#> [1]  0.007096181 -0.088134741  0.193550087

Parameter Estimates

mu_hat
#> [1] 0.05485509 0.19656426 0.11287414
phi_hat
#>            [,1]       [,2]        [,3]
#> [1,] -0.2600790 -0.2739031  0.02599305
#> [2,]  0.7412146 -1.0029554  0.20495037
#> [3,] -0.9824315  1.5000000 -1.09114522
sigma_hat
#>            [,1]        [,2]        [,3]
#> [1,] 0.11468340 0.015704157 0.013824115
#> [2,] 0.01570416 0.041783864 0.003386464
#> [3,] 0.01382411 0.003386464 0.045167722
theta_hat
#>           [,1]      [,2]      [,3]
#> [1,] 0.2176211 0.0000000 0.0000000
#> [2,] 0.0000000 0.1984187 0.0000000
#> [3,] 0.0000000 0.0000000 0.1983315
mu0_hat
#> [1]  0.007096181 -0.088134741  0.193550087
sigma0_hat
#>             [,1]        [,2]       [,3]
#> [1,]  0.20758145 -0.06414261  0.1523794
#> [2,] -0.06414261  0.42805983 -0.7788602
#> [3,]  0.15237935 -0.77886015  1.4235730
beta_var1_hat <- expm::expm(phi_hat)
beta_var1_hat
#>            [,1]       [,2]         [,3]
#> [1,]  0.7085379 -0.1409334 0.0006109835
#> [2,]  0.3590317  0.3792895 0.0770292110
#> [3,] -0.2586161  0.5936353 0.3894688694

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
Chow, S.-M., Losardo, D., Park, J., & Molenaar, P. C. M. (2023). Continuous-time dynamic models: Connections to structural equation models and other discrete-time models. In R. H. Hoyle (Ed.), Handbook of structural equation modeling (2nd ed.). The Guilford Press.
Deboeck, P. R., & Preacher, K. J. (2015). No need to be discrete: A method for continuous time mediation analysis. Structural Equation Modeling: A Multidisciplinary Journal, 23(1), 61–75. https://doi.org/10.1080/10705511.2014.973960
Oravecz, Z., Tuerlinckx, F., & Vandekerckhove, J. (2011). A hierarchical latent stochastic differential equation model for affective dynamics. Psychological Methods, 16(4), 468–490. https://doi.org/10.1037/a0024375
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/
Uhlenbeck, G. E., & Ornstein, L. S. (1930). On the theory of the brownian motion. Physical Review, 36(5), 823–841. https://doi.org/10.1103/physrev.36.823