Question

When I try to run the example from page 22 of the PerformanceAnalytics reference, I get an error message. See below.

PS I am a beginner & this has never worked for me. Also, my underlying issue is that I'm getting exactly the same error when trying to use table.CAPM with my own data.

Thanks for any assistance.

> search()
[1] ".GlobalEnv"                   "package:PerformanceAnalytics"
[3] "package:xts"                  "package:zoo"                 
[5] "package:stats"                "package:graphics"            
[7] "package:grDevices"            "package:utils"               
[9] "package:datasets"             "package:methods"             
[11] "Autoloads"                    "package:base"                
> version
           _                            
platform       x86_64-w64-mingw32           
arch           x86_64                       
os             mingw32                      
system         x86_64, mingw32              
status                                      
major          2                            
minor          15.2                         
year           2012                         
month          10                           
day            26                           
svn rev        61015                        
language       R                            
version.string R version 2.15.2 (2012-10-26)
nickname       Trick or Treat               
> data(managers)
> CAPM.alpha(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf=.035/12)
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases
> 
Was it helpful?

Solution

The bug is not in your code, it is in the R package itself. It it is shown on the package validation check here and it can be reproduced with:

library(PerformanceAnalytics)
example(CAPM.alpha)

The error seems to be on line 40 of Return.excess.R. It should be replaced with:

xR = coredata(as.xts(R))-coredata(as.xts(Rf))

The easiest way of fixing this in practice is to run:

require(utils)
assignInNamespace(
  "Return.excess",
  function (R, Rf = 0)
  { # @author Peter Carl
    # edited by orizon
      # .. additional comments removed
      R = checkData(R)
      if(!is.null(dim(Rf))){
          Rf = checkData(Rf)
          indexseries=index(cbind(R,Rf))
          columnname.Rf=colnames(Rf)
      }
      else {
          indexseries=index(R)
          columnname.Rf=Rf
          Rf=xts(rep(Rf, length(indexseries)),order.by=indexseries)
      }
      return.excess <- function (R,Rf)
      { 
          xR = coredata(as.xts(R))-coredata(as.xts(Rf)) #fixed
      }
      result = apply(R, MARGIN=2, FUN=return.excess, Rf=Rf)
      colnames(result) = paste(colnames(R), ">", columnname.Rf)
      result = reclass(result, R)
      return(result)
  },
  "PerformanceAnalytics"
)

Then your original command works:

> data(managers)
> CAPM.alpha(managers[,1,drop=FALSE], managers[,8,drop=FALSE], Rf=.035/12)
[1] 0.005960609

Be aware that I have not verified that the function does what it purports to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top