[R] Multiple regressions with changing dependent variable and time span

arun smartpink111 at yahoo.com
Sat Nov 30 07:42:33 CET 2013

The link seems to be not working.  From the description, it looks like:
dat1 <- as.data.frame(matrix(sample(200,154*337,replace=TRUE),ncol=337))
 colnames(dat1) <- c(paste("F",1:3,sep="."),paste("r",1:334,sep="."))
lst1 <- lapply(paste("r",1:334,sep="."),function(x) cbind(dat1[,c(1:3)],dat1[x]))

 lst2 <- lapply(lst1,function(x) {colnames(x)[4] <-"r";x} )

res <- do.call(rbind,lapply(lst2,function(x) rollapply(x,width=32,FUN=function(z) coef(lm(r~ F.1+F.2+F.3,data=as.data.frame(z))),by.column=FALSE,align="right")))

row.names(res) <- rep(paste("r",1:334,sep="."),each=123)
#[1] 41082     4

coef(lm(r.1~F.1+F.2+F.3,data=dat1[1:32,]) )
#(Intercept)         F.1         F.2         F.3 
#109.9168150  -0.1705361  -0.1028231   0.2027911 
coef(lm(r.1~F.1+F.2+F.3,data=dat1[2:33,]) )
#(Intercept)         F.1         F.2         F.3 
#119.3718949  -0.1660709  -0.2059830   0.1338608 
#    (Intercept)        F.1        F.2       F.3
#r.1    109.9168 -0.1705361 -0.1028231 0.2027911
#r.1    119.3719 -0.1660709 -0.2059830 0.1338608


On Friday, November 29, 2013 6:43 PM, nooldor <nooldor at gmail.com> wrote:
Hi all!

I am just starting my adventure with R, so excuse me naive questions.

My data look like that:


I have 3 independent variables (F.1, F.2 and F.3) and 334 other variables
(r.1, r.2, ... r.334) - each one of these will be dependent variable in my

Total span of the time is 154 observations. But I would like to have rolling
window regression with length of 31 observations.

I would like to run script like that:

summary(lm(r.1~F.1+F.2+F.3, data=data))
vif(lm(r.1~F.1+F.2+F.3, data=data))

But for each of 334 (r.1 to r.334) dependent variables separately and with
rolling-window of the length 31obs.

Id est:
summary(lm(r.1~F.1+F.2+F.3, data=data)) would be run 123 (154 total obs -
31. for the first regression) times for rolling-fixed period of 31 obs.

The next regression would be:
summary(lm(r.2~F.1+F.2+F.3, data=data)) also 123 times ... and so on till
summary(lm(r.334~F.1+F.2+F.3, data=data))

It means it would be 123 x 334 regressions (=41082 regressions)

I would like to save results (summary + vif test) of all those 41082
regressions in one read-user-friendly file like this given by e.g command

Could you help with it?



    [[alternative HTML version deleted]]

R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list