简体   繁体   English

使用PerformanceAnalytics包下标超出范围

[英]Subscript out of bound using the PerformanceAnalytics package

as part of my portfolio analysis work with R, I found the PerformanceAnalytics package which allows for Portfolio Optimization with Rebalancing Periods. 作为我与R进行投资组合分析的一部分,我发现了PerformanceAnalytics软件包,该软件包可实现带有再平衡期的投资组合优化。 For an equally weighted portfolio I want to compute the rebalanced portfolio weights and later the portfolio turnover. 对于同等加权的投资组合,我想计算重新平衡的投资组合权重,然后再计算投资组合周转率。

Before I used my own return data, I worked with the edhec dataset that is given by the package "PerformanceAnalytics". 在使用自己的返回数据之前,我使用了由“ PerformanceAnalytics”软件包提供的edhec数据集。

library(PerformanceAnalytics)
data(edhec)
results <- Return.portfolio(edhec,rebalance_on="months",verbose=T)
bop <- results$BOP.Weight #beginning of period weights
eop <- results$EOP.Weight #end of period weights

For this data everything works fine, but when I use my own dataset return_wd_xts I receive the following error for the following command. 对于此数据,一切正常,但是当我使用自己的数据集return_wd_xts ,以下命令收到以下错误。

Result <- Return.portfolio(return_wd_xts,rebalance_on="months",verbose=T)

Error in [<- ( *tmp* , k, , value = c(1.18880324085028, 1.18880324085028, : subscript out of bounds [<-*tmp* ,k,,value = c(1.18880324085028,1.18880324085028,:下标超出范围

As I am only changing the input parameter for the returns, I further checked the requirements, but did not notice any mistake. 由于仅更改退货的输入参数,因此我进一步检查了要求,但没有发现任何错误。 The package needs for "R" which specifies this parameter : "An xts, vector, matrix, data frame, timeSeries or zoo object of asset returns" 程序包需要指定该参数的“ R”:“资产回报的xts,向量,矩阵,数据帧,timeSeries或zoo对象”

Both are xts-objects, of course with different dimensions 两者都是xts对象,当然具有不同的尺寸

> dim(edhec)
[1] 152  13
> dim(return_wd_xts)
[1] 339   5

Here is a small part of these datasets which look the same way with respect to object 这些数据集中的一小部分在对象方面看起来是一样的

print(edhec[1:2, 1:4])
           Convertible Arbitrage CTA Global Distressed Securities Emerging Markets
1997-01-31                0.0119     0.0393                0.0178           0.0791
1997-02-28                0.0123     0.0298                0.0122           0.0525
print(return_wd_xts[1:2])
                DAX 30  FRANCE CAC FTSE All Share S&P 500 COMP        TOPIX
1989-02-01 -0.02505788  0.05263979    0.126172479   0.06947866  0.036789128
1989-03-01  0.00516151 -0.05138403    0.005407798  -0.02984021 -0.000433607

I hope you understand my problem and may help me to make this command work with my dataset. 希望您理解我的问题,并可以帮助我使此命令与我的数据集一起使用。 Thank you for reading! 感谢您的阅读!

EDIT: Here is a subset of my data 编辑:这是我的数据的子集

dput((return_wd_xts [1:20]))
structure(c(-0.0250578781638776, 0.00516150959728493, 0.027984930778987, 
0.0195568335946277, 0.0302476452426729, 0.0546235532472364, 0.0563960785993615, 
0.0184616380328433, -0.0191649019029796, -0.0665670274978261, 
0.0882841155121556, 0.104673742709786, 0.0371213941123424, -0.0385187293423887, 
0.0771683841234946, -0.0630725360350319, 0.0238145415986074, 
0.0309389114664863, -0.0117695066096512, -0.150003389036393, 
0.052639785059758, -0.0513840293484853, 0.0505421849581241, 0.0155642004003558, 
0.0363734024332616, 0.0134464149885949, 0.0611794007744512, 0.0306364141480501, 
-0.00476426357615802, -0.0307021420106996, 0.0539317596724095, 
0.0415031613822435, -0.053760533794565, -0.0338496575263754, 
0.0610766549458317, 0.0503379829858021, 0.0218084330904627, -0.00795989511152998, 
-0.0304398139576833, -0.175842319085768, 0.126172479401038, 0.00540779787360186, 
0.0314517537542995, 0.01453934878465, 0.000434128493880409, 0.0255336793168354, 
0.0578892368585609, 0.042087921981058, -0.0391320042167483, -0.0648506565720994, 
0.0611233889072859, 0.0475085133961074, -0.0267936184684199, 
-0.0455480674705554, -0.00356607515714309, -0.0449416111143059, 
0.10956430081054, 0.00800782556241665, -0.0108157881757961, -0.0859772845435875, 
0.069478662120746, -0.0298402081915591, 0.0342339735387596, 0.0438334768568805, 
0.0456543343689892, -0.00609072901753578, 0.0758752300946291, 
0.0324820659733045, -0.00584492636420969, -0.0259775996143165, 
0.0311303923158632, 0.0103056824572813, -0.070224865042007, 0.0162103558059008, 
0.0199957087624316, -0.0171621374023858, 0.0938262079394509, 
-0.00793665021865761, -0.00918129795170854, -0.0930133488049235, 
0.0367891282282156, -0.000433607003410761, 0.0121513316929921, 
0.0143989004011288, 0.00599829397678363, -0.0203203973786165, 
0.0630356139495902, -0.00954781314102344, 0.0397939154539181, 
-0.00457220832276896, 0.0466803914188968, 0.0218468298980689, 
-0.0451422736463294, -0.0824118566225031, -0.200446204450984, 
0.0679573810667315, 0.0913633589712441, -0.0324209892423918, 
-0.044392066114818, -0.143646040192435), class = c("xts", "zoo"
), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", 
"POSIXt"), .indexTZ = "", tzone = "", index = structure(c(602290800, 
604710000, 607384800, 609976800, 612655200, 615247200, 617925600, 
620604000, 623199600, 625878000, 628470000, 631148400, 633826800, 
636246000, 638920800, 641512800, 644191200, 646783200, 649461600, 
652140000), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(20L, 
5L), .Dimnames = list(NULL, c("DAX 30", "FRANCE CAC", "FTSE All Share", 
"S&P 500 COMP", "TOPIX")))

There's a bug in Return.portfolio when you pass it an xts object with a POSIXct index and specify the rebalance_on argument. 有一个错误Return.portfolio当你用它传递的XTS对象POSIXct指数并指定rebalance_on说法。 You can work-around it by converting the index of return_wd_xts to Date. 您可以通过将return_wd_xts的索引转换为Date来解决此问题。

index(return_wd_xts) <- as.Date(index(return_wd_xts))
Result <- Return.portfolio(return_wd_xts, rebalance_on = "months", verbose = TRUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM