I am trying to construct rolling risk contributions by each security using the PerformanceAnalytics package. Once I can get past this error I would like to plot the results over time. I keep receiving the error that the subscript is out of bounds and i'm not sure where to start in terms of resolving it. This is the error specifically:
Error in `[.xts`(x, i, which.i = TRUE) : subscript out of bounds
Not very experienced with R. Thank you for any guidance! Here is the code.
library(PerformanceAnalytics)
symbols <- c("SPY","EEM","BND","BIL")
prices <-
getSymbols(symbols, src = 'yahoo', from = "2016-01-01",
auto.assign = TRUE, warnings = FALSE) %>%
map(~Cl(get(.))) %>%
reduce(merge) %>%
`colnames<-`(symbols)
portfolioComponentReturns <- na.omit(Return.calculate(prices, method = "log"))
w = c(.25,.25,.25,.25)
rollingvolpct <- na.omit(rollapply(portfolioComponentReturns,
width=252,
FUN = function(Z)
{
t = StdDev(portfolioComponentReturns,weights = w, portfolio_method = "components", data = as.data.frame(Z), na.rm=T);
return(t$pct_contrib)
},
by.column=FALSE, align="right"))