I was attempting to calculate an OLS regression in R when I saw some strange things. The inverse of a square matrix does not exist if and only if the determinants is 0. Also, the matrix must be of full rank.
So not sure how the below is possible:
> dim(X)
[1] 20000 51
> det(t(X) %*% X)
[1] 3.863823e+161 #non-zero
> solve(t(X) %*% X)
Error in solve.default(t(X) %*% X) :
system is computationally singular: reciprocal condition number = 3.18544e-17
Why is solve() throw an error when trying to calculate the inverse when we know the determinant is not zero? What am I missing here?
Checked that the matrix has full rank:
> qr(t(X) %*% X)$rank
[1] 51
But then just to test further I reassigned one of the X columns to the same value of another:
> X[,2] = X[,3]
Thus, two columns of the X matrix are now the same.
> qr(t(X) %*% X)$rank
[1] 50
We now can confirm the X'X matrix is not of full rank.
> det(t(X) %*% X)
[1] 1.634637e+138
But the determinant is still not equal to 0? How is this possible and what am I missing?