XTS来自不同的来源.使用R计算Beta [英] XTS dates from different sources. Using R to calculate beta

查看:98
本文介绍了XTS来自不同的来源.使用R计算Beta的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R有点陌生.我想我的错误对有经验的人来说是微不足道的.

I'm somewhat new to R. I imagine my error will be trivial to the experienced.

我正在尝试编写一个R程序,该程序将为许多股票计算beta.从Input.csv读取股票代码,并从yahoo下载数据.然后,代码循环执行每只股票的beta计算,并输出总结回归的csv.

I'm attempting to write an R program that will calculate beta for a number of stocks. The stock symbols are read from Input.csv, and the data is downloaded from yahoo. The code then loops through a beta calculation for each stock and outputs a csv summarizing the regressions.

在所有期间均采用单一无风险利率的情况下,我可以使用该代码,但是我认为在将超额收益归一化时,可能需要使用每个月的实际无风险利率.我在执行此步骤时遇到了麻烦.从FRED(GS20)成功下载了xts,但是从证券和市场收益中减去收益后,xts的长度为零.这会杀死程序.

I got the code to work when a single risk free rate was assumed in all periods, but I believe I may need to use the actual risk free rate in each month when normalizing excess returns. I am having trouble with this step. The xts is successfully downloaded from FRED (GS20), but when the return is subtracted from the security and market return, it produces an xts of zero length. This kills the program.

我认为这可能是因为FRED和Yahoo之间的日期格式不同.我注意到getSymbols命令忽略了起始日期.任何帮助将不胜感激.

I believe this may be because the dates are in different formats between FRED and Yahoo. I noticed that the getSymbols command ignored the from-to dates. Any help would be appreciated.

require(PerformanceAnalytics)
require(quantmod)
require(car)

setwd("R Projects/Beta Test")

proxy <- read.csv("Input.csv",header=FALSE)[,1]
summary <- as.data.frame(matrix(0, ncol = 5, nrow = 0))

mar <- getSymbols("^GSPC", src = "yahoo", from = as.Date("2006-01-01"),
                  to = as.Date("2011-12-31"),auto.assign=FALSE)
riskFree <- getSymbols("GS20", src = "FRED", from = as.Date("2006-12-01"),
                       to = as.Date("2011-12-31"),auto.assign=FALSE)

for (n in proxy){

    sec <- getSymbols(n, src = "yahoo", from = as.Date("2006-01-01"),
                      to = as.Date("2011-12-31"),auto.assign=FALSE)

    #Monthly Returns
    #ERROR PRODUCED HERE
    sec.xsmonthly <- monthlyReturn(to.monthly(sec),type="log") - riskFree
    mar.xsmonthly <- monthlyReturn(to.monthly(mar),type="log") - riskFree

    sec.reg <- lm(sec.xsweekly ~ mar.xsmonthly + lag(mar.xsmonthly,-1))

    summary[n,1] <- coef(sec.reg)[1]
    summary[n,2] <- coef(sec.reg)[2]
    summary[n,3] <- coef(sec.reg)[3]
    summary[n,5]<-summary(sec.reg)$r.squared
}

summary[,4] <- summary[,2]+summary[,3]

colnames(summary) <- c("Alpha","Beta","Beta-Lag","Sum Beta","R-Squared")
write.csv(summary,file="output.csv")

推荐答案

请注意,getSymbols.FRED没有参数fromto,因为无法用日期范围查询FRED.问题在于FRED日期在每个月初对齐,而to.monthly的输出在每个月末对齐.解决此问题的一种方法是在循环前在riskFree上调用to.monthly:

Note that getSymbols.FRED does not have from and to arguments because there's no way to query FRED with a date range. The problem is that the FRED dates are aligned at the start of each month and the output of to.monthly is aligned at the end of each month. One way around this is to call to.monthly on riskFree before your loop:

mar <- getSymbols("^GSPC", from="2006-01-01", to="2011-12-31", auto.assign=FALSE)
riskFree <- getSymbols("GS20", src="FRED", auto.assign=FALSE)

riskFree <- Cl(to.monthly(riskFree))
mar.xsmonthly <- monthlyReturn(to.monthly(mar),type="log") - riskFree

这篇关于XTS来自不同的来源.使用R计算Beta的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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