Using rollapply to calculate VaR using R

I did the following to calculate Value at Risk (VaR) for a 20 second transition 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 thing with other 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 get 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 is and how to fix this error?

+2
source share
1 answer

There are columns that are completely missing in the first (3) width=20 windows, so the error

 data(managers) class(managers) # [1] "xts" "zoo" class(managers$HAM4) # [1] "xts" "zoo" var2<-rollapply(managers,width=20,FUN=function(x) VaR(R=x,p=.95,method="modified"),by.column=TRUE) 

With traceback you can check for possible sources of errors, check step 12, na.omit(x) , see ?na.omit

 traceback() #17: as.matrix.xts(x) #16: as.matrix(x) #15: as.vector(as.matrix(x), mode = mode) #14: as.vector.zoo(x, mode) #13: as.vector(x, mode) #12: as.vector(na.omit(R[, column])) #11: VaR.CornishFisher(R = R, p = p) #10: VaR(R = managers, p = 0.95, method = "modified") at #1 #9: FUN(.subset_xts(data, (i - width + 1):i, j), ...) #8: FUN(newX[, i], ...) #7: apply(ind, 1, function(i) FUN(.subset_xts(data, (i - width + # 1):i, j), ...)) #6: FUN(1:10[[5L]], ...) #5: lapply(X = X, FUN = FUN, ...) #4: sapply(1:NCOL(data), function(j) apply(ind, 1, function(i) FUN(.subset_xts(data, # (i - width + 1):i, j), ...))) #3: xts(sapply(1:NCOL(data), function(j) apply(ind, 1, function(i) FUN(.subset_xts(data, # (i - width + 1):i, j), ...))), tt, if (by == 1) attr(data, # "frequency")) #2: rollapply.xts(managers, width = 20, FUN = function(managers) VaR(R = managers, # p = 0.95, method = "modified"), by.column = TRUE) #1: rollapply(managers, width = 20, FUN = function(managers) VaR(R = managers, # p = 0.95, method = "modified"), by.column = TRUE) # 

For your first window = 20 (actually about 60) observations, the HAM5, HAM6 columns are completely absent, and they lead to empty data in step 12. above

 head(managers,20) #Empty data since NA columns are omitted see step 12 above. na.omit(head(managers,20)) # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] #> 

Missing missing values ​​in edhec data, so there were no problems.

 any(is.na(edhec)) any(is.na(managers)) 

A simpler way is to save rows that have no missing values ​​and calculate statistics on them.

 managers_sub = managers[complete.cases(managers),] var3<-rollapply(managers_sub,width=20,FUN=function(x) VaR(R=x,p=.95,method="modified"),by.column=TRUE) 

OR

 You could find the index where none of the columns are completely missing and subset accordingly sapply(colnames(managers),function(x) all(is.na(managers[60:80,x]))) 
+3
source

Source: https://habr.com/ru/post/974477/


All Articles