I did the following for calculating Value at Risk (VaR) over 20 period rolling window:
require(PerformanceAnalytics); require(zoo)
data(edhec)
class(edhec) # [1] "xts" "zoo"
class(edhec$CTAGlobal) # "NULL"
var1<-rollapply(edhec,width=20,FUN=function(edhec) VaR(R=edhec,p=.95,method="modified"),by.column=TRUE)
It produces the desired output, and then I tried the same on another data:
data(managers)
class(managers) # [1] "xts" "zoo"
class(managers$HAM4) # [1] "xts" "zoo"
var2<-rollapply(managers,width=20,FUN=function(managers) VaR(R=managers,p=.95,method="modified"),by.column=TRUE)
But I am getting the following error:
Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) :
'dimnames' applied to non-array
Can someone please tell me why this difference and how to fix this error?
There are columns which are completely missing in first few (3)
width=20
windows, hence the errorWith
traceback
, you can inspect the possible sources of error, notice step 12, ofna.omit(x)
, see?na.omit
For your first window=20 (actually around 60) observations, columns HAM5,HAM6 are missing completely and these result in empty data in the step 12. above
There are no missing values in
edhec
data , hence there were no issuesAn easier way is to retain rows which do not have any missing values and compute statistics over them
OR