使用 addPosLimit 和 osMaxPos 抛出 "error in PosLimit[, "MaxPos"] :维数不正确"

Using addPosLimit and osMaxPos throws "error in PosLimit[, "MaxPos"] : incorrect number of dimensions"

即使我从 blog.fosstrading.com/2011/08/tactical-asset-allocation-using.html 复制粘贴示例,我也会收到此错误:

 error in PosLimit[, "MaxPos"] : incorrect number of dimensions

这是一个错误还是我遗漏了什么?

sessionInfo() 的输出:

R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] dplyr_0.4.3                   quantstrat_0.9.1687           foreach_1.4.2                
 [4] blotter_0.9.1695              PerformanceAnalytics_1.4.3662 FinancialInstrument_1.2.0    
 [7] quantmod_0.4-5                TTR_0.23-0                    xts_0.9-7                    
[10] zoo_1.7-12                   

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.1      lattice_0.20-33  codetools_0.2-14 assertthat_0.1   grid_3.2.2      
 [6] R6_2.1.1         DBI_0.3.1        magrittr_1.5     iterators_1.0.7  tools_3.2.2     
[11] parallel_3.2.2  

您已加载 dplyr。它定义了一个 lag 函数来屏蔽通用函数 stats::lagdplyr::lag 不执行任何方法分派,因此在某个地方有一个滞后方法在应该被调用的时候没有被调用。

dplyr 还屏蔽了 xts 中定义的 firstlast 泛型,这也可能导致问题。

短期解决方法是先调用 library(dplyr),这样 xts 中的 firstlast 将在 dplyr 中屏蔽它们的对应部分。长期的解决方案是所有的包都应该显式地导入它们使用的所有函数,以避免包的顺序 loaded/attached 引起的问题(注意用户未打包的代码仍然会受到包 [=25= 的影响) ]顺序)。