R: how to do rolling regressions for multiple return data at once? with the dependent variable in one data frame and the regressor in the other?
<p>Hi guys i would like to ask is there any way to do rolling window regressions for multiple return at once, with the dependent variable in one data frame and the regressor in the other?. i am trying to combine the rollapply and the sapply funtion to do this. So far i cant seem to make it work.</p>

<p>For finance backgrounds: What i am trying to do is to compute the regressor for Fama-Macbeth regressions. With a rolling window that is rolled forward by 1 month to update the regressor. Different from the original 1973 Fama-macbeth that rolls forward the estimation period by 4 years.</p>

<p>I attached a link to the .csv files needed for the sample script below, it contains daily price data from Yahoo Finance, so that you guys can better see what i am trying to do.</p>

<p><a href="https://drive.google.com/open?id=0BzMN_d7wpPwHOVBIdWxLWVNjUFk" rel="nofollow">here are some csv files for the script</a>
, just put it in in your R work directory and run this script.</p>

<pre><code>library(xts)
library(quantmod)
library(lmtest)
library(sandwich)
library(MASS)
library(tseries)


data.AMZN&lt;-read.csv("AMZN.csv",header=TRUE)
date&lt;-as.Date(data.AMZN$Date,format="%Y-%m-%d")
data.AMZN&lt;-cbind(date, data.AMZN[,-1])
data.AMZN&lt;-data.AMZN[order(data.AMZN$date),]
data.AMZN&lt;-xts(data.AMZN[,2:7],order.by=data.AMZN[,1])
names(data.AMZN)&lt;-
  paste(c("AMZN.Open","AMZN.High","AMZN.Low",
          "AMZN.Close","AMZN.Volume","AMZN.Adjusted"))
data.AMZN[c(1:3,nrow(data.AMZN)),]

data.YHOO&lt;-read.csv("YHOO.csv",header=TRUE)
date&lt;-as.Date(data.YHOO$Date,format="%Y-%m-%d")
data.YHOO&lt;-cbind(date, data.YHOO[,-1])
data.YHOO&lt;-data.YHOO[order(data.YHOO$date),]
data.YHOO&lt;-xts(data.YHOO[,2:7],order.by=data.YHOO[,1])
names(data.YHOO)&lt;-
  paste(c("YHOO.Open","YHOO.High","YHOO.Low",
          "YHOO.Close","YHOO.Volume","YHOO.Adjusted"))
data.YHOO[c(1:3,nrow(data.YHOO)),]

data.mkt&lt;-read.csv("GSPC.csv",header=TRUE)
date&lt;-as.Date(data.mkt$Date,format="%Y-%m-%d")
data.mkt&lt;-cbind(date, data.mkt[,-1])
data.mkt&lt;-data.mkt[order(data.mkt$date),]
data.mkt&lt;-xts(data.mkt[,2:7],order.by=data.mkt[,1])
names(data.mkt)[1:6]&lt;-
  paste(c("GSPC.Open","GSPC.High","GSPC.Low",
          "GSPC.Close","GSPC.Volume","GSPC.Adjusted"))
data.mkt[c(1:3,nrow(data.mkt))]

rets&lt;-diff(log(data.AMZN$AMZN.Adjusted))
rets$YHOO&lt;-diff(log(data.YHOO$YHOO.Adjusted))
names(rets)[1]&lt;-"AMZN"

mktrets&lt;-diff(log(data.mkt$GSPC.Adjusted))
names(mktrets)[1]&lt;- "GSPC"


rets&lt;-rets[-1,]
rets.df = as.data.frame(rets)

mktrets&lt;-mktrets[-1,]
mktrets.df = as.data.frame(mktrets)

# combining this funtion : do 252 days rolling window linear regression, 
#for a single asset as dependent variable and the other as regressor, in the same data frame
coeffs&lt;-rollapply(rets,
                  width=252,
                  FUN=function(X)
                  {
                    roll.reg=lm(AMZN~YHOO,#YHOO is supposed to be GSPC, just an illustration.
                                data=as.data.frame(X))
                    return(summary(roll.reg)$coef)
                  },
                  by.column=FALSE)

#With this funtion : it does linear regressions for multiple assets in a different data frame at once
#and put it in a matrix.

Coefficients = sapply(1:ncol(rets),function(x) {
  summary(lm(rets[,x]~mktrets[,1]))$coefficients
}
)

#I need to the rolling regressions with different data frames because 
#in the real application,i need to assign a unique and specific regressor to 
#each dependent variable
</code></pre>

<p>Perhaps it's too much to ask, but i realy need to do this. Any suggestions on how to do this or any other way to do this will be very appreciated.</p>

<p>Thanks guys.</p>