大家好我想问一下,有多少方法可以一次性进行多次返回滚动窗口回归,一个数据框中的因变量和另一个数据框中的回归量?我试图结合rollapply和sapply功能来做到这一点。到目前为止,我似乎无法使其发挥作用。
对于金融背景:我想要做的是计算Fama-Macbeth回归的回归量。使用滚动窗口向前滚动1个月以更新回归量。不同于最初的1973年Fama-macbeth,将估计期推进了4年。
我附上了以下示例脚本所需的.csv文件的链接,它包含来自Yahoo Finance的每日价格数据,以便您们可以更好地了解我想要做的事情。
here are some csv files for the script ,只需将其放入R工作目录并运行此脚本。
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
或许它要求太多,但我真的需要这样做。任何关于如何做到这一点或任何其他方式的建议将非常感激。
谢谢你们。
答案 0 :(得分:0)
如果你需要所有系数,你可以修改rollapply
中的函数(编辑:结果需要是一个向量):
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)
然后你可以从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"