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