0

Hey guys I have some data, and would like to automatically run some Regressions based on this code:

library(dplyr)
library(plyr)

set.seed(123)
v1<-c(rnorm(340,0,1))
v2<-c(rnorm(340,1,1))
v3<-rnorm(340,2,1)
v4<-rnorm(340,4,1)
v5<-rnorm(340,8,1)
v6<-rnorm(340,0,3)
v7<-rnorm(340,2,6)
v8<-rnorm(340,3,2)
v9<-rnorm(340,9,1)
v10<-rnorm(340,0,2)
date<-seq(as.Date("1990-01-01"), as.Date("2018-04-01"),by="months")
data<-data.frame(date,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10)
rownames(data)<-data$date
data<- data[-1]
rm(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10)

all.combs<- function(dataframe,y){
  df<-dataframe
  df[[substitute(y)]]<- NULL

  m1<- data.frame(t(combn(colnames(df),1)))
  colnames(m1)<-"x1"
  m2<- data.frame(t(combn(colnames(df),2)))
  colnames(m2)<-c("x1","x2")
  m3<- data.frame(t(combn(colnames(df),3)))
  colnames(m3)<-c("x1","x2","x3")
  m4<- data.frame(t(combn(colnames(df),4)))
  colnames(m4)<-c("x1","x2","x3","x4")
  liste<- rbind.fill(m1,m2,m3,m4)
  return(liste)
}

test<-all.combs(data,v3)

where test is a dataframe where every row is a list of independent variables, and v3 is my dependent Variable. Now I basically want to run Regressions with all the rows of test. i.e. something like that

for (i in 1:nrow(test){
reg<-lm(data$v3~data$test[i] 
regs$"i"<-predict(reg)
}

So I have a dataframe with all predicted values of all Regressions of all combinations I generated with my function on v3.

Thanks in advance, Peter

Peter
  • 1
  • 2
  • 1
    What exactly are you doing and why? – user2974951 Oct 22 '18 at 12:06
  • Maybe this will help: https://stackoverflow.com/questions/21284453/saving-and-accessing-results-from-regression-in-a-loop or this https://stackoverflow.com/questions/34721842/loop-linear-regression-and-saving-coefficients – MrFlick Oct 22 '18 at 14:35
  • @user2974951 In the end I want to run vector autoregressions to check what variables are useful to forecast inflation and how they affect inflation, based on the following Paper: https://www.snb.ch/de/mmr/studies/id/economic_studies_2006_02. I already have a code written in Gretl, where I have everything set up, except that I have to write down all the models from hand. – Peter Oct 22 '18 at 16:56

1 Answers1

0

I found the solution to my question, by adding these lines and refering to row 149 in the example:

test<-all.combs(data,CPI)

to.match<-c(as.character(test[149,]))

tt<-select(data,grep(paste(to.match,collapse = "|"),names(data)))
Peter
  • 1
  • 2