PerformanceAnalytics中CAPM.alpha出错

时间:2012-12-03 05:32:32

标签: r performanceanalytics

当我尝试运行PerformanceAnalytics参考的第22页的示例时,收到错误消息。见下文。

PS我是初学者&这从来没有对我有用。另外,我的根本问题是,当我尝试将table.CAPM与我自己的数据一起使用时,我收到了完全相同的错误。

感谢您的帮助。

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

1 个答案:

答案 0 :(得分:1)

错误不在你的代码中,它在R包本身。它显示在包验证检查here上,可以通过以下方式复制:

library(PerformanceAnalytics)
example(CAPM.alpha)

错误似乎在Return.excess.R的第40行。它应该替换为:

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

在实践中解决此问题的最简单方法是运行:

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

然后您的原始命令有效:

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

请注意,我尚未确认该功能是否符合其声称的功能。