0

I calculate with a simulation modell many 42x42 covariance matrices and read it to R with read.table. In R I have to calculate the roots of these covariance matrices. Problem: sometimes in R occurs negative eigenvalues, which should not be (because it´s a covariance matrix)

Explanation I found: very small values are vulnerable for floating point error one of Eigenvalues of covariance matrix is negative in R

But even if I use nearPD to estimate the nearest positive semi definite matrix it´s not working reliably for all my matrices. Sometimes I get "Warning message: In nearPD(nearPD_not_working, keepDiag = TRUE, maxit = 1e+05) : 'nearPD()' did not converge in 100000 iterations" or in my results (the root of the matrix) are just NaNs.

Is there a way to stop R from producing the floating point error?

In future I might have not just 42x42 matrices, but much bigger ones. I need a solution which works for all of my matrices, not just for some after playing with the iteration number of nearPD.

#example data
require(Matrix)
require(matrixcalc)


###3 nearPD without errors, but NaN results
root_not_working <- 
  structure(c(2.2e-05, -0.000307, -0.00069, 8e-06, -0.000223, 4e-05, 
              -0.000111, 2.8e-05, -0.000278, 3.5e-05, -0.00023, -0.000463, 
              -3e-06, 2.5e-05, -0.000218, -1.5e-05, 5.6e-05, -0.000327, 5.1e-05, 
              -0.00023, -0.00012, 5e-06, -3.2e-05, -0.000246, 5e-05, 1.3e-05, 
              2.5e-05, -0.000359, 4e-05, -0.000249, 4.6e-05, -7.2e-05, -0.000226, 
              -0.000198, -0.00027, -0.000346, -0.000241, 3.7e-05, 4.2e-05, 
              -0.000333, -7.4e-05, 2e-06, -0.000307, 0.004229, 0.009503, -0.000111, 
              0.003066, -0.000556, 0.001532, -0.000385, 0.003832, -0.000485, 
              0.003169, 0.00638, 3.7e-05, -0.000348, 0.003005, 0.000206, -0.000775, 
              0.004507, -0.000699, 0.003164, 0.00165, -6.6e-05, 0.00044, 0.003392, 
              -0.000695, -0.000174, -0.000348, 0.004944, -0.000549, 0.003429, 
              -0.000627, 0.000997, 0.003105, 0.002719, 0.003721, 0.004759, 
              0.003317, -0.000515, -0.000581, 0.004589, 0.001022, -2.7e-05, 
              -0.00069, 0.009503, 0.021356, -0.000249, 0.006891, -0.001249, 
              0.003442, -0.000866, 0.008611, -0.001089, 0.007121, 0.014338, 
              8.4e-05, -0.000783, 0.006753, 0.000462, -0.001742, 0.010128, 
              -0.001571, 0.00711, 0.003707, -0.000147, 0.000989, 0.007623, 
              -0.001562, -0.00039, -0.000782, 0.01111, -0.001234, 0.007706, 
              -0.001408, 0.002242, 0.006979, 0.006111, 0.008363, 0.010695, 
              0.007454, -0.001157, -0.001305, 0.010313, 0.002297, -6e-05, 8e-06, 
              -0.000111, -0.000249, 3e-06, -8e-05, 1.5e-05, -4e-05, 1e-05, 
              -1e-04, 1.3e-05, -8.3e-05, -0.000167, -1e-06, 9e-06, -7.9e-05, 
              -5e-06, 2e-05, -0.000118, 1.8e-05, -8.3e-05, -4.3e-05, 2e-06, 
              -1.2e-05, -8.9e-05, 1.8e-05, 5e-06, 9e-06, -0.000129, 1.4e-05, 
              -9e-05, 1.6e-05, -2.6e-05, -8.1e-05, -7.1e-05, -9.7e-05, -0.000125, 
              -8.7e-05, 1.3e-05, 1.5e-05, -0.00012, -2.7e-05, 1e-06, -0.000223, 
              0.003066, 0.006891, -8e-05, 0.002223, -0.000403, 0.001111, -0.000279, 
              0.002778, -0.000352, 0.002298, 0.004626, 2.7e-05, -0.000253, 
              0.002179, 0.000149, -0.000562, 0.003268, -0.000507, 0.002294, 
              0.001196, -4.8e-05, 0.000319, 0.00246, -0.000504, -0.000126, 
              -0.000252, 0.003585, -0.000398, 0.002487, -0.000454, 0.000723, 
              0.002252, 0.001972, 0.002698, 0.003451, 0.002405, -0.000373, 
              -0.000421, 0.003328, 0.000741, -1.9e-05, 4e-05, -0.000556, -0.001249, 
              1.5e-05, -0.000403, 7.3e-05, -0.000201, 5.1e-05, -0.000504, 6.4e-05, 
              -0.000416, -0.000839, -5e-06, 4.6e-05, -0.000395, -2.7e-05, 0.000102, 
              -0.000592, 9.2e-05, -0.000416, -0.000217, 9e-06, -5.8e-05, -0.000446, 
              9.1e-05, 2.3e-05, 4.6e-05, -0.00065, 7.2e-05, -0.000451, 8.2e-05, 
              -0.000131, -0.000408, -0.000357, -0.000489, -0.000625, -0.000436, 
              6.8e-05, 7.6e-05, -0.000603, -0.000134, 4e-06, -0.000111, 0.001532, 
              0.003442, -4e-05, 0.001111, -0.000201, 0.000555, -0.00014, 0.001388, 
              -0.000176, 0.001148, 0.002311, 1.4e-05, -0.000126, 0.001088, 
              7.4e-05, -0.000281, 0.001632, -0.000253, 0.001146, 0.000597, 
              -2.4e-05, 0.000159, 0.001228, -0.000252, -6.3e-05, -0.000126, 
              0.001791, -0.000199, 0.001242, -0.000227, 0.000361, 0.001125, 
              0.000985, 0.001348, 0.001724, 0.001201, -0.000187, -0.00021, 
              0.001662, 0.00037, -1e-05, 2.8e-05, -0.000385, -0.000866, 1e-05, 
              -0.000279, 5.1e-05, -0.00014, 3.5e-05, -0.000349, 4.4e-05, -0.000289, 
              -0.000581, -3e-06, 3.2e-05, -0.000274, -1.9e-05, 7.1e-05, -0.000411, 
              6.4e-05, -0.000288, -0.00015, 6e-06, -4e-05, -0.000309, 6.3e-05, 
              1.6e-05, 3.2e-05, -0.000451, 5e-05, -0.000313, 5.7e-05, -9.1e-05, 
              -0.000283, -0.000248, -0.000339, -0.000434, -0.000302, 4.7e-05, 
              5.3e-05, -0.000418, -9.3e-05, 2e-06, -0.000278, 0.003832, 0.008611, 
              -1e-04, 0.002778, -0.000504, 0.001388, -0.000349, 0.003472, -0.000439, 
              0.002871, 0.005781, 3.4e-05, -0.000316, 0.002723, 0.000186, -0.000703, 
              0.004084, -0.000634, 0.002867, 0.001495, -5.9e-05, 0.000399, 
              0.003074, -0.00063, -0.000157, -0.000315, 0.00448, -0.000498, 
              0.003107, -0.000568, 0.000904, 0.002814, 0.002464, 0.003372, 
              0.004312, 0.003006, -0.000467, -0.000526, 0.004158, 0.000926, 
              -2.4e-05, 3.5e-05, -0.000485, -0.001089, 1.3e-05, -0.000352, 
              6.4e-05, -0.000176, 4.4e-05, -0.000439, 5.6e-05, -0.000363, -0.000731, 
              -4e-06, 4e-05, -0.000345, -2.4e-05, 8.9e-05, -0.000517, 8e-05, 
              -0.000363, -0.000189, 8e-06, -5e-05, -0.000389, 8e-05, 2e-05, 
              4e-05, -0.000567, 6.3e-05, -0.000393, 7.2e-05, -0.000114, -0.000356, 
              -0.000312, -0.000427, -0.000546, -0.00038, 5.9e-05, 6.7e-05, 
              -0.000526, -0.000117, 3e-06, -0.00023, 0.003169, 0.007121, -8.3e-05, 
              0.002298, -0.000416, 0.001148, -0.000289, 0.002871, -0.000363, 
              0.002375, 0.004781, 2.8e-05, -0.000261, 0.002252, 0.000154, -0.000581, 
              0.003377, -0.000524, 0.002371, 0.001236, -4.9e-05, 0.00033, 0.002542, 
              -0.000521, -0.00013, -0.000261, 0.003705, -0.000412, 0.00257, 
              -0.00047, 0.000747, 0.002327, 0.002038, 0.002789, 0.003566, 0.002486, 
              -0.000386, -0.000435, 0.003439, 0.000766, -2e-05, -0.000463, 
              0.00638, 0.014338, -0.000167, 0.004626, -0.000839, 0.002311, 
              -0.000581, 0.005781, -0.000731, 0.004781, 0.009626, 5.6e-05, 
              -0.000525, 0.004534, 0.00031, -0.00117, 0.006799, -0.001055, 
              0.004774, 0.002489, -9.9e-05, 0.000664, 0.005118, -0.001049, 
              -0.000262, -0.000525, 0.007459, -0.000829, 0.005174, -0.000945, 
              0.001505, 0.004685, 0.004103, 0.005615, 0.00718, 0.005005, -0.000777, 
              -0.000876, 0.006924, 0.001542, -4e-05, -3e-06, 3.7e-05, 8.4e-05, 
              -1e-06, 2.7e-05, -5e-06, 1.4e-05, -3e-06, 3.4e-05, -4e-06, 2.8e-05, 
              5.6e-05, 0, -3e-06, 2.7e-05, 2e-06, -7e-06, 4e-05, -6e-06, 2.8e-05, 
              1.5e-05, -1e-06, 4e-06, 3e-05, -6e-06, -2e-06, -3e-06, 4.4e-05, 
              -5e-06, 3e-05, -6e-06, 9e-06, 2.7e-05, 2.4e-05, 3.3e-05, 4.2e-05, 
              2.9e-05, -5e-06, -5e-06, 4e-05, 9e-06, 0, 2.5e-05, -0.000348, 
              -0.000783, 9e-06, -0.000253, 4.6e-05, -0.000126, 3.2e-05, -0.000316, 
              4e-05, -0.000261, -0.000525, -3e-06, 2.9e-05, -0.000247, -1.7e-05, 
              6.4e-05, -0.000371, 5.8e-05, -0.000261, -0.000136, 5e-06, -3.6e-05, 
              -0.000279, 5.7e-05, 1.4e-05, 2.9e-05, -0.000407, 4.5e-05, -0.000282, 
              5.2e-05, -8.2e-05, -0.000256, -0.000224, -0.000306, -0.000392, 
              -0.000273, 4.2e-05, 4.8e-05, -0.000378, -8.4e-05, 2e-06, -0.000218, 
              0.003005, 0.006753, -7.9e-05, 0.002179, -0.000395, 0.001088, 
              -0.000274, 0.002723, -0.000345, 0.002252, 0.004534, 2.7e-05, 
              -0.000247, 0.002135, 0.000146, -0.000551, 0.003203, -0.000497, 
              0.002248, 0.001172, -4.7e-05, 0.000313, 0.00241, -0.000494, -0.000123, 
              -0.000247, 0.003513, -0.00039, 0.002437, -0.000445, 0.000709, 
              0.002207, 0.001932, 0.002644, 0.003382, 0.002357, -0.000366, 
              -0.000413, 0.003261, 0.000726, -1.9e-05, -1.5e-05, 0.000206, 
              0.000462, -5e-06, 0.000149, -2.7e-05, 7.4e-05, -1.9e-05, 0.000186, 
              -2.4e-05, 0.000154, 0.00031, 2e-06, -1.7e-05, 0.000146, 1e-05, 
              -3.8e-05, 0.000219, -3.4e-05, 0.000154, 8e-05, -3e-06, 2.1e-05, 
              0.000165, -3.4e-05, -8e-06, -1.7e-05, 0.00024, -2.7e-05, 0.000167, 
              -3e-05, 4.8e-05, 0.000151, 0.000132, 0.000181, 0.000231, 0.000161, 
              -2.5e-05, -2.8e-05, 0.000223, 5e-05, -1e-06, 5.6e-05, -0.000775, 
              -0.001742, 2e-05, -0.000562, 0.000102, -0.000281, 7.1e-05, -0.000703, 
              8.9e-05, -0.000581, -0.00117, -7e-06, 6.4e-05, -0.000551, -3.8e-05, 
              0.000142, -0.000826, 0.000128, -0.00058, -0.000302, 1.2e-05, 
              -8.1e-05, -0.000622, 0.000127, 3.2e-05, 6.4e-05, -0.000906, 0.000101, 
              -0.000629, 0.000115, -0.000183, -0.000569, -0.000499, -0.000682, 
              -0.000873, -0.000608, 9.4e-05, 0.000106, -0.000841, -0.000187, 
              5e-06, -0.000327, 0.004507, 0.010128, -0.000118, 0.003268, -0.000592, 
              0.001632, -0.000411, 0.004084, -0.000517, 0.003377, 0.006799, 
              4e-05, -0.000371, 0.003203, 0.000219, -0.000826, 0.004803, -0.000745, 
              0.003372, 0.001758, -7e-05, 0.000469, 0.003615, -0.000741, -0.000185, 
              -0.000371, 0.005269, -0.000585, 0.003655, -0.000668, 0.001063, 
              0.00331, 0.002898, 0.003966, 0.005072, 0.003535, -0.000549, -0.000619, 
              0.004891, 0.001089, -2.9e-05, 5.1e-05, -0.000699, -0.001571, 
              1.8e-05, -0.000507, 9.2e-05, -0.000253, 6.4e-05, -0.000634, 8e-05, 
              -0.000524, -0.001055, -6e-06, 5.8e-05, -0.000497, -3.4e-05, 0.000128, 
              -0.000745, 0.000116, -0.000523, -0.000273, 1.1e-05, -7.3e-05, 
              -0.000561, 0.000115, 2.9e-05, 5.8e-05, -0.000817, 9.1e-05, -0.000567, 
              0.000104, -0.000165, -0.000513, -0.00045, -0.000615, -0.000787, 
              -0.000548, 8.5e-05, 9.6e-05, -0.000759, -0.000169, 4e-06, -0.00023, 
              0.003164, 0.00711, -8.3e-05, 0.002294, -0.000416, 0.001146, -0.000288, 
              0.002867, -0.000363, 0.002371, 0.004774, 2.8e-05, -0.000261, 
              0.002248, 0.000154, -0.00058, 0.003372, -0.000523, 0.002367, 
              0.001234, -4.9e-05, 0.000329, 0.002538, -0.00052, -0.00013, -0.00026, 
              0.003699, -0.000411, 0.002566, -0.000469, 0.000746, 0.002324, 
              0.002035, 0.002784, 0.003561, 0.002482, -0.000385, -0.000434, 
              0.003434, 0.000765, -2e-05, -0.00012, 0.00165, 0.003707, -4.3e-05, 
              0.001196, -0.000217, 0.000597, -0.00015, 0.001495, -0.000189, 
              0.001236, 0.002489, 1.5e-05, -0.000136, 0.001172, 8e-05, -0.000302, 
              0.001758, -0.000273, 0.001234, 0.000644, -2.6e-05, 0.000172, 
              0.001323, -0.000271, -6.8e-05, -0.000136, 0.001929, -0.000214, 
              0.001338, -0.000244, 0.000389, 0.001211, 0.001061, 0.001452, 
              0.001857, 0.001294, -0.000201, -0.000226, 0.00179, 0.000399, 
              -1e-05, 5e-06, -6.6e-05, -0.000147, 2e-06, -4.8e-05, 9e-06, -2.4e-05, 
              6e-06, -5.9e-05, 8e-06, -4.9e-05, -9.9e-05, -1e-06, 5e-06, -4.7e-05, 
              -3e-06, 1.2e-05, -7e-05, 1.1e-05, -4.9e-05, -2.6e-05, 1e-06, 
              -7e-06, -5.3e-05, 1.1e-05, 3e-06, 5e-06, -7.7e-05, 9e-06, -5.3e-05, 
              1e-05, -1.5e-05, -4.8e-05, -4.2e-05, -5.8e-05, -7.4e-05, -5.1e-05, 
              8e-06, 9e-06, -7.1e-05, -1.6e-05, 0, -3.2e-05, 0.00044, 0.000989, 
              -1.2e-05, 0.000319, -5.8e-05, 0.000159, -4e-05, 0.000399, -5e-05, 
              0.00033, 0.000664, 4e-06, -3.6e-05, 0.000313, 2.1e-05, -8.1e-05, 
              0.000469, -7.3e-05, 0.000329, 0.000172, -7e-06, 4.6e-05, 0.000353, 
              -7.2e-05, -1.8e-05, -3.6e-05, 0.000515, -5.7e-05, 0.000357, -6.5e-05, 
              0.000104, 0.000323, 0.000283, 0.000387, 0.000495, 0.000345, -5.4e-05, 
              -6e-05, 0.000478, 0.000106, -3e-06, -0.000246, 0.003392, 0.007623, 
              -8.9e-05, 0.00246, -0.000446, 0.001228, -0.000309, 0.003074, 
              -0.000389, 0.002542, 0.005118, 3e-05, -0.000279, 0.00241, 0.000165, 
              -0.000622, 0.003615, -0.000561, 0.002538, 0.001323, -5.3e-05, 
              0.000353, 0.002721, -0.000557, -0.000139, -0.000279, 0.003966, 
              -0.000441, 0.002751, -0.000503, 8e-04, 0.002491, 0.002181, 0.002985, 
              0.003817, 0.002661, -0.000413, -0.000466, 0.003681, 0.00082, 
              -2.2e-05, 5e-05, -0.000695, -0.001562, 1.8e-05, -0.000504, 9.1e-05, 
              -0.000252, 6.3e-05, -0.00063, 8e-05, -0.000521, -0.001049, -6e-06, 
              5.7e-05, -0.000494, -3.4e-05, 0.000127, -0.000741, 0.000115, 
              -0.00052, -0.000271, 1.1e-05, -7.2e-05, -0.000557, 0.000114, 
              2.9e-05, 5.7e-05, -0.000813, 9e-05, -0.000564, 0.000103, -0.000164, 
              -0.00051, -0.000447, -0.000612, -0.000782, -0.000545, 8.5e-05, 
              9.5e-05, -0.000754, -0.000168, 4e-06, 1.3e-05, -0.000174, -0.00039, 
              5e-06, -0.000126, 2.3e-05, -6.3e-05, 1.6e-05, -0.000157, 2e-05, 
              -0.00013, -0.000262, -2e-06, 1.4e-05, -0.000123, -8e-06, 3.2e-05, 
              -0.000185, 2.9e-05, -0.00013, -6.8e-05, 3e-06, -1.8e-05, -0.000139, 
              2.9e-05, 7e-06, 1.4e-05, -0.000203, 2.3e-05, -0.000141, 2.6e-05, 
              -4.1e-05, -0.000128, -0.000112, -0.000153, -0.000196, -0.000136, 
              2.1e-05, 2.4e-05, -0.000189, -4.2e-05, 1e-06, 2.5e-05, -0.000348, 
              -0.000782, 9e-06, -0.000252, 4.6e-05, -0.000126, 3.2e-05, -0.000315, 
              4e-05, -0.000261, -0.000525, -3e-06, 2.9e-05, -0.000247, -1.7e-05, 
              6.4e-05, -0.000371, 5.8e-05, -0.00026, -0.000136, 5e-06, -3.6e-05, 
              -0.000279, 5.7e-05, 1.4e-05, 2.9e-05, -0.000407, 4.5e-05, -0.000282, 
              5.2e-05, -8.2e-05, -0.000256, -0.000224, -0.000306, -0.000392, 
              -0.000273, 4.2e-05, 4.8e-05, -0.000378, -8.4e-05, 2e-06, -0.000359, 
              0.004944, 0.01111, -0.000129, 0.003585, -0.00065, 0.001791, -0.000451, 
              0.00448, -0.000567, 0.003705, 0.007459, 4.4e-05, -0.000407, 0.003513, 
              0.00024, -0.000906, 0.005269, -0.000817, 0.003699, 0.001929, 
              -7.7e-05, 0.000515, 0.003966, -0.000813, -0.000203, -0.000407, 
              0.00578, -0.000642, 0.004009, -0.000733, 0.001166, 0.003631, 
              0.003179, 0.004351, 0.005564, 0.003878, -0.000602, -0.000679, 
              0.005365, 0.001195, -3.1e-05, 4e-05, -0.000549, -0.001234, 1.4e-05, 
              -0.000398, 7.2e-05, -0.000199, 5e-05, -0.000498, 6.3e-05, -0.000412, 
              -0.000829, -5e-06, 4.5e-05, -0.00039, -2.7e-05, 0.000101, -0.000585, 
              9.1e-05, -0.000411, -0.000214, 9e-06, -5.7e-05, -0.000441, 9e-05, 
              2.3e-05, 4.5e-05, -0.000642, 7.1e-05, -0.000445, 8.1e-05, -0.00013, 
              -0.000403, -0.000353, -0.000483, -0.000618, -0.000431, 6.7e-05, 
              7.5e-05, -0.000596, -0.000133, 3e-06, -0.000249, 0.003429, 0.007706, 
              -9e-05, 0.002487, -0.000451, 0.001242, -0.000313, 0.003107, -0.000393, 
              0.00257, 0.005174, 3e-05, -0.000282, 0.002437, 0.000167, -0.000629, 
              0.003655, -0.000567, 0.002566, 0.001338, -5.3e-05, 0.000357, 
              0.002751, -0.000564, -0.000141, -0.000282, 0.004009, -0.000445, 
              0.002781, -0.000508, 0.000809, 0.002518, 0.002205, 0.003018, 
              0.003859, 0.00269, -0.000418, -0.000471, 0.003722, 0.000829, 
              -2.2e-05, 4.6e-05, -0.000627, -0.001408, 1.6e-05, -0.000454, 
              8.2e-05, -0.000227, 5.7e-05, -0.000568, 7.2e-05, -0.00047, -0.000945, 
              -6e-06, 5.2e-05, -0.000445, -3e-05, 0.000115, -0.000668, 0.000104, 
              -0.000469, -0.000244, 1e-05, -6.5e-05, -0.000503, 0.000103, 2.6e-05, 
              5.2e-05, -0.000733, 8.1e-05, -0.000508, 9.3e-05, -0.000148, -0.00046, 
              -0.000403, -0.000551, -0.000705, -0.000491, 7.6e-05, 8.6e-05, 
              -0.00068, -0.000151, 4e-06, -7.2e-05, 0.000997, 0.002242, -2.6e-05, 
              0.000723, -0.000131, 0.000361, -9.1e-05, 0.000904, -0.000114, 
              0.000747, 0.001505, 9e-06, -8.2e-05, 0.000709, 4.8e-05, -0.000183, 
              0.001063, -0.000165, 0.000746, 0.000389, -1.5e-05, 0.000104, 
              8e-04, -0.000164, -4.1e-05, -8.2e-05, 0.001166, -0.00013, 0.000809, 
              -0.000148, 0.000235, 0.000733, 0.000641, 0.000878, 0.001123, 
              0.000782, -0.000121, -0.000137, 0.001082, 0.000241, -6e-06, -0.000226, 
              0.003105, 0.006979, -8.1e-05, 0.002252, -0.000408, 0.001125, 
              -0.000283, 0.002814, -0.000356, 0.002327, 0.004685, 2.7e-05, 
              -0.000256, 0.002207, 0.000151, -0.000569, 0.00331, -0.000513, 
              0.002324, 0.001211, -4.8e-05, 0.000323, 0.002491, -0.00051, -0.000128, 
              -0.000256, 0.003631, -0.000403, 0.002518, -0.00046, 0.000733, 
              0.002281, 0.001997, 0.002733, 0.003495, 0.002436, -0.000378, 
              -0.000426, 0.00337, 0.000751, -2e-05, -0.000198, 0.002719, 0.006111, 
              -7.1e-05, 0.001972, -0.000357, 0.000985, -0.000248, 0.002464, 
              -0.000312, 0.002038, 0.004103, 2.4e-05, -0.000224, 0.001932, 
              0.000132, -0.000499, 0.002898, -0.00045, 0.002035, 0.001061, 
              -4.2e-05, 0.000283, 0.002181, -0.000447, -0.000112, -0.000224, 
              0.003179, -0.000353, 0.002205, -0.000403, 0.000641, 0.001997, 
              0.001749, 0.002393, 0.00306, 0.002133, -0.000331, -0.000373, 
              0.002951, 0.000657, -1.7e-05, -0.00027, 0.003721, 0.008363, -9.7e-05, 
              0.002698, -0.000489, 0.001348, -0.000339, 0.003372, -0.000427, 
              0.002789, 0.005615, 3.3e-05, -0.000306, 0.002644, 0.000181, -0.000682, 
              0.003966, -0.000615, 0.002784, 0.001452, -5.8e-05, 0.000387, 
              0.002985, -0.000612, -0.000153, -0.000306, 0.004351, -0.000483, 
              0.003018, -0.000551, 0.000878, 0.002733, 0.002393, 0.003275, 
              0.004188, 0.002919, -0.000453, -0.000511, 0.004038, 0.000899, 
              -2.4e-05, -0.000346, 0.004759, 0.010695, -0.000125, 0.003451, 
              -0.000625, 0.001724, -0.000434, 0.004312, -0.000546, 0.003566, 
              0.00718, 4.2e-05, -0.000392, 0.003382, 0.000231, -0.000873, 0.005072, 
              -0.000787, 0.003561, 0.001857, -7.4e-05, 0.000495, 0.003817, 
              -0.000782, -0.000196, -0.000392, 0.005564, -0.000618, 0.003859, 
              -0.000705, 0.001123, 0.003495, 0.00306, 0.004188, 0.005356, 0.003733, 
              -0.00058, -0.000653, 0.005165, 0.00115, -3e-05, -0.000241, 0.003317, 
              0.007454, -8.7e-05, 0.002405, -0.000436, 0.001201, -0.000302, 
              0.003006, -0.00038, 0.002486, 0.005005, 2.9e-05, -0.000273, 0.002357, 
              0.000161, -0.000608, 0.003535, -0.000548, 0.002482, 0.001294, 
              -5.1e-05, 0.000345, 0.002661, -0.000545, -0.000136, -0.000273, 
              0.003878, -0.000431, 0.00269, -0.000491, 0.000782, 0.002436, 
              0.002133, 0.002919, 0.003733, 0.002602, -0.000404, -0.000455, 
              0.0036, 0.000802, -2.1e-05, 3.7e-05, -0.000515, -0.001157, 1.3e-05, 
              -0.000373, 6.8e-05, -0.000187, 4.7e-05, -0.000467, 5.9e-05, -0.000386, 
              -0.000777, -5e-06, 4.2e-05, -0.000366, -2.5e-05, 9.4e-05, -0.000549, 
              8.5e-05, -0.000385, -0.000201, 8e-06, -5.4e-05, -0.000413, 8.5e-05, 
              2.1e-05, 4.2e-05, -0.000602, 6.7e-05, -0.000418, 7.6e-05, -0.000121, 
              -0.000378, -0.000331, -0.000453, -0.00058, -0.000404, 6.3e-05, 
              7.1e-05, -0.000559, -0.000124, 3e-06, 4.2e-05, -0.000581, -0.001305, 
              1.5e-05, -0.000421, 7.6e-05, -0.00021, 5.3e-05, -0.000526, 6.7e-05, 
              -0.000435, -0.000876, -5e-06, 4.8e-05, -0.000413, -2.8e-05, 0.000106, 
              -0.000619, 9.6e-05, -0.000434, -0.000226, 9e-06, -6e-05, -0.000466, 
              9.5e-05, 2.4e-05, 4.8e-05, -0.000679, 7.5e-05, -0.000471, 8.6e-05, 
              -0.000137, -0.000426, -0.000373, -0.000511, -0.000653, -0.000455, 
              7.1e-05, 8e-05, -0.00063, -0.00014, 4e-06, -0.000333, 0.004589, 
              0.010313, -0.00012, 0.003328, -0.000603, 0.001662, -0.000418, 
              0.004158, -0.000526, 0.003439, 0.006924, 4e-05, -0.000378, 0.003261, 
              0.000223, -0.000841, 0.004891, -0.000759, 0.003434, 0.00179, 
              -7.1e-05, 0.000478, 0.003681, -0.000754, -0.000189, -0.000378, 
              0.005365, -0.000596, 0.003722, -0.00068, 0.001082, 0.00337, 0.002951, 
              0.004038, 0.005165, 0.0036, -0.000559, -0.00063, 0.00498, 0.001109, 
              -2.9e-05, -7.4e-05, 0.001022, 0.002297, -2.7e-05, 0.000741, -0.000134, 
              0.00037, -9.3e-05, 0.000926, -0.000117, 0.000766, 0.001542, 9e-06, 
              -8.4e-05, 0.000726, 5e-05, -0.000187, 0.001089, -0.000169, 0.000765, 
              0.000399, -1.6e-05, 0.000106, 0.00082, -0.000168, -4.2e-05, -8.4e-05, 
              0.001195, -0.000133, 0.000829, -0.000151, 0.000241, 0.000751, 
              0.000657, 0.000899, 0.00115, 0.000802, -0.000124, -0.00014, 0.001109, 
              0.000247, -6e-06, 2e-06, -2.7e-05, -6e-05, 1e-06, -1.9e-05, 4e-06, 
              -1e-05, 2e-06, -2.4e-05, 3e-06, -2e-05, -4e-05, 0, 2e-06, -1.9e-05, 
              -1e-06, 5e-06, -2.9e-05, 4e-06, -2e-05, -1e-05, 0, -3e-06, -2.2e-05, 
              4e-06, 1e-06, 2e-06, -3.1e-05, 3e-06, -2.2e-05, 4e-06, -6e-06, 
              -2e-05, -1.7e-05, -2.4e-05, -3e-05, -2.1e-05, 3e-06, 4e-06, -2.9e-05, 
              -6e-06, 0), .Dim = c(42L, 42L))



