Calculation of VaR with a complete missing column

I need to calculate the VaR stock returns shaft. From this post: Using the rollapply function to calculate VaR using R , I understand that columns having complete missing cases will throw an error. But since the start date and end date of the stock return for different firms are different, it creates missing values ​​when the data is converted from long to wide format. Evaluation can be performed using only rows without missing values, but this leads to serious data loss. So, is there a way to do the calculation with columns that have complete missing values, and for missing columns, getting the output "NA". This is what I did:

library(PerformanceAnalytics) data(managers) VaR(managers, p=.95, method="modified") 

It does the required calculation, but when I tried it using the first 60 rows with the “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 was caused by the missing “HAM6” column, but is there a way to save the missing columns and get the “NA” output for “HAM6” instead of deleting the “HAM6” column? I tried most of all the methods available for handling missing values, but did not find a suitable solution. Any help is greatly appreciated.

+2
source share
2 answers

Use apply(managers,2,...) with validation if the entire NA column looks like this:

 apply(managers2,2,function(x){ if(!all(is.na(x))){ return(as.numeric(VaR(x, p=.95, method="modified"))) } else { return(NA) } }) 

Result:

 VaR calculation produces unreliable result (inverse risk) for column: 1 : -0.00354267287759942 HAM1 HAM2 HAM3 HAM4 HAM5 HAM6 EDHEC LS EQ SP500 TR US 10Y TR US 3m TR -0.03212244 -0.03698665 -0.04403660 -0.08093557 -0.12635656 NA -0.02275816 -0.06886077 -0.02510378 NA 

Warning refers to US 3m TR . It is for this reason that NA exists.

+1
source

In addition to @ Floo0's solution, as a workaround to this problem, missing values ​​can be attributed to the average return of the corresponding period. See ( http://www.r-bloggers.com/missing-data-imputation/ ) for more information.

 require(PerformanceAnalytics) data(managers) managers.df=as.data.frame(managers) dateidx = as.Date(index(managers)) #Compute mean Return for each period MeanReturn_PerPeriod=rowMeans(managers.df,na.rm=TRUE) #Create copy of dataset for new values managers.df.new=managers.df #Impute NA Values by average return for rest of the data for(x in 1:ncol(managers.df.new)) { managers.df.new[,x][is.na(managers.df.new[,x])]=MeanReturn_PerPeriod[is.na(managers.df.new[,x])] } managers_imputed=xts(managers.df.new,order.by=dateidx) #Test VaR calculation managers2<-managers_imputed[1:60,] VaR(managers2, p=.95, method="modified") #VaR calculation produces unreliable result (inverse risk) for column: 10 : -0.00354267287759942 # HAM1 HAM2 HAM3 HAM4 HAM5 HAM6 EDHEC LS EQ SP500 TR US 10Y TR #VaR -0.03212244 -0.03491864 -0.0440366 -0.08093557 -0.02880137 -0.02696782 -0.02130781 -0.06886077 -0.02510378 # US 3m TR #VaR NA 
0
source

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


All Articles