1.6 Taking the inverse of a square matrix
The inverse of a matrix is denoted \(\mathbf{A}^{-1}\). You can think of the inverse of a matrix like \(1/a\). \(1/a \times a = 1\). \(\mathbf{A}^{-1}\mathbf{A} = \mathbf{A}\mathbf{A}^{-1} = \mathbf{I}\). The inverse of a matrix does not always exist; for one it has to be square. We’ll be using inverses for variance-covariance matrices and by definition (of a variance-covariance matrix), the inverse of those exist. In R, there are a couple way common ways to take the inverse of a variance-covariance matrix (or something with the same properties). solve()
is the most common probably:
= diag(3, 3) + matrix(1, 3, 3)
A = solve(A)
invA %*% A invA
[,1] [,2] [,3]
[1,] 1.000000e+00 -6.938894e-18 0
[2,] 2.081668e-17 1.000000e+00 0
[3,] 0.000000e+00 0.000000e+00 1
%*% invA A
[,1] [,2] [,3]
[1,] 1.000000e+00 -6.938894e-18 0
[2,] 2.081668e-17 1.000000e+00 0
[3,] 0.000000e+00 0.000000e+00 1
Another option is to use chol2inv()
which uses a Cholesky decomposition:
= diag(3, 3) + matrix(1, 3, 3)
A = chol2inv(chol(A))
invA %*% A invA
[,1] [,2] [,3]
[1,] 1.000000e+00 6.938894e-17 0.000000e+00
[2,] 2.081668e-17 1.000000e+00 -2.775558e-17
[3,] -5.551115e-17 0.000000e+00 1.000000e+00
%*% invA A
[,1] [,2] [,3]
[1,] 1.000000e+00 2.081668e-17 -5.551115e-17
[2,] 6.938894e-17 1.000000e+00 0.000000e+00
[3,] 0.000000e+00 -2.775558e-17 1.000000e+00
For the purpose of this course, solve()
is fine.