sparsevar is an R package that estimates sparse VAR and VECM model using penalized least squares methods (PLS): it is possible to use various penalties such as ENET, SCAD or MC+ penalties. The sparsity parameter can be estimated using cross-validation or time slicing. When using ENET it is possible to estimate VAR(1) of dimension up to 200, while when using one of the other two is better not to go beyond 50. When estimating a VAR(\(p\)) model then the limits are roughly \(200/p\) and \(50/p\), respectively.

The authors of sparsevar are Monica Billio, Lorenzo Frattarolo and Simone Vazzoler and the R package is mantained by Simone Vazzoler. This vignette describes the usage of sparsevar in R.


The simplest way to install the package is by using the CRAN repositories, by typing in the R console

install.packages("sparsevar", repos = "")

It is also possible to install the developing version of the package by typing

install.packages("devtools", repos = "")

Quick start

To load the sparsevar package simply type


Using a function included in the package, we simply generate a \(20\times 20\) VAR\((2)\) process

sim <- simulateVAR(N = 20, p = 2)

and we can estimate the matrices of the process using

fit <- fitVAR(sim$series, p = 2)

The results can be seen by plotting the matrices

plotVAR(sim, fit)

plot of chunk unnamed-chunk-6

Description of the package's functions

Estimation of a VAR or VECM models

Use fitVAR for VAR model estimation or fitVECM for VECM estimation.

The common arguments for the two functions are:

Global options

Options for penalty = "ENET"

Options for penalty = "SCAD" or "MCP"


The output of the function fitVAR is a S3 object of class var containing:

Simulation of VAR models

Use simulateVAR. The parameters for the function are:

Estimation of Impulse Response function

Use the functions impulseResponse and errorBands to compute the impulse response function and to estimate the error bands of the model respectively.

irf <- impulseResponse(fit)
eb <- errorBandsIRF(fit, irf)


Estimations' examples

results <- fitVAR(rets)

will estimate VAR(1) process using LASSO regression on the dataset rets.

The command

results <- fitVAR(rets, p = 3, penalty = "ENET", parallel = TRUE,
                  ncores = 5, alpha = 0.95, type.measure = "mae",
                  lambda = "lambda.1se")

will estimate a VAR(3) model on the dataset rets using the penalty "ENET" with alpha = 0.95 (between LASSO and Ridge). For the cross validation it will use "mae" (mean absolute error) insteadof mean square error and it will choose as model the one correspondent to the lambda which is at 1 std deviation from the minimum. Moreover it will parallelize the cross validation over 5 cores.

IRF example

Here we compute the IRF for the model estimated in the Quick Start section.

irf <- impulseResponse(fit)
eb <- errorBandsIRF(fit, irf, verbose = FALSE)
plotIRFGrid(irf, eb, indexes = c(11,20))

Simulations' examples

sim <- simulateVAR(N = 100, nobs = 250, rho = 0.75, sparsity = 0.05, method = "normal")