Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XTS dates from different sources. Using R to calculate beta

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")
like image 775
Daniel Morgan Avatar asked Nov 13 '22 01:11

Daniel Morgan


1 Answers

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
like image 133
Joshua Ulrich Avatar answered Dec 15 '22 03:12

Joshua Ulrich