The Vector Autoregressive Model
Ivan Jacob Agaloos Pesigan
2025-03-27
Source:vignettes/var.Rmd
var.Rmd
Model
The measurement model is given by where represents a vector of observed variables and a vector of latent variables for individual and time . Since the observed and latent variables are equal, we only generate data from the dynamic structure.
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 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] 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
Using the SimSSMVARFixed
Function from the
simStateSpace
Package to Simulate Data
library(simStateSpace)
sim <- SimSSMVARFixed(
n = n,
time = time,
mu0 = mu0,
sigma0_l = sigma0_l,
alpha = alpha,
beta = beta,
psi_l = psi_l
)
data <- as.data.frame(sim)
head(data)
#> id time y1 y2 y3
#> 1 1 0 -1.84569501 0.5815402 0.8057225
#> 2 1 1 -1.34252674 -1.1219724 0.9873906
#> 3 1 2 -0.57123433 -1.1591679 0.1280274
#> 4 1 3 -0.44448720 -0.9783200 -0.3028425
#> 5 1 4 -0.28796224 -1.2222325 -0.3807219
#> 6 1 5 0.01622801 -1.2362032 -1.0056038
summary(data)
#> id time y1 y2
#> Min. : 1.0 Min. : 0.0 Min. :-3.237597 Min. :-3.0920279
#> 1st Qu.: 250.8 1st Qu.:249.8 1st Qu.:-0.299100 1st Qu.:-0.3947572
#> Median : 500.5 Median :499.5 Median :-0.000691 Median :-0.0001698
#> Mean : 500.5 Mean :499.5 Mean :-0.000459 Mean : 0.0001905
#> 3rd Qu.: 750.2 3rd Qu.:749.2 3rd Qu.: 0.298163 3rd Qu.: 0.3962505
#> Max. :1000.0 Max. :999.0 Max. : 2.804227 Max. : 3.1208824
#> y3
#> Min. :-3.289965
#> 1st Qu.:-0.348035
#> Median :-0.000256
#> Mean :-0.000300
#> 3rd Qu.: 0.347924
#> Max. : 2.951257
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 = matrix(data = 0, nrow = 3, ncol = 3),
params.observed = matrix(data = "fixed", nrow = 3, ncol = 3)
)
Prepare the Model
model <- dynr::dynr.model(
data = dynr_data,
initial = dynr_initial,
measurement = dynr_measurement,
dynamics = dynr_dynamics,
noise = dynr_noise,
outfile = "var.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.0001514855 0.0002004125 -0.0003490679 0.6993823
#> 0.0004247078 0.00028304 0.4989264 0.599653 -0.0007450518 -0.1020144 0.3997392
#> 0.5004114 -2.304735 0.001087328 -0.001322656 -2.302935 -0.000471382 -2.303606
#> -0.05600292 0.03466927 0.02662427 -0.0003182081 0.2387647 0.2162489 -0.04672307
#> 0.2424949 0.01927736
#>
#> Transformed fitted parameters: -0.0001514855 0.0002004125 -0.0003490679
#> 0.6993823 0.0004247078 0.00028304 0.4989264 0.599653 -0.0007450518 -0.1020144
#> 0.3997392 0.5004114 0.09978525 0.0001084992 -0.0001319816 0.09996515
#> -4.726522e-05 0.09989818 -0.05600292 0.03466927 0.02662427 0.9996818 0.2386888
#> 0.2161801 1.011342 0.2830416 1.122333
#>
#> Doing end processing
#> Successful trial
#> Total Time: 13.46473
#> Backend Time: 13.46456
Summary
summary(results)
#> Coefficients:
#> Estimate Std. Error t value ci.lower ci.upper Pr(>|t|)
#> alpha_1_1 -1.515e-04 3.160e-04 -0.479 -7.709e-04 4.679e-04 0.3159
#> alpha_2_1 2.004e-04 3.163e-04 0.634 -4.196e-04 8.204e-04 0.2632
#> alpha_3_1 -3.491e-04 3.162e-04 -1.104 -9.688e-04 2.707e-04 0.1348
#> beta_1_1 6.994e-01 8.035e-04 870.422 6.978e-01 7.010e-01 <2e-16 ***
#> beta_1_2 4.247e-04 6.761e-04 0.628 -9.004e-04 1.750e-03 0.2649
#> beta_1_3 2.830e-04 6.893e-04 0.411 -1.068e-03 1.634e-03 0.3407
#> beta_2_1 4.989e-01 8.044e-04 620.272 4.973e-01 5.005e-01 <2e-16 ***
#> beta_2_2 5.997e-01 6.770e-04 885.763 5.983e-01 6.010e-01 <2e-16 ***
#> beta_2_3 -7.451e-04 6.901e-04 -1.080 -2.098e-03 6.076e-04 0.1402
#> beta_3_1 -1.020e-01 8.042e-04 -126.860 -1.036e-01 -1.004e-01 <2e-16 ***
#> beta_3_2 3.997e-01 6.765e-04 590.860 3.984e-01 4.011e-01 <2e-16 ***
#> beta_3_3 5.004e-01 6.896e-04 725.605 4.991e-01 5.018e-01 <2e-16 ***
#> psi_1_1 9.979e-02 1.412e-04 706.902 9.951e-02 1.001e-01 <2e-16 ***
#> psi_2_1 1.085e-04 9.987e-05 1.086 -8.725e-05 3.042e-04 0.1387
#> psi_3_1 -1.320e-04 9.987e-05 -1.322 -3.277e-04 6.376e-05 0.0932 .
#> psi_2_2 9.997e-02 1.414e-04 706.894 9.969e-02 1.002e-01 <2e-16 ***
#> psi_3_2 -4.727e-05 9.993e-05 -0.473 -2.431e-04 1.486e-04 0.3181
#> psi_3_3 9.990e-02 1.413e-04 706.905 9.962e-02 1.002e-01 <2e-16 ***
#> mu0_1_1 -5.600e-02 3.202e-02 -1.749 -1.188e-01 6.748e-03 0.0401 *
#> mu0_2_1 3.467e-02 3.201e-02 1.083 -2.807e-02 9.741e-02 0.1394
#> mu0_3_1 2.662e-02 3.398e-02 0.784 -3.998e-02 9.323e-02 0.2167
#> sigma0_1_1 9.997e-01 4.605e-02 21.707 9.094e-01 1.090e+00 <2e-16 ***
#> sigma0_2_1 2.387e-01 3.285e-02 7.266 1.743e-01 3.031e-01 <2e-16 ***
#> sigma0_3_1 2.162e-01 3.466e-02 6.237 1.482e-01 2.841e-01 <2e-16 ***
#> sigma0_2_2 1.011e+00 4.633e-02 21.829 9.205e-01 1.102e+00 <2e-16 ***
#> sigma0_3_2 2.830e-01 3.481e-02 8.132 2.148e-01 3.513e-01 <2e-16 ***
#> sigma0_3_3 1.122e+00 5.053e-02 22.210 1.023e+00 1.221e+00 <2e-16 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> -2 log-likelihood value at convergence = 1609594.99
#> AIC = 1609648.99
#> BIC = 1609968.01
Parameter Estimates
alpha_hat
#> [1] -0.0001514855 0.0002004125 -0.0003490679
beta_hat
#> [,1] [,2] [,3]
#> [1,] 0.6993823 0.0004247078 0.0002830400
#> [2,] 0.4989264 0.5996530305 -0.0007450518
#> [3,] -0.1020144 0.3997392429 0.5004113876
psi_hat
#> [,1] [,2] [,3]
#> [1,] 0.0997852527 1.084992e-04 -1.319816e-04
#> [2,] 0.0001084992 9.996515e-02 -4.726522e-05
#> [3,] -0.0001319816 -4.726522e-05 9.989818e-02
mu0_hat
#> [1] -0.05600292 0.03466927 0.02662427
sigma0_hat
#> [,1] [,2] [,3]
#> [1,] 0.9996818 0.2386888 0.2161801
#> [2,] 0.2386888 1.0113421 0.2830416
#> [3,] 0.2161801 0.2830416 1.1223326