2
votes

I have a estimate of covariance matrix

I want to take the inverse of this matrix, R gives me the following error

A=[ 3529861.470  8785861.47  6920.344 17120.34;
     8785861.470 26209861.47 17120.344 51920.34;
    6920.344    17120.34    14.000    34.00;
    17120.344    51920.34    34.000   104.00]

"Error in solve.default(l) : system is computationally singular: reciprocal condition number = 2.14511e-22".

however Matlab does the inverse calculation without giving any error message. Does anyone know the reason why R is giving me error? Is the any other way to calculate inverse

1
Matlab gives a condition number (cond(A)) of 9.5e12, so the matrix is actually close to singular. Perhaps R just has a stricter threshold than Matlab to declare a matrix as close to singular - Luis Mendo
Your example isn't reproducible. solve() has no trouble inverting this matrix. - Richard Border
That's because solve use gauss elimination avoiding determinant calculation - Mikhail Genkin
@Spandy MATLAB's rcond returns 7.5E-14. Are you sure that MATLAB and R have the same matrix? - TroyHaskin
solve inverts this matrix with no errors or warnings for me in R as well. - joran

1 Answers

5
votes
A <- matrix(c(3529861.470,8785861.47,6920.344,17120.34,
       8785861.470,26209861.47,17120.344,51920.34,
   6920.344,17120.34,14.000,34.00,
   17120.344,51920.34,34.000,104.00),
     nrow=4,byrow=TRUE)

solve(A) ## works on my system

##              [,1]         [,2]         [,3]        [,4]
## [1,]   -1.2515442    0.7617239     535.4871   -349.3141
## [2,]    0.7617072   -0.4635922    -325.9051    212.5957
## [3,]  535.4884664 -325.9130639 -229114.3516 149458.2734
## [4,] -349.3061387  212.5955306  149454.4973 -97492.7335

eigen(A)$values
## [1]  2.921525e+07  5.245875e+05  1.440703e+00 -3.061760e-06

rcond(A)  ## condition number
## [1] 7.516179e-14

You should be able to adjust the tol parameter if you are having trouble inverting a matrix, but this is of course done at your own risk -- you're overriding a warning that the matrix operation may be numerically unstable.

This is with

R Under development (unstable) (2015-02-11 r67792)
Platform: i686-pc-linux-gnu (32-bit)
Running under: Ubuntu precise (12.04.5 LTS)