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 author of sparsevar is 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 = "http://cran.us.r-project.org")
It is also possible to install the developing version of the package by typing
install.packages("devtools", repos = "http://cran.us.r-project.org") devtools::install_github("svazzole/sparsevar")
To load the sparsevar package simply type
library(sparsevar)
Using a function included in the package, we simply generate a $20\times 20$ VAR$(2)$ process
set.seed(1) sim <- simulate_var(n = 20, p = 2)
and we can estimate the matrices of the process using
fit <- fit_var(sim$series, p = 2) fit_2 <- fit_var(sim$series, p = 2, threshold = TRUE)
Note that we created two different estimations: the first one is the default one which
retains all the coefficients. The second one is when we fix the threshold to TRUE, setting
all the "small" coefficients to zero. The "soft" version of the threshold is
$$
t = \frac{1}{\sqrt{p n_c \log{n_r}}}
$$
The results can be seen by plotting the matrices
plot_var(sim, fit, fit_2)
Use fit_var for VAR model estimation or fit_vecm for VECM estimation.
The common arguments for the two functions are:
data: a matrix containing the multivariate time series (variables in columns, observations in rows);p: the order of the VAR model to be estimated; default p = 1 for fit_var and p=2
for fit_vecm.method: the method used to estimate the sparsity parameter. Default is method = "cv"
(cross-validation). Another possibility is method = "timeSlice".penalty: the penalty used in least squares. Possible values are: "ENET", "SCAD" or "MCP";...: sequence of options. Some of them depend on the penalty used, some on the method
and some are global.parallel: TRUE or FALSE (default). Parallel cross-validation (on the folds);ncores: if parallel = TRUE then you must specify the number of cores used for the parallelization (default = 1).nfolds: number of folds to use in the cross validation (default nfolds = 10)threshold: TRUE or FALSE (default). If TRUE all the elements of the VAR/VECM
matrices that are small "enough" are set to 0.penalty = "ENET"lambda: "lambda.min" (default) or "lambda.1se";alpha: a value in (0,1) (default alpha = 1). alpha = 1 is LASSO regression, alpha = 0 is Ridge LS;type.measure: "mse" (default) or "mae";nlambda: number of lambdas used for cross validation.foldsID: the vector containing the IDs for the folds in the cross validation.penalty = "SCAD" or "MCP"eps: convergence toleranceThe output of the function fit_var is a S3 object of class var containing:
mu: a vector for the mean;A: a list of length p containing the matrices estimated for the VAR(p) model;lambda: the estimated sparsity parameter;mse: the mean square error of the cross validation or time slicing;time: elapsed time for the estimation;series: the transformed data matrix (centered or scaled);residuals: the matrix of the estimated residuals;sigma: the variance/covariance matrix of the residuals;penalty: the penalty used (ENET, SCAD or MCP);method: the method used ("cv" or "timeSlice").Use simulate_var. The parameters for the function are:
n: the dimension of the process;nobs: the number of observations of the process;rho: the variance/covariance "intensity";sparsity: the percentage of non zero elements in the matrix of the VAR;method: "normal" or "bimodal".Use the functions impulseResponse and errorBands to compute the impulse response
function and to estimate the error bands of the model respectively.
irf <- impulse_response(fit) eb <- error_bands_irf(fit, irf)
results <- fit_var(rets)
will estimate VAR(1) process using LASSO regression on the dataset rets.
The command
results <- fit_var(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) instead of 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.
Here we compute the IRF for the model estimated in the Quick Start section.
irf_2 <- impulse_response(fit_2) eb_2 <- error_bands_irf(fit_2, irf_2, verbose = FALSE) plot_irf_grid(irf_2, eb_2, indexes = c(2, 3, 4, 11))
sim <- simulate_var(n = 100, nobs = 250, rho = 0.75, sparsity = 0.05, method = "normal")
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.