Fit the Discrete-Time Vector Autoregressive Model
Ivan Jacob Agaloos Pesigan
2025-08-03
Source:vignettes/multi-group.Rmd
multi-group.Rmd
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 matrix of factor loadings, and the covariance matrix of . In this model, is an identity matrix and is a diagonal matrix.
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 . In this model, is a symmetric matrix.
Data Generation
The parameters used in this example were based on Bringmann et al. (2013).
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 transition matrix be given by
Let the dynamic process noise be given by
R Function Arguments
n
#> [1] 1000
time
#> [1] 1000
mu0
#> [1] 3.814355 2.576348
sigma0
#> [,1] [,2]
#> [1,] 1.3978842 0.5782369
#> [2,] 0.5782369 1.6636513
sigma0_l # sigma0_l <- t(chol(sigma0))
#> [,1] [,2]
#> [1,] 1.1823215 0.000000
#> [2,] 0.4890691 1.193509
alpha
#> [1] 2.87 2.04
beta
#> [,1] [,2]
#> [1,] 0.280 -0.048
#> [2,] -0.035 0.260
psi
#> [,1] [,2]
#> [1,] 1.3000000 0.5696315
#> [2,] 0.5696315 1.5600000
psi_l # psi_l <- t(chol(psi))
#> [,1] [,2]
#> [1,] 1.1401754 0.000000
#> [2,] 0.4995998 1.144727
nu
#> [1] 0 0
lambda
#> [,1] [,2]
#> [1,] 1 0
#> [2,] 0 1
theta
#> [,1] [,2]
#> [1,] 0.2 0.0
#> [2,] 0.0 0.2
theta_l # theta_l <- t(chol(theta))
#> [,1] [,2]
#> [1,] 0.4472136 0.0000000
#> [2,] 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
)
data <- as.data.frame(sim)
head(data)
#> id time y1 y2
#> 1 1 0 3.455114 1.9223092
#> 2 1 1 5.696312 0.6452660
#> 3 1 2 4.364664 1.4954592
#> 4 1 3 4.900281 2.6730811
#> 5 1 4 2.325515 0.2139305
#> 6 1 5 3.340405 1.9930331
plot(sim)
Model Fitting
The FitDTVARMx
function fits a DT-VAR model assuming
fixed parameters.
library(fitDTVARMx)
fit <- FitDTVARMx(
data = data,
observed = paste0("y", seq_len(k)),
id = "id",
ncores = parallel::detectCores()
)
#> Running DTVAR with 16 parameters
#>
#> Beginning initial fit attempt
#> Running DTVAR with 16 parameters
#>
#> Lowest minimum so far: 6560584.96448053
#>
#> Solution found
#>
#> Solution found! Final fit=6560585 (started at 112160520) (1 attempt(s): 1 valid, 0 errors)
#> Start values from best fit:
#> 0.238845204438278,0.385079342937541,0.192341329589063,-0.319501502562121,2.40734832712481,1.93273981750249,0.765165356271534,0.552642713133481,7.5802858677544e-14,0.730254135746907,1.89415411434006,1,1,7.94885699044023,4.61237397585988,2.14952390785434
summary(fit)
#> Summary of DTVAR
#>
#> free parameters:
#> name matrix row col Estimate Std.Error A
#> 1 beta_11 DTVAR_1.beta eta1 eta1 2.388452e-01 NA
#> 2 beta_21 DTVAR_1.beta eta2 eta1 3.850793e-01 NA
#> 3 beta_12 DTVAR_1.beta eta1 eta2 1.923413e-01 NA
#> 4 beta_22 DTVAR_1.beta eta2 eta2 -3.195015e-01 NA
#> 5 alpha_1 DTVAR_1.gamma eta1 1 2.407348e+00 NA
#> 6 alpha_2 DTVAR_1.gamma eta2 1 1.932740e+00 NA
#> 7 psi_11 DTVAR_1.psi eta1 eta1 7.651654e-01 NA !
#> 8 psi_21 DTVAR_1.psi eta1 eta2 5.526427e-01 0.001584757
#> 9 psi_22 DTVAR_1.psi eta2 eta2 7.580286e-14 NA !
#> 10 theta_11 DTVAR_1.theta y1 y1 7.302541e-01 NA
#> 11 theta_22 DTVAR_1.theta y2 y2 1.894154e+00 NA
#> 12 mu0_1 DTVAR_1.mu0 eta1 mu0 1.000000e+00 NA !
#> 13 mu0_2 DTVAR_1.mu0 eta2 mu0 1.000000e+00 0.095680038 !
#> 14 sigma0_11 DTVAR_1.sigma0 1 1 7.948857e+00 NA !
#> 15 sigma0_21 DTVAR_1.sigma0 1 2 4.612374e+00 0.272433470 !
#> 16 sigma0_22 DTVAR_1.sigma0 2 2 2.149524e+00 0.304208784 !
#> lbound ubound
#> 1
#> 2
#> 3
#> 4
#> 5
#> 6
#> 7 2.2250738585072e-308
#> 8
#> 9 0!
#> 10 2.2250738585072e-308
#> 11 2.2250738585072e-308
#> 12 -1 1!
#> 13 -1 1!
#> 14 2.2250738585072e-308
#> 15
#> 16 2.2250738585072e-308
#>
#> Model Statistics:
#> | Parameters | Degrees of Freedom | Fit (-2lnL units)
#> Model: 16 1999984 6560585
#> Saturated: NA NA NA
#> Independence: NA NA NA
#> Number of observations/statistics: 1e+06/2e+06
#>
#> Information Criteria:
#> | df Penalty | Parameters Penalty | Sample-Size Adjusted
#> AIC: 2560617 6560617 6560617
#> BIC: -21070215 6560806 6560755
#> To get additional fit indices, see help(mxRefModels)
#> timestamp: 2025-08-03 20:34:21
#> Wall clock time: 3704.049 secs
#> optimizer: SLSQP
#> OpenMx version number: 2.22.7
#> Need help? See help(mxSummary)