| Title: | Sparse VAR (Vector Autoregression) / VECM (Vector Error Correction Model) Estimation |
|---|---|
| Description: | A wrapper for sparse VAR (Vector Autoregression) and VECM (Vector Error Correction Model) time series models estimation using penalties like ENET (Elastic Net), SCAD (Smoothly Clipped Absolute Deviation) and MCP (Minimax Concave Penalty). Based on the work of Basu and Michailidis (2015) <doi:10.1214/15-AOS1315>. |
| Authors: | Simone Vazzoler [aut, cre] |
| Maintainer: | Simone Vazzoler <[email protected]> |
| License: | GPL-2 |
| Version: | 1.0.0 |
| Built: | 2026-06-05 07:02:20 UTC |
| Source: | https://github.com/svazzole/sparsevar |
Compute the accuracy of a fit
accuracy(reference_m, a)accuracy(reference_m, a)
reference_m |
the matrix to use as reference |
a |
the matrix obtained from a fit |
A numeric value between 0 and 1 representing the accuracy of the fit, computed as the proportion of correctly identified zero/non-zero entries.
Build the bootstrapped series from the original var
bootstrapped_var(v)bootstrapped_var(v)
v |
the VAR object as from fitVAR or simulateVAR |
A matrix containing the bootstrapped time series with the same dimensions as the original series.
A function to find which entries of the impulse response function are zero.
check_impulse_zero(irf)check_impulse_zero(irf)
irf |
irf output from impulseResponse function |
a matrix containing the indices of the impulse response function that are 0.
Check if the input is a var object
check_is_var(v)check_is_var(v)
v |
the object to test |
A logical value: TRUE if the input is a var or varx object,
FALSE otherwise.
Build the VAR(1) representation of a VAR(p) process
companion_var(v)companion_var(v)
v |
the VAR object as from |
A sparse matrix (of class dgCMatrix) representing the
companion form of the VAR(p) process.
This function computes forecasts for a given VAR.
compute_forecasts(v, num_steps)compute_forecasts(v, num_steps)
v |
a VAR object as from fitVAR. |
num_steps |
the number of forecasts to produce. |
A matrix of dimension (number of variables) x (num_steps) containing the forecasted values for each variable at each forecast horizon.
Creates a sparse square matrix with a given sparsity and distribution.
create_sparse_matrix( n, sparsity, method = "normal", stationary = FALSE, p = 1, ... )create_sparse_matrix( n, sparsity, method = "normal", stationary = FALSE, p = 1, ... )
n |
the dimension of the square matrix |
sparsity |
the density of non zero elements |
method |
the method used to generate the entries of the matrix.
Possible values are |
stationary |
should the spectral radius of the matrix be smaller than 1?
Possible values are |
p |
normalization constant (used for VAR of order greater than 1, default = 1) |
... |
other options for the matrix (you can specify the mean
|
An nxn sparse matrix.
M <- create_sparse_matrix( n = 30, sparsity = 0.05, method = "normal", stationary = TRUE )M <- create_sparse_matrix( n = 30, sparsity = 0.05, method = "normal", stationary = TRUE )
A function to estimate a (possibly big) multivariate VECM time series using penalized least squares methods, such as ENET, SCAD or MC+.
decompose_pi(vecm, rk, ...)decompose_pi(vecm, rk, ...)
vecm |
the VECM object |
rk |
rank |
... |
options for the function (TODO: specify) |
alpha
beta
A function to estimate the confidence intervals for irf and oirf.
error_bands_irf(v, irf, alpha, m, resampling, ...)error_bands_irf(v, irf, alpha, m, resampling, ...)
v |
a var object as from fitVAR or simulateVAR |
irf |
irf output from impulseResponse function |
alpha |
level of confidence (default |
m |
number of bootstrapped series (default |
resampling |
type of resampling: |
... |
some options for the estimation: |
a matrix containing the indices of the impulse response function that are 0.
A function to estimate a (possibly high-dimensional) multivariate VAR time series using penalized least squares methods, such as ENET, SCAD or MC+.
fit_var(data, p = 1, penalty = "ENET", method = "cv", ...)fit_var(data, p = 1, penalty = "ENET", method = "cv", ...)
data |
the data from the time series: variables in columns and observations in rows |
p |
order of the VAR model |
penalty |
the penalty function to use. Possible values
are |
method |
possible values are |
... |
the options for the estimation. Global options are:
|
A the list (of length p) of the estimated matrices
of the process
fit the results of the penalized LS estimation
mse the mean square error of the cross validation
time elapsed time for the estimation
residuals the time series of the residuals
A function to estimate a (possibly high-dimensional) multivariate VARX time series using penalized least squares methods, such as ENET, SCAD or MC+.
fit_varx(data, p = 1, xt_matrix, m = 1, penalty = "ENET", method = "cv", ...)fit_varx(data, p = 1, xt_matrix, m = 1, penalty = "ENET", method = "cv", ...)
data |
the data from the time series: variables in columns and observations in rows |
p |
order of the VAR model |
xt_matrix |
the exogenous variables |
m |
order of the exogenous variables |
penalty |
the penalty function to use. Possible values are
|
method |
possible values are |
... |
the options for the estimation. Global options are:
|
A the list (of length p) of the estimated matrices
of the process
fit the results of the penalized LS estimation
mse the mean square error of the cross validation
time elapsed time for the estimation
residuals the time series of the residuals
A function to estimate a (possibly big) multivariate VECM time series using penalized least squares methods, such as ENET, SCAD or MC+.
fit_vecm(data, p, penalty, method, log_scale, ...)fit_vecm(data, p, penalty, method, log_scale, ...)
data |
the data from the time series: variables in columns and observations in rows |
p |
order of the VECM model |
penalty |
the penalty function to use.
Possible values are |
method |
|
log_scale |
should the function consider the
|
... |
options for the function (TODO: specify) |
Pi the matrix Pi for the VECM model
G the list (of length p-1) of the
estimated matrices of the process
fit the results of the penalized LS estimation
mse the mean square error of the cross validation
time elapsed time for the estimation
Compute the Frobenius norm of m
frob_norm(m)frob_norm(m)
m |
the matrix (real or complex valued) |
A numeric value representing the Frobenius norm of the matrix.
A function to estimate the Impulse Response Function of a given VAR.
impulse_response(v, len = 20)impulse_response(v, len = 20)
v |
the data in the for of a VAR |
len |
length of the impulse response function |
irf a 3d array containing the impulse response function.
This function computes information criteria (AIC, Schwartz and Hannan-Quinn) for VARs.
inform_crit(v)inform_crit(v)
v |
a list of VAR objects as from fitVAR. |
A data frame with columns AIC, BIC, and
HannanQuinn containing the information criteria values for each
VAR model in the input list.
Compute the L-infinity matrix norm of m
l_infty_norm(m)l_infty_norm(m)
m |
the matrix (real or complex valued) |
A numeric value representing the L-infinity matrix norm (maximum absolute row sum).
Compute the L1 matrix norm of m
l1norm(m)l1norm(m)
m |
the matrix (real or complex valued) |
A numeric value representing the L1 matrix norm (maximum absolute column sum).
Compute the L2 matrix norm of M
l2norm(m)l2norm(m)
m |
the matrix (real or complex valued) |
A numeric value representing the L2 matrix norm.
Compute the max-norm of m
max_norm(m)max_norm(m)
m |
the matrix (real or complex valued) |
A numeric value representing the maximum absolute entry of the matrix.
Multiple plot function. ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
multiplot(..., plotlist = NULL, cols = 1, layout = NULL)multiplot(..., plotlist = NULL, cols = 1, layout = NULL)
... |
a sequence of ggplots to be plotted in the grid. |
plotlist |
a list containing ggplots as elements. |
cols |
number of columns in layout |
layout |
a matrix specifying the layout. If present, 'cols' is ignored. If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE), then plot 1 will go in the upper left, 2 will go in the upper right, and 3 will go all the way across the bottom. Taken from R Cookbook |
A ggplot containing the plots passed as arguments
Plot a IRF object
plot_irf(irf, eb, i, j, type, bands)plot_irf(irf, eb, i, j, type, bands)
irf |
the irf object to plot |
eb |
the errorbands to plot |
i |
the first index |
j |
the second index |
type |
|
bands |
|
An image plot relative to the impulse response function.
Plot a IRF grid object
plot_irf_grid(irf, eb, indexes, type, bands)plot_irf_grid(irf, eb, indexes, type, bands)
irf |
the irf object computed using impulseResponse |
eb |
the error bands estimated using errorBands |
indexes |
a vector containing the indices that you want to plot |
type |
plot the irf ( |
bands |
which type of bands to plot ("quantiles" (default) or "sd") |
An image plot relative to the impulse response function.
Plot a sparse matrix
plot_matrix(m, colors)plot_matrix(m, colors)
m |
the matrix to plot |
colors |
dark or light |
An image plot with a particular color palette
(black zero entries, red for the negative ones and
green for the positive)
Plot all the matrices of a VAR model
plot_var(..., colors)plot_var(..., colors)
... |
a sequence of VAR objects (one or more
than one, as from |
colors |
the gradient used to plot the matrix. It can be "light" (low = red – mid = white – high = blue) or "dark" (low = red – mid = black – high = green) |
An image plot with a specific color palette
Plot all the matrices of a VECM model
plot_vecm(v)plot_vecm(v)
v |
a VECM object (as from |
An image plot with a specific color palette
(black zero entries, red for the negative ones and green for
the positive)
This function generates a simulated multivariate VAR time series.
simulate_var(n, p, nobs, rho, sparsity, mu, method, covariance, ...)simulate_var(n, p, nobs, rho, sparsity, mu, method, covariance, ...)
n |
dimension of the time series (default |
p |
number of lags of the VAR model (default |
nobs |
number of observations to be generated (default
|
rho |
base value for the covariance matrix (default |
sparsity |
density (in percentage) of the number of nonzero elements
of the VAR matrices (default |
mu |
a vector containing the mean of the simulated process (default
|
method |
which method to use to generate the VAR matrix. Possible values
are |
covariance |
type of covariance matrix to use in the simulation.
Possible values: |
... |
the options for the simulation. These are:
|
A a list of NxN matrices ordered by lag
data a list with two elements: series the multivariate
time series and noises the time series of errors
S the variance/covariance matrix of the process
This function generates a simulated multivariate VAR time series.
simulate_varx(n, k, p, m, nobs, rho, sparsity_a1, sparsity_a2, sparsity_a3, mu, method, covariance, ...)simulate_varx(n, k, p, m, nobs, rho, sparsity_a1, sparsity_a2, sparsity_a3, mu, method, covariance, ...)
n |
dimension of the time series. |
k |
TODO |
p |
number of lags of the VAR model. |
m |
TODO |
nobs |
number of observations to be generated. |
rho |
base value for the covariance matrix. |
sparsity_a1 |
density (in percentage) of the number of nonzero elements of the A1 block. |
sparsity_a2 |
density (in percentage) of the number of nonzero elements of the A2 block. |
sparsity_a3 |
density (in percentage) of the number of nonzero elements of the A3 block. |
mu |
a vector containing the mean of the simulated process. |
method |
which method to use to generate the VAR matrix. Possible values
are |
covariance |
type of covariance matrix to use in the simulation.
Possible values: |
... |
the options for the simulation. These are:
|
A a list of NxN matrices ordered by lag
data a list with two elements: series the multivariate time
series and noises the time series of errors
S the variance/covariance matrix of the process
It performs the estimation of the matrices of the models using penalized least squares methods such as LASSO, SCAD and MCP.
fit_var, fit_vecm, simulate_var,
create_sparse_matrix,
plotMatrix, plotVAR, plotVECM
l2norm, l1norm, lInftyNorm, maxNorm,
frobNorm,
spectralRadius, spectralNorm, impulseResponse
Maintainer: Simone Vazzoler [email protected]
Useful links:
Compute the spectral norm of m
spectral_norm(m)spectral_norm(m)
m |
the matrix (real or complex valued) |
A numeric value representing the spectral norm (largest singular value).
Compute the spectral radius of m
spectral_radius(m)spectral_radius(m)
m |
the matrix (real or complex valued) |
A numeric value representing the spectral radius (largest absolute eigenvalue).
This function should retain only the coefficients of the matrices of the VAR that are statistically significative (from the bootstrap)
test_granger(v, eb)test_granger(v, eb)
v |
the VAR object as from fitVAR or simulateVAR |
eb |
the error bands as obtained from errorBands |
A list of matrices containing only the statistically significant VAR coefficients (non-significant coefficients are set to zero).
Transform the input data
transform_data(data, p, opt)transform_data(data, p, opt)
data |
the data |
p |
the order of the VAR |
opt |
a list containing the options |
A list containing:
X: the design matrix for the VAR estimation
y: the response vector
series: the (possibly centered/scaled) data matrix
mu: the column means used for centering
Computes the True Negative Rate (Specificity) between a reference matrix and an estimated matrix. TNR = TN / (TN + FP), where negatives are zero entries in the reference matrix.
true_negative_rate(reference_m, a)true_negative_rate(reference_m, a)
reference_m |
the reference (ground truth) matrix |
a |
the estimated matrix to compare against the reference |
The true negative rate (between 0 and 1), or NA if there are no actual negatives in the reference matrix
Computes the True Positive Rate (Sensitivity/Recall) between a reference matrix and an estimated matrix. TPR = TP / (TP + FN), where positives are non-zero entries in the reference matrix.
true_positive_rate(reference_m, a)true_positive_rate(reference_m, a)
reference_m |
the reference (ground truth) matrix |
a |
the estimated matrix to compare against the reference |
The true positive rate (between 0 and 1), or NA if there are no actual positives in the reference matrix
Estimate VAR using ENET penalty
var_enet(data, p, lambdas, opt)var_enet(data, p, lambdas, opt)
data |
the data |
p |
the order of the VAR |
lambdas |
a vector containing the lambdas to be used in the fit |
opt |
a list containing the options |
A glmnet object containing the fitted model.
Estimate VAR using MCP penalty
var_mcp(data, p, lambdas, opt)var_mcp(data, p, lambdas, opt)
data |
the data |
p |
the order of the VAR |
lambdas |
a vector containing the lambdas to be used in the fit |
opt |
a list containing the options |
An ncvreg object containing the fitted model.
Estimate VAR using SCAD penalty
var_scad(data, p, lambdas, opt, penalty)var_scad(data, p, lambdas, opt, penalty)
data |
the data |
p |
the order of the VAR |
lambdas |
a vector containing the lambdas to be used in the fit |
opt |
a list containing the options |
penalty |
a string "SCAD" or something else |
An ncvreg object containing the fitted model.