#test if positive semi definite

is.positive.semi.definite(root_not_working)


#estimation of the nearest positive semi definite matrix

r_matrix <- nearPD(root_not_working, keepDiag =TRUE, maxit = 100000)


#calculation of the matrix root

###
###
#1
r_dec0 <- eigen(r_matrix$mat)

#2
r_ev0 <- r_dec0$vectors

#3
r_ew0 <- diag(r_dec0$values)

###
###

#4 Root of the matrix
r_rootkov = r_ev0 %*% r_ew0^{0.5} %*% t(r_ev0) 
struto
  • 1
  • 1
  • 1
    Are you sure that you not have the problem of nearly multicollinearity? – jogo Feb 07 '18 at 13:05
  • how would this be a problem? In some cases I have a very high correlation between my variables, yes. But it´s the result of my model. – struto Feb 07 '18 at 16:30
  • http://web.mit.edu/ehliu/Public/Yelp/conditioning_and_precision.pdf https://en.wikipedia.org/wiki/Numerical_stability https://en.wikipedia.org/wiki/Condition_number http://radio.feld.cvut.cz/matlab/toolbox/control/numerical/relcomp2.html – jogo Feb 08 '18 at 07:13
  • ok, regarding your links I have ill-conditioned matrices? and therefore I can´t calculate the root of the matrix? sorry, I´m no mathematician, perhaps I miss the crucial point of information... – struto Feb 08 '18 at 16:27
  • Multicollinearity definitely can cause this. Also, how many data points did you use in estimating the covariance matrix? If you don't have enough, that could cause this behaviour too. – kangaroo_cliff Feb 08 '18 at 23:50
  • @struto Yes, that it is what I meant. But if this is the case your question is off-topic for SO because this is a modelling problem and not a problem of programming. A numerical more stable programming for a similar situation you find in the code of `lm()`: to get the OLS-solution *b* (*from (Xt * X) * b = Xt * Y*) not the matrix *Xt * X* is factorised but one uses a QR-decomposition of the model matrix *X*. – jogo Feb 09 '18 at 08:37
  • @headpoint: the datasets for each covariance are different regarding which event happended in my simulation. it could be, that I have 999 to 1 times the same value because the objects have died in 999 cases before (it´s a time depending model). I also set the covariance values to zero, if I had no standarddeviation and couldn´t therefore calculate the covariance. – struto Feb 15 '18 at 10:16
  • @jogo: thank you for your answer - I try to understand your hint with lm(), perhaps it takes a while. ;) changing my modell to produce better marices will be not possible I think... – struto Feb 15 '18 at 10:24
  • @struto I didn't understand your comment. Unless you can give us a good idea how the covariance matrices are created, it is difficult to help you. – kangaroo_cliff Feb 16 '18 at 00:40
  • @Suren: sorry, if my explanation was not clear enough. I do a Monte-Carlo-Simulation - I have events that can happen over time(t) on different places(p) (the probability of happening is different over time and place). If a event happens it destroyes the place for the next time periods -> money return = 0 or something very small. The covariance should show me how the places in time are related for having a loss of money. When I have a "bad" place it could happen for example, that it´s destroyed always before timepoint 10 - for time 11,12,13(..) is just the same value after damage. – struto Feb 23 '18 at 10:55

1 Answers1

0

One option it to rescale the numbers.

Floating point errors are caused by values close to zero. If the values are transformed to be away from zero, there will be fewer floating point errors.

Brian Spiering
  • 1,002
  • 1
  • 9
  • 18