缺少完整列的VaR计算 [英] VaR calculation with complete missing column
问题描述
我需要计算股票收益的滚动VaR.从这篇文章中:使用rollapply函数使用R进行VaR计算,我知道缺少完整情况的列将给出错误.但是由于各个公司的股票收益的开始日期和结束日期不同,因此当数据从长格式转换为宽格式时,它会产生缺失值.可以仅使用没有缺失值的行来进行估计,但这会导致严重的数据丢失.因此,有什么方法可以对具有完全缺失值的列进行计算,对于缺失列,则获得输出"NA".这就是我所做的:
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")
它执行所需的计算,但是当我尝试对前60行的"HAM6"列完全缺失
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")
我收到以下错误:
Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) :
'dimnames' applied to non-array
我知道错误是由于缺少"HAM6"列而引起的,但是有什么办法可以保留丢失的列并为"HAM6"获得输出"NA",而不是删除"HAM6"列?我已尽力尝试用于处理缺失值的方法,但是找不到任何合适的解决方案.非常感谢您的帮助.
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.
推荐答案
使用apply(managers,2,...)
来检查整个列是否为NA
,如下所示:
Use apply(managers,2,...)
with checking if the whole column is NA
as follows:
apply(managers2,2,function(x){
if(!all(is.na(x))){
return(as.numeric(VaR(x, p=.95, method="modified")))
} else {
return(NA)
}
})
结果:
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
警告引用US 3m TR
.这就是存在NA
The warning referrs to US 3m TR
. This is the reason that there is an NA
这篇关于缺少完整列的VaR计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!