Repository navigation
Fit VAR model
How to fit a VAR(p) model using the function fitVAR.
fitVAR(data, p = 1, penalty = "ENET", method = "cv", ...)The possible parameters are:
-
data: the matrix containing the time series (row -> observations, cols -> variables); -
p: order of the VAR process -
penalty: the penalty to use in the least squares. Possible values are:"ENET","SCAD"and"MCP"; -
method: the method to use to estimate the sparsity parameter(s). Possible values are"cv"(cross validation) and"timeSlice"(time slicing); -
...: other options that can be global or depend on the method or the penalty used.
-
scale: logical. IfTRUErescales the data; default value isFALSE. -
threshold:TRUEorFALSE; the default value isFALSE. -
thresholdType:"hard"or"soft"; the default value is"soft".
-
nfolds: number of folds in the cross validation (only formethod = "cv"); -
foldsIDs: fix the IDs of the folds (during the cross validation), in crescent order (TRUEorFALSE(default)); -
nlambda: the number of lambdas to use in the cross validation (default = 100); -
type.measure:"mae"or"mse"(default). The measure to be used in cross validation; -
lambda: the lambda to use after the cross validation. Possible values are"lambda.min"or"lambda.1se". -
parallel: logical; to use parallelization during cross validation; -
ncores: the number of cores to use whenparallel = TRUE.
-
leaveOut: number of observations to leave out (only formethod = "timeSlice"); -
horizon: the horizon to use for estimating mse/mae (default = 1).
-
alpha: a value in[0,1].0is Ridge regression,1is LASSO (default);
-
picasso: logical. Determines if you want to use picasso package for SCAD estimation (only whenmethod = "timeSlice"); -
eps: tolerance.
An S3 object of the class var with type fit, containing:
-
mu: the estimated vector mean of the process; -
A: a list containing thepestimated matrices of the VAR model; -
lambda: the "best" lambda obtained from cross validation or time slicing; -
mse: the mean square error; -
mseSD: the mean square error standard deviation; -
time: elapsed time; -
series: the (transformed: centered and/or scaled) time series; -
residuals: the time series of the residuals; -
sigma: the variance/covariance matrix of the residuals; -
penalty: the penalty used; -
method: the method used.
First we simulate a VAR(3) model with sparsity = 0.1.
set.seed(10101)
sim <- simulateVAR(N = 20, p = 3, sparsity = 0.1)Then we estimate a VAR(3) model using the data we have created before:
fit <- fitVAR(sim$series, p = 3, penalty = "ENET", method = "cv")
plotVAR(sim, fit)
Let's check the variance/covariance of the residuals:
plotMatrix(sim$sigma)
plotMatrix(fit$sigma)

Let's use the same simulation of the previous example to see how the option threshold = TRUE works.
fit_Thresh <- fitVAR(sim$series, p = 3, penalty = "ENET", method = "cv", threshold = TRUE)
plotVAR(fit, fit_Thresh)
In the image above we can notice the difference of the estimated VAR with threshold = FALSE (first row) and the fitted VAR with threshold = TRUE (second row). All the coefficients of the matrices with absolute value smaller than the threshold are set to zero. The value of the threshold is

where p and K are the order and the dimension of the VAR respectively and N is the number of observations. Let us compare the simulated VAR with the thresholded one.
plotVAR(sim, fit_Thresh)
sparsevar package wiki