Search code examples
rperformanceanalytics

Error with CAPM.beta.bear and TimingRatio in PerformanceAnalytics


When I run the code below, my function for CAPM.beta.bull works correctly, but returns an error for both CAPM.beta.bear and TimingRatio

library(PerformanceAnalytics)
library(quantmod)

getSymbols("AAPL", from="2012-01-01", to="2015-01-01")
getSymbols("SPY", from="2012-01-01", to="2015-01-01")


stockbull = function(call){
CAPM.beta.bull(Ad(call), Ad(SPY) , Rf=0)
}

stockbear = function(call){
CAPM.beta.bear(Ad(call), Ad(SPY), Rf=0)
}

stocktiming = function(call){
TimingRatio(Ad(call), Ad(SPY) , Rf=0)
}

I am running R 3.1.3 and here is my resulting error, any help would be much appreciated.

> stockbull(AAPL)
[1] 0.3041228
> stockbear(AAPL)
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases
> stocktiming(AAPL)
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases

Solution

  • Most of the PerformanceAnalytics functions use stock returns, not prices so you need to first compute returns and then pass to these functions. For example, your stockbull function could become

    stockbull = function(call){
      Ra <- Return.calculate(Ad(call))
      Rb <- Return.calculate(Ad(SPY))
      CAPM.beta.bull(Ra, Rb , Rf=0)
    }
    

    with very similar changes for the other functions. With these modifications, the CAPM.beta functions return values close to 1. which are reasonable results.