Skip to content

Fit VAR model

Simone Vazzoler edited this page Apr 24, 2017 · 2 revisions

How to fit a VAR(p) model using the function fitVAR.

Usage

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.

Global options

  • scale: logical. If TRUE rescales the data; default value is FALSE.
  • threshold: TRUE or FALSE; the default value is FALSE.
  • thresholdType: "hard" or "soft"; the default value is "soft".

Options depending on the method

CV options

  • nfolds: number of folds in the cross validation (only for method = "cv");
  • foldsIDs: fix the IDs of the folds (during the cross validation), in crescent order (TRUE or FALSE (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 when parallel = TRUE.

Timeslicing options

  • leaveOut: number of observations to leave out (only for method = "timeSlice");
  • horizon: the horizon to use for estimating mse/mae (default = 1).

Options depending on the penalty used

LASSO

  • alpha: a value in [0,1]. 0 is Ridge regression, 1 is LASSO (default);

SCAD and MCP

  • picasso: logical. Determines if you want to use picasso package for SCAD estimation (only when method = "timeSlice");
  • eps: tolerance.

Output

An S3 object of the class var with type fit, containing:

  • mu: the estimated vector mean of the process;
  • A: a list containing the p estimated 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.

Example

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)

fitVAR3

Let's check the variance/covariance of the residuals:

plotMatrix(sim$sigma)
plotMatrix(fit$sigma)

vcovSim vcovFit

Threshold Usage

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)

fitVSfit_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

threshold_formula

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)

simVSfit_Thresh