作为我与R的投资组合分析工作的一部分,我找到了PerformanceAnalytics包,它允许具有重新平衡期的投资组合优化。对于同等加权的投资组合,我想计算重新平衡的投资组合权重以及之后的投资组合周转率。
在我使用自己的返回数据之前,我使用了包裹提供的edhec
数据集" PerformanceAnalytics"。
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 ,,值= c(1.18880324085028,1.18880324085028,: 下标超出范围
由于我只是更改了返回的输入参数,我进一步检查了要求,但没有发现任何错误。包裹需要&#34; R&#34;它指定了这个参数:&#34;资产的xts,vector,matrix,dataframe,timeSeries或zoo对象返回&#34;
两者都是xts-objects,当然具有不同的维度
> 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")))
答案 0 :(得分:2)
当Return.portfolio
传递带有POSIXct
索引的xts对象并指定rebalance_on
参数时,会出现错误。{1}}您可以通过将return_wd_xts
的索引转换为日期来解决此问题。
index(return_wd_xts) <- as.Date(index(return_wd_xts))
Result <- Return.portfolio(return_wd_xts, rebalance_on = "months", verbose = TRUE)