问题
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.
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.
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.
here are some csv files for the script , just put it in in your R work directory and run this script.
library(xts)
library(quantmod)
library(lmtest)
library(sandwich)
library(MASS)
library(tseries)
data.AMZN<-read.csv("AMZN.csv",header=TRUE)
date<-as.Date(data.AMZN$Date,format="%Y-%m-%d")
data.AMZN<-cbind(date, data.AMZN[,-1])
data.AMZN<-data.AMZN[order(data.AMZN$date),]
data.AMZN<-xts(data.AMZN[,2:7],order.by=data.AMZN[,1])
names(data.AMZN)<-
paste(c("AMZN.Open","AMZN.High","AMZN.Low",
"AMZN.Close","AMZN.Volume","AMZN.Adjusted"))
data.AMZN[c(1:3,nrow(data.AMZN)),]
data.YHOO<-read.csv("YHOO.csv",header=TRUE)
date<-as.Date(data.YHOO$Date,format="%Y-%m-%d")
data.YHOO<-cbind(date, data.YHOO[,-1])
data.YHOO<-data.YHOO[order(data.YHOO$date),]
data.YHOO<-xts(data.YHOO[,2:7],order.by=data.YHOO[,1])
names(data.YHOO)<-
paste(c("YHOO.Open","YHOO.High","YHOO.Low",
"YHOO.Close","YHOO.Volume","YHOO.Adjusted"))
data.YHOO[c(1:3,nrow(data.YHOO)),]
data.mkt<-read.csv("GSPC.csv",header=TRUE)
date<-as.Date(data.mkt$Date,format="%Y-%m-%d")
data.mkt<-cbind(date, data.mkt[,-1])
data.mkt<-data.mkt[order(data.mkt$date),]
data.mkt<-xts(data.mkt[,2:7],order.by=data.mkt[,1])
names(data.mkt)[1:6]<-
paste(c("GSPC.Open","GSPC.High","GSPC.Low",
"GSPC.Close","GSPC.Volume","GSPC.Adjusted"))
data.mkt[c(1:3,nrow(data.mkt))]
rets<-diff(log(data.AMZN$AMZN.Adjusted))
rets$YHOO<-diff(log(data.YHOO$YHOO.Adjusted))
names(rets)[1]<-"AMZN"
mktrets<-diff(log(data.mkt$GSPC.Adjusted))
names(mktrets)[1]<- "GSPC"
rets<-rets[-1,]
rets.df = as.data.frame(rets)
mktrets<-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<-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
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.
Thanks guys.
回答1:
If you need all coefficients you can modify the function in your rollapply
(Edit: the result needs to be a vector):
coeffs<-rollapply(1:nrow(rets),
width=252,
FUN=function(i) #i=1:252
{
yrets=rets.df[i,]
xmktrets=mktrets.df[i,]
Coefficients =do.call("cbind",lapply(1:ncol(yrets),function(y) { #y=2
t(summary(lm(yrets[,y]~xmktrets))$coefficients)
} ))
rs=c()
for(j in 1:4)rs<-c(rs,Coefficients[j,])
c(rs,Data=as.Date(index(rets)[max(i)],"%y-%m-%d"))
},
by.column=FALSE)
Then you could extract information from coeffs:
#the betas
colnames(rets)
plot.zoo(coeffs[,c(1,2)],col=2:3,main=colnames(rets)[1]) #"AMZN"
plot.zoo(coeffs[,c(3,4)],col=3:4,main=colnames(rets)[2]) #"YHOO"
来源:https://stackoverflow.com/questions/39438484/r-how-to-do-rolling-regressions-for-multiple-return-data-at-once-with-the-depe