Search code examples
rperformanceanalytics

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

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.

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

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"

Both are xts-objects, of course with different dimensions

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

Solution

  • There's a bug in Return.portfolio when you pass it an xts object with a POSIXct index and specify the rebalance_on argument. You can work-around it by converting the index of return_wd_xts to Date.

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