来自CreditMetrics的cm.quantile生成NaN

时间:2012-10-15 07:22:42

标签: r

请输入R以下结构:

M <- structure(c(0.861, 0.02115, 0.01031, 0.00218, 0.00062, 0.00075, 
0.00047, 0.00028, 0.00049, 0.00046, 0.00025, 0, 0, 0.00028, 0, 
0, 0, 0, 0, 0, 0, 0, 0.05806, 0.74127, 0.04621, 0.01426, 0.00119, 
0.00036, 0.00054, 0.00041, 0.00069, 0.00011, 2e-05, 0, 0.00018, 
0.00013, 0, 5e-05, 0.00036, 0, 0, 0, 0, 0, 0.02818, 0.08446, 
0.73325, 0.04292, 0.01296, 0.00212, 0.00136, 0.00082, 4e-04, 
0.00035, 0.00027, 0.00026, 0.00011, 0.00015, 7e-05, 0.00037, 
0, 0, 0, 0, 0, 0, 0.00544, 0.06172, 0.09626, 0.74319, 0.05289, 
0.0108, 0.00233, 0.00153, 0.00079, 0.00043, 0.00048, 0, 0.00027, 
8e-05, 8e-05, 0, 0, 0, 0, 0, 0, 0, 0.00326, 0.01558, 0.03482, 
0.08853, 0.74873, 0.05157, 0.01757, 0.00257, 0.00169, 0.00121, 
0.00171, 0.00026, 0.00021, 0.00038, 0.00018, 8e-05, 0, 0, 0, 
0, 0, 0, 0.00124, 0.00656, 0.01336, 0.03007, 0.08159, 0.75361, 
0.06561, 0.01745, 0.0068, 0.00239, 0.00146, 0.00076, 0.00144, 
8e-04, 0, 0.00019, 0.00029, 0.00011, 0, 0, 0, 0, 0.00014, 0.00125, 
0.00436, 0.00979, 0.02872, 0.07991, 0.73237, 0.06614, 0.0248, 
0.00557, 0.00342, 0.00063, 0.00127, 0.00088, 0.00037, 0.00064, 
0, 0, 0, 0, 0, 0, 0.00012, 0.0016, 0.00099, 0.0028, 0.0071, 0.0285, 
0.06673, 0.72947, 0.05849, 0.02627, 0.00567, 0.00293, 0.00147, 
0.00063, 0.00091, 0.00041, 0.00058, 0.00011, 0, 0, 0, 0, 0, 0.00029, 
0.00113, 0.00235, 0.00401, 0.00852, 0.03129, 0.07254, 0.73812, 
0.08704, 0.02916, 0.00736, 0.003, 0.00127, 9e-04, 0.00053, 0, 
0.00056, 0, 0, 0, 0, 0, 6e-05, 2e-05, 0.00079, 0.00171, 0.00407, 
0.01094, 0.02855, 0.06595, 0.70365, 0.09882, 0.03277, 0.00725, 
0.00273, 0.00133, 0.00093, 4e-05, 0.00214, 0, 0, 0, 0, 0.00016, 
6e-05, 3e-04, 0.00022, 0.0022, 0.0019, 0.00471, 0.00774, 0.01578, 
0.04864, 0.63246, 0.08694, 0.0249, 0.00447, 0.00286, 0.00076, 
0.00058, 0.00096, 0.00126, 0.00065, 0, 0, 0.00016, 0, 6e-05, 
0.00028, 0.00141, 0.00121, 0.00177, 0.00433, 0.00612, 0.02536, 
0.04685, 0.62721, 0.06413, 0.02543, 0.00599, 0.00224, 4e-04, 
0.00079, 0.00095, 0.00146, 0, 0, 0, 3e-05, 0, 0.00024, 0.00047, 
0.0012, 0.00191, 0.00286, 0.00555, 0.01114, 0.03758, 0.06879, 
0.63721, 0.06643, 0.01916, 0.00552, 0.00248, 0.00406, 0, 0.00195, 
0, 0, 2e-05, 0, 0.00016, 0.00024, 0.00078, 0.00051, 0.00097, 
0.00354, 0.00468, 0.0081, 0.01457, 0.02812, 0.05973, 0.63797, 
0.0695, 0.02281, 0.00754, 0.00682, 0.00158, 0.00211, 0, 0, 0, 
0, 8e-05, 0, 0.00024, 0.00036, 0.00049, 0.00074, 0.00239, 0.00355, 
0.0105, 0.02154, 0.0443, 0.06606, 0.61553, 0.06689, 0.02387, 
0.01201, 0.00443, 0.00552, 0, 0, 0, 0, 6e-05, 0, 1e-04, 0.00012, 
0.00022, 0.00042, 0.00104, 0.00262, 0.00677, 0.00992, 0.02098, 
0.03936, 0.079, 0.59103, 0.09278, 0.03765, 0.01549, 0.01576, 
0, 0, 0, 0, 0, 3e-05, 0, 0.00033, 3e-05, 0.00053, 0.00121, 0.00182, 
0.00138, 0.00213, 0.00489, 0.01213, 0.03251, 0.06436, 0.5217, 
0.07683, 0.02941, 0.01836, 0, 0, 2e-05, 0, 0.00024, 0, 7e-05, 
0.00032, 4e-05, 0.00029, 0.00017, 9e-04, 0.00185, 0.00202, 0.00339, 
0.00545, 0.01572, 0.03769, 0.08199, 0.46362, 0.07383, 0.03559, 
0, 0, 0, 0, 0, 0, 0, 6e-05, 7e-05, 7e-05, 0.00027, 0.00104, 0.00044, 
8e-04, 0.00068, 0.00253, 0.00483, 0.01156, 0.04023, 0.05112, 
0.3796, 0.04176, 0, 0, 0, 0, 4e-05, 0, 0, 0, 0.00014, 2e-04, 
1e-04, 0.00042, 0.00054, 0.00106, 0.00114, 0.00362, 0.0057, 0.01098, 
0.02518, 0.04904, 0.07889, 0.35197, 0, 0, 0.0422, 0.065969, 0.058349, 
0.06165, 0.05443, 0.05312, 0.05984, 0.05811, 0.06283, 0.06596, 
0.09949, 0.09921, 0.10674, 0.10623, 0.10933, 0.11679, 0.11731, 
0.12062, 0.14814, 0.16705, 1, 0, 0, 1.00000000002876e-06, 9.99999999917733e-07, 
0.000460000000000127, 0.000780000000000114, 0.000659999999999883, 
0.000600000000000045, 0.00141000000000013, 0.00163999999999986, 
0.00296999999999981, 0.00631000000000004, 0.00729000000000002, 
0.01671, 0.0229900000000003, 0.0360299999999999, 0.0661700000000001, 
0.0846699999999999, 0.17356, 0.26642, 0.35782, 0, 1), .Dim = c(22L, 
22L), .Dimnames = list(c("Aaa", "Aa1", "Aa2", "Aa3", "A1", "A2", 
"A3", "Baa1", "Baa2", "Baa3", "Ba1", "Ba2", "Ba3", "B1", "B2", 
"B3", "Caa1", "Caa2", "Caa3", "Ca-C", "WR", "Default"), c("Aaa", 
"Aa1", "Aa2", "Aa3", "A1", "A2", "A3", "Baa1", "Baa2", "Baa3", 
"Ba1", "Ba2", "Ba3", "B1", "B2", "B3", "Caa1", "Caa2", "Caa3", 
"Ca-C", "WR", "Default")))

现在输入以下代码:

library(CreditMetrics) # donwloadable from CRAN repository
cm.quantile(M)

在我的系统上,这将返回一个矩阵,其最后一列名为Aaa,包含NaN s;这些NaN在我的进一步代码开发中是个大问题。

为什么NaN会发生?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

NANs和Inf是由于计算数据而产生的,包括除以零或取负数的对数。

您可以检查对象中是否存在NaN:

   is.nan(object)

您可以在对象中检查Inf的存在:

    is.infinite(object)

如果您想删除NaN和Infs,可以试试这个:

    object = M
    M[is.nan(M)] <- NA   # assign NA, commonly referred as missing values, to NaN values
    M[is.infinite(M)] <- NA # assign NA to Inf values

然后,您可以在分析期间保留NAs,或者根据您的喜好将其删除。

    M <- data.frame(M)
    M <- na.omit(M)

要检查是否存在NA,您可以尝试:

    is.na(M)
对于NAs的存在,

将提供“TRUE”的逻辑数据结构,对于没有NA,将提供False。您可以将其用作下标来删除数据结构中的所有NA。

注意:据我所知,R中的统计模型为您提供了在模型构建过程中删除NAs的选项。

              na.rm = TRUE / FALSE

只要R中的统计方法允许,您就可以使用此选项。甚至像ggplot2这样的图形软件包也提供了在绘制图形时删除NA​​的选项,这实际上表明您可以在数据中保留NAs,并可以在分析的适当时间删除它们。

HTH!