Can somebody help me to run in R a VAR(1) (vector autoregression) with a rolling window on a multiple time series AND somehow store the Bcoef
(coefficients) and residuals? It seems like I cannot figure out a way to do it all at once.
My code: (using packages library(vars)
for vector autoregressions
varcoef <- function(x) Bcoef(VAR(x, p=1, type =c("const"), lag.max = NULL))
varr <- function(x) resid(VAR(x, p=1, type =c("const"), lag.max = NULL))
rolling.var.coef <- rollapply(eur.var,width=120,varcoef, by.column=FALSE)
var.resids<-as.data.frame(rollapplyr(eur.var,width=120,varr, by.column=FALSE))
the are two problems with this approach:
- I have 3000 days and the output matrices
rolling.var.coef
andvar.resids
are also of length 3000, while the lengths must be 7x3000 (there are 7 coefficients) and 119*3000 (each regression has 119 residuals), so it calculates the VAR(1) only for the a couple of the first days - AND the most important thing: how to do it in one function, not two. because the output is two matrices
Here is the approximate view of my data - 3000 days like this.
V1 V2 V3 V4 V5 V6 V7
2016-05-10 -0.34 -0.35 -0.37 -0.40 -0.41 -0.30 0.14
2016-05-09 -0.36 -0.35 -0.37 -0.40 -0.41 -0.30 0.15