I need to calculate rolling VaR of stock returns. From this post: Using rollapply function for VaR calculation using R , I understand that columns having complete missing cases will give error. But since the starting date and end date of stock returns for various firms are different, it creates missing values when data is converted from long to wide format. Estimation can be done using only rows with no missing values but this leads to serious loss of data. Thus, is there any way to perform the calculation with columns having complete missing values and for the missing columns, getting an output 'NA'. This is what I did:
library(PerformanceAnalytics)
data(managers)
VaR(managers, p=.95, method="modified")
It performs the desired calculation, but when I tried this with first 60 rows with 'HAM6' column completely missing
managers2<-managers[1:60,]
VaR(managers2, p=.95, method="modified")
I get the following error:
Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) :
'dimnames' applied to non-array
I understand that the error is due the missing 'HAM6' column, but is there any way to retain the missing columns and get an output 'NA' for 'HAM6' rather than deleting 'HAM6' column? I have tried most to the methods available for handling missing values, but couldn't find any suitable solution. Any help is much appreciated.
In addition to @Floo0's solution, as a workaround to this problem the missing values could be imputed by the mean return of the corresponding period. See (http://www.r-bloggers.com/missing-data-imputation/) for more information
Use
apply(managers,2,...)
with checking if the whole column isNA
as follows:Result:
The warning referrs to
US 3m TR
. This is the reason that there is anNA