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