我正在尝试使用PerformanceAnalytics包为每个安全性构建滚动风险贡献。一旦我能够克服这个错误,我想绘制一段时间的结果。我一直收到下标超出范围的错误,我不知道从哪里开始解决它。具体是这个错误:
Error in `[.xts`(x, i, which.i = TRUE) : subscript out of bounds
对R不是很有经验感谢您的任何指导!这是代码。
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"))