Fit the Discrete-Time Vector Autoregressive Model By ID
Ivan Jacob Agaloos Pesigan
2025-08-19
Source:vignettes/by-id.Rmd
by-id.Rmd
Note: When the discrete-time vector autoregressive model is estimated separately for each ID, any of the parameters can vary across individuals. In this example, however, we are only varying and .
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 for individual , a matrix of autoregression and cross regression coefficients for individual , 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 intercept vector be normally distributed with the following means
and covariance matrix
Let the transition matrix be normally distributed with the following means
and covariance matrix
The SimAlphaN
and SimBetaN
functions from
the simStateSpace
package generates random intercept
vectors and transition matrices from the multivariate normal
distribution. Note that the SimBetaN
function generates
transition matrices that are weakly stationary.
Let the dynamic process noise be given by
R Function Arguments
n
#> [1] 1000
time
#> [1] 1000
mu0
#> [[1]]
#> [1] 3.814355 2.576348
sigma0
#> [[1]]
#> [,1] [,2]
#> [1,] 1.3978842 0.5782369
#> [2,] 0.5782369 1.6636513
sigma0_l # sigma0_l <- t(chol(sigma0))
#> [[1]]
#> [,1] [,2]
#> [1,] 1.1823215 0.000000
#> [2,] 0.4890691 1.193509
# first alpha in the list of length n
alpha[[1]]
#> [1] 1.382816 2.056527
# first beta in the list of length n
beta[[1]]
#> [,1] [,2]
#> [1,] 0.21090478 0.00660174
#> [2,] 0.01360809 0.37057228
psi
#> [,1] [,2]
#> [1,] 1.3000000 0.5696315
#> [2,] 0.5696315 1.5600000
psi_l # psi_l <- t(chol(psi))
#> [[1]]
#> [,1] [,2]
#> [1,] 1.1401754 0.000000
#> [2,] 0.4995998 1.144727
nu
#> [[1]]
#> [1] 0 0
lambda
#> [[1]]
#> [,1] [,2]
#> [1,] 1 0
#> [2,] 0 1
theta
#> [,1] [,2]
#> [1,] 0.2 0.0
#> [2,] 0.0 0.2
Using the SimSSMIVary
Function from the
simStateSpace
Package to Simulate Data
library(simStateSpace)
sim <- SimSSMIVary(
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.040533 3.202203
#> 2 1 1 3.630653 4.691667
#> 3 1 2 2.621350 5.240797
#> 4 1 3 2.073843 5.555230
#> 5 1 4 2.013430 5.269451
#> 6 1 5 1.677296 2.836631
plot(sim)
Model Fitting
The FitDTVARIDMx
function fits a DT-VAR model on each
individual
.
library(fitDTVARMx)
fit <- FitDTVARIDMx(
data = data,
observed = paste0("y", seq_len(k)),
id = "id",
mu0_values = mu0[[1]],
sigma0_l_values = sigma0_l,
ncores = parallel::detectCores()
)
#> Error in sigma0_l_values %*% t(sigma0_l_values): requires numeric/complex matrix/vector arguments
summary(fit, means = FALSE)
#> Error: object 'fit' not found
summary(fit, means = TRUE)
#> Error: object 'fit' not found