This function creates the dependent variable (Y) and predictor variable (X) matrices.
Arguments
- data
Numeric matrix. The time series data with dimensions
tbyk, wheretis the number of observations andkis the number of variables.- p
Integer. The order of the VAR model (number of lags).
- exo_mat
Numeric matrix. Matrix of exogenous variables with dimensions
tbym.
Value
List containing the dependent variable (Y)
and predictor variable (X) matrices.
Note that the resulting matrices will have t - p rows.
Details
The YX() function creates the Y and X matrices
required for fitting a Vector Autoregressive (VAR) model.
Given the input data matrix with dimensions t by k,
where t is the number of observations and k is the number of variables,
and the order of the VAR model p (number of lags),
the function constructs lagged predictor matrix X
and the dependent variable matrix Y.
The steps involved in creating the Y and X matrices are as follows:
Determine the number of observations
tand the number of variableskfrom the input data matrix.Create matrices
XandYto store lagged variables and the dependent variable, respectively.Populate the matrices
XandYwith the appropriate lagged data. The predictors matrixXcontains a column of ones and the lagged values of the dependent variables, while the dependent variable matrixYcontains the original values of the dependent variables.The function returns a list containing the
YandXmatrices, which can be used for further analysis and estimation of the VAR model parameters.
Examples
set.seed(42)
time <- 1000L
burn_in <- 200
k <- 3
p <- 2
constant <- c(1, 1, 1)
coef <- matrix(
data = c(
0.4, 0.0, 0.0, 0.1, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0, 0.2, 0.0,
0.0, 0.0, 0.6, 0.0, 0.0, 0.3
),
nrow = k,
byrow = TRUE
)
chol_cov <- chol(diag(3))
exo_mat <- MASS::mvrnorm(
n = time + burn_in,
mu = c(0, 0, 0),
Sigma = diag(3)
)
exo_coef <- matrix(
data = c(
0.5, 0.0, 0.0,
0.0, 0.5, 0.0,
0.0, 0.0, 0.5
),
nrow = 3
)
y <- SimVARExo(
time = time,
burn_in = burn_in,
constant = constant,
coef = coef,
chol_cov = chol_cov,
exo_mat = exo_mat,
exo_coef = exo_coef
)
yx <- YXExo(
data = y,
p = 2,
exo_mat = exo_mat[(burn_in + 1):(time + burn_in), ]
)
str(yx)
#> List of 2
#> $ Y: num [1:998, 1:3] 2.73 2.89 1.61 1.37 2.5 ...
#> $ X: num [1:998, 1:10] 1 1 1 1 1 1 1 1 1 1 ...