Skip to content
All library documents

Fixing Return.portfolio Rebalancing with Date-Based xts Indexes

Article Quant Q&A · Author: Rob Roy

Summary

The document describes an R error that occurs when the PerformanceAnalytics Return.portfolio function is asked to rebalance a portfolio on a calendar frequency. The reported call fails with a subscript error, while the function works when rebalancing is omitted. The suggested workaround converts the returns data from a timeSeries object to xts and sets its index class to Date before calling Return.portfolio with quarterly rebalancing enabled.

The post reports that this change allowed the function to run, but offers no explanation of the underlying cause or broader validation. The author also cautions that changing the index class may have other effects. Treat this as a troubleshooting lead for date-indexed portfolio return data, and check that the converted index still represents the intended dates and frequency in your own workflow.

Key ideas

  • Return.portfolio may fail when rebalancing is requested on data stored as a timeSeries object.
  • Converting the returns to xts and setting the index class to Date is reported as a workaround.
  • The example applies quarterly rebalancing after changing the index.
  • The post does not explain the cause or assess possible side effects of changing the index class.

Tags

Full text
# Return.portfolio error from PerformanceAnalytics package


# Return.portfolio error from PerformanceAnalytics package












When using the PerformanceAnalytics package of R, I am getting an error from the Return.portfolio function whenever I ask it to rebalance_on any frequency. If the rebalance parameter is removed, the function works fine.

Return.portfolio(portfolio.returns.daily, weights=risk.weights, rebalance_on="months")

Error in `[<-`(`*tmp*`, k, , value = c(0.734291071622665, 0.629392347105141, : subscript out of bounds

## Answer by user20169 (score 1)

https://quant.stackexchange.com/a/25233

Just ran into and solved this problem.

Convert the timeSeries object into an xts object then change the indexClass to "Date"

```
# returns is a timeSeries object
r <- as.xts(returns)
indexClass(r) <- "Date"
a <- Return.portfolio(r, rebalance_on="quarters", verbose=TRUE)
```

I'm not an R expert or anything so I don't know if changing the indexClass has any other ramifications, but this allows me Return.portfolio to work.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.