Error in `dimnames<-.xts`(`*tmp*`, value = dn) : length of 'dimnames' [2] not equal to array extent

When trying to compute the rolling modified sharpe ratio, I receive the following error message : Error in dimnames<-.xts(*tmp*, value = dn) :
length of 'dimnames' [2] not equal to array extent.

Here is my code up to the error:

1. Estimate the performance risk adjusted statistical indicators.

rm
library(quantmod)
library(PerformanceAnalytics)
getSymbols(c("AAPL","MA","^GSPC","^IRX"),periodicity="monthly", src="yahoo",from="2018-01-01",to="2021-12-31")
aapl=AAPL[,4]
ma=MA[,4]
sp500=GSPC[,4]
irx=IRX[,4]
raapl=diff(log(aapl))
rma=diff(log(ma))
rsp500=diff(log(sp500))
rirx=diff(log(irx))
raapl <- na.omit(raapl)
rma <- na.omit(rma)
rsp500 <- na.omit(rsp500)
rirx <- na.omit(rirx)
port=cbind(raapl,rma,rsp500,rirx)

library(zoo)
library(PerformanceAnalytics)

Modified Rolling Sharpe Ratio for APPLE:

Sharpe1<-rollapply(raapl,6,FUN=function(raapl)SharpeRatio.modified(R=raapl,Rf=Af,p=.95,FUN=c("StdDev","VaR","ES"),
annualized=TRUE, weights=NULL))
Sharpe1
plot(Sharpe1,main="Rolling Sharpe Ratio estimation APPLE",ylab="Sharpe Ratio at 95%",legend.loc="topleft")

Modified Rolling Sharpe Ratio for MASTERCARD:

Sharpe2<-rollapply(rma,6,FUN=function(rma)SharpeRatio.modified(R=rma,Rf=Af,p=.95,FUN=c("StdDev","VaR","ES"),
annualized=TRUE, weights=NULL))
Sharpe2
plot(Sharpe2,main="Rolling Sharpe Ratio estimation MASTERCARD",ylab="Sharpe Ratio at 95%",legend.loc="topleft")

Sharpemodified<-rollapply(rma,6,FUN=function(rma) SharpeRatio(rma,Rf=rirx,p=0.95,weights=NULL,annualize=FALSE,method="modified"))

Hi @Maurice_Scholtes,
Welcome to the RStudio Community Forum.

The help file for the function SharpeRatio.modified() says that the FUN argument can only have one string to denote the denominator. So, this should work (although with warnings as the function is deprecated):

# This variable was missing:
Af <- 0 #???

# Modified Rolling Sharpe Ratio for APPLE:
Sharpe1 <- rollapply(raapl, 6, FUN=function(raapl)
                     SharpeRatio.modified(R=raapl, Rf=Af, p=.95, FUN="StdDev",
                                          annualized=TRUE, weights=NULL))

Hope this helps.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.