[英]Subscript out of bound using the PerformanceAnalytics package
作為我與R進行投資組合分析的一部分,我發現了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
對於此數據,一切正常,但是當我使用自己的數據集return_wd_xts
,以下命令收到以下錯誤。
Result <- Return.portfolio(return_wd_xts,rebalance_on="months",verbose=T)
[<-
(*tmp*
,k,,value = c(1.18880324085028,1.18880324085028,:下標超出范圍
由於僅更改退貨的輸入參數,因此我進一步檢查了要求,但沒有發現任何錯誤。 程序包需要指定該參數的“ R”:“資產回報的xts,向量,矩陣,數據幀,timeSeries或zoo對象”
兩者都是xts對象,當然具有不同的尺寸
> dim(edhec)
[1] 152 13
> dim(return_wd_xts)
[1] 339 5
這些數據集中的一小部分在對象方面看起來是一樣的
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
希望您理解我的問題,並可以幫助我使此命令與我的數據集一起使用。 感謝您的閱讀!
編輯:這是我的數據的子集
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")))
有一個錯誤Return.portfolio
當你用它傳遞的XTS對象POSIXct
指數並指定rebalance_on
說法。 您可以通過將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.