5

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")
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
Daniel Morgan
  • 179
  • 1
  • 5

1 Answers1

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
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
  • Thank you for pointing this out. I'd like to mention that Fred's GS20 releases data monthly, so the Cl didn't end up being necessary. There were loads of other problems with my code that I eventually muddled through after hours of work. Thanks to everyone who read, even if they didn't have something to add. – Daniel Morgan Aug 12 '12 at 22:32