graphics.off()# Clear plots. Can use par(mfrow=c(1,1))
1 Theory
Either look at the math camp notes, or on some simplified online blogs.
Inverse of a matrix is defined only for a square matrix, and cannot be calculated for non-square matrices.
Furthermore, not all squares matrices have an inverse - it requires linear independence of all columns and rows i.e. the vector or data is not redundant in the matrix.
A simpler way to see if the inverse of matrix exists if you write out the formula of the inverse of a matrix in terms of the dividing the adjoint of the matrix (which always exists) by the determinant of the matrix. \[
A^{-1}=\dfrac{Adj \ A}{det(A)}
\]
The determinant of the matrix will be 0 if the rows or columns are linearly dependent. Since we cannot divide by zero, the matrix inverse will not exist if the determinant is 0. In this instance, we will say we have an singular matrix.
A matrix is nonsingular if and only if its inverse exist - i.e. its determinant is not 0.
We need non-singular (data) matrix for our linear regression.
Thus, you should always check for variable values and duplicate rows in your data to be able to check if you can estimate your “beta” coefficients in your linear regression as you have to invert the your data matrix to find it. Most statistical software will automatically drop duplicate rows but will give you an error if the matrix is not invertible.
??inverse# Use the solve() function to calculate the inverse.# solve(B)# Using tryCatch to catch and print the error messageresult<-tryCatch({solve(B)}, error =function(e){cat("Error: ", conditionMessage(e), "\n")})
Error: Lapack routine dgesv: system is exactly singular: U[3,3] = 0
You may not be able to visually eyeball that the third row is a linear combination of the first two rows, with an equal weight on row 1 and row 2. But calculating the determinant identifies this (i.e. det(C) = 0) and thus you know you will not be able to calculate the inverse of the matrix C as it should not exist. solve(C) command gives us an error, as expected.
# Row 1m11<-1m12<-2m13<-2# Row 2m21<--2m22<-.5m23<-0# Row 3 is a linear combination of Row 1 and Row 2C<-matrix(data =c(m11,m12,m13,m21,m22,m23,2*m11+2*m21,2*m12+2*m22,2*m13+2*m23), nrow =3, ncol =3, byrow =TRUE)C
#solve(C)# Using tryCatch to catch and print the error messageresult<-tryCatch({solve(C)}, error =function(e){cat("Error: ", conditionMessage(e), "\n")})
Error: Lapack routine dgesv: system is exactly singular: U[3,3] = 0
2.5 Matrix D
You may not be able to visually eyeball that the third row is a linear combination of the first two rows, with a weight of -2 on row 1 and a weight of 3 on the second row. Seeing the pattern is harder here than matrix C above. But again calculating the determinant identifies this (i.e. det(D) = 0) and thus you know you will not be able to calculate the inverse of the matrix D as it should not exist. solve(D) command gives us an error, as expected.
#solve(D)# Using tryCatch to catch and print the error messageresult<-tryCatch({solve(D)}, error =function(e){cat("Error: ", conditionMessage(e), "\n")})
Error: system is computationally singular: reciprocal condition number = 2.77556e-18
2.6 Matrix E
Determinant of E is not defined if you have NA , non-numerical or missing values.