Model
The measurement model is given by where , , and are random variables and , , and are model parameters. represents a vector of observed random variables, a vector of latent random variables, and a vector of random measurement errors, at time and individual . denotes a vector of intercepts, a matrix of factor loadings, and the covariance matrix of .
An alternative representation of the measurement error is given by where is a vector of independent standard normal random variables and .
The dynamic structure is given by where , , and are random variables, and , , and are model parameters. Here, is a vector of latent variables at time and individual , represents a vector of latent variables at time and individual , and represents a vector of dynamic noise at time and individual . denotes a vector of intercepts, a matrix of autoregression and cross regression coefficients, and the covariance matrix of .
An alternative representation of the dynamic noise is given by where .
Data Generation
Notation
Let be the number of time points and be the number of individuals.
Let the measurement model intecept vector be given by
Let the factor loadings matrix be given by
Let the measurement error covariance matrix be given by
Let the initial condition be given by
Let the constant vector be given by
Let the transition matrix be given by
Let the dynamic process noise be given by
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
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 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