0

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 and var.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  
Katia
  • 1
  • 2
  • Welcome to StackOverflow. Please take a look at tips on how to create a [minimum example](http://stackoverflow.com/help/mcve) and this post on [producing a good example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) in R. Also, would you mind including in your question any packages that you are using? `Bcoef`, for example, is not part of base R. – lmo May 27 '16 at 18:37
  • Thanks for the comment! added the package name.. – Katia May 27 '16 at 18:42
  • As mentioned in the links, it is also a good idea to include a sample dataset (probably smaller than your original) that reproduces you problem for people to work with. Take a look at the first answer for the R example link to see how to use `dput`. – lmo May 27 '16 at 18:46

1 Answers1

1

So, try something in these lines (approach is borrowed from pieces of code of the package frequencyConnectedness).

library(vars)

data(Canada)
data <- data.frame(Canada)
window <- 10

# your VAR function, saving both matrices in a list
caller <- function(j) {
  var.2c <- VAR(data[(1:window)+j,],p=1,type = "const")
  B <- Bcoef(var.2c)
  r <- resid(var.2c)
  list(B,r)
}

# Roll the fn over moving windows
out <- pbapply::pblapply(0:(nrow(Canada)-window), caller)

The beauty here is that with large and more time consuming functions (such as SVARs) you can go parallel.

Parallel computations using linux/mac

For example, on linux/mac systems this should make your computer's life easier (different story for windows, see link above, and solution below):

library(vars)
library(pbapply)

data(Canada)
data <- data.frame(Canada)
window <- 10

caller <- function(j) {
  var.2c <- VAR(data[(1:window)+j,],p=1,type = "const")
  B <- Bcoef(var.2c)
  r <- resid(var.2c)
  list(B,r)
}

# Calculate the number of cores and define cluster
no_cores <- detectCores() - 1
cluster <- makeCluster(no_cores, type ="FORK")

out <- pbapply::pblapply(0:(nrow(Canada)-window), caller, cl = cluster)

stopCluster(cluster)

Parallel computations using windows

# Calculate the number of cores and create PSOCK cluster
no_cores <- detectCores() - 1
cluster <- makeCluster(no_cores)

# Export necessary data and functions to the global environment of the cluster workers 
# and necessary packages on the cluster workers
clusterExport(cluster, c("Canada","data","window","caller"), envir=environment())
clusterEvalQ(cluster, library(vars))

#Moving window estimation 
out <- pblapply(0:(nrow(Canada)-window), caller,cl = cluster)

stopCluster(cluster)
Luks
  • 133
  • 11