XTS dates from different sources. Using R to calculate beta

喜欢而已 提交于 2019-12-06 02:25:44

问题


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

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.

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.

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")

回答1:


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


来源:https://stackoverflow.com/questions/11802086/xts-dates-from-different-sources-using-r-to-calculate-beta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!