R:如何一次对多个返回数据进行滚动回归?在一个数据帧中使用因变量,在另一个数据帧中使用回归变量? [英] 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?

查看:157
本文介绍了R:如何一次对多个返回数据进行滚动回归?在一个数据帧中使用因变量,在另一个数据帧中使用回归变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想问一下有没有一种方法可以一次滚动滚动回归以实现多次返回,而因变量在一个数据帧中,而回归变量在另一个数据帧中.我试图结合rollapply和sapply功能来做到这一点.到目前为止,我似乎无法使其正常工作.

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.

对于金融背景:我要做的是为Fama-Macbeth回归计算回归值.具有一个滚动窗口,该滚动窗口将向前滚动1个月以更新回归器.与最初的1973年Fama-Macbeth将估算期延长了4年不同.

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.

我在下面的示例脚本中附加了指向.csv文件的链接,该链接包含Yahoo Finance的每日价格数据,以便您可以更好地了解我的操作.

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.

这是脚本的某些csv文件 ,只需将其放在R work目录中并运行此脚本即可.

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.

谢谢大家.

推荐答案

如果需要所有系数,则可以在rollapply中修改函数(结果必须是向量):

If you need all coefficients you can modify the function in your rollapply ( 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)

然后您可以从coeffs中提取信息:

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"

这篇关于R:如何一次对多个返回数据进行滚动回归?在一个数据帧中使用因变量,在另一个数据帧中使用回归变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