Search code examples
rloopsvar

R loop for multiple VARs


I have an R loop:

for(i in 1:10){
  VAR(reg_full,p = i, type = "both")
}

but when I run it I get no output (no error either). The console is simply waiting for my next output.

My goal is to run 10 different VAR models with lags 1 through 10, and then plot their IRFs using the IRF function of vars package. I am stuck on step one (calculate the VARs).

p=i is the lag selection that I would like in the model, ideally i takes the values 1:10. reg_full is my dataframe, type = "both" is just an option (not important).

Also, how do I assign a model name, such as model1 to the model so that I store the results. The results are stored in a list object?

Thank you all for your efforts.


Solution

  • L <- list()
    for(i in 1:10){
      L[[i]] <- VAR(reg_full,p = i, type = "both")
    }
    

    You can access each individual model with L[[i]], for example the first model with L[[1]].