0

Please, put in R the following structure:

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")))

Now input the following code:

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

On my system this returns a matrix whose the last column, named Aaa, contains NaNs; these NaNs are a big problem in my further code developments.

Why do those NaNs happen and how may I solve this issue?

Lisa Ann
  • 3,345
  • 6
  • 31
  • 42

1 Answers1

1

NANs and Inf occurs as a result of computation of your data including dividing by zero or taking logarithms of a negative number.

You can check for the presence of NaNs in your object as:

   is.nan(object)

You can check for the presence of Inf in your object as:

    is.infinite(object)

If you would like to remove NaNs and Infs, you can try this:

    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

Then you can keep NAs during your analysis or remove it as you prefer to do it.

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

To check for the presence of NAs, you can try:

    is.na(M)

will provide a logical data structure with "TRUE" for the presence of NAs and False for the absence of NAs. You can use it as a subscript to get rid of all NAs in your data structure.

Note: Statistical models in R, to my knowledge, provides you an option for removing NAs during model building.

              na.rm = TRUE / FALSE

You can use this option whenever a statistical method in R allows you to do so. Even graphical packages like ggplot2 provides option for removing NAs while plotting the graph, which essentially indicates that you can keep NAs in your data and can remove them at an appropriate time of your analysis.

HTH!

Sathish
  • 12,453
  • 3
  • 41
  • 59
  • Thank you for your help, Sathish. Just a question: do you get the same result with `NaN` I get from that code? If so, could it be an issue of `qnorm()`? Analyzing the code of `cm.quantile()` it looks like `NaN`s are produced wheh R applies `qnorm()` to 1: it should produce all `Inf`, but sometimes it returns `NaN`. – Lisa Ann Oct 15 '12 at 08:37
  • `Inf` should be correct, because of `qnorm(1)`; I do not understand why sometimes `qnorm(1) = Inf` - which is correct - and sometimes `qnorm(1) = NaN`: it's always the same function applied to the same argument... – Lisa Ann Oct 15 '12 at 09:11
  • @LisaAnn Eventually sometimes the value shown as 1 is a little bit greater than 1 (1.00000000000000000001). http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal `qnorm(1.001)` results `NaN`. `qnorm(1)` gives `Inf` – jogo Jun 06 '16 at 09:58