How to Calculate Inverse Matrix in R (With Examples)


The inverse of a matrix is defined as a matrix that you can multiply an original matrix by to get the identity matrix.

An identity matrix has the following properties:

  • It is a square matrix (same number of rows as columns)
  • It has a value of 1 on the diagonals of the matrix
  • It has a value of 0 in all other locations

Often we use statistical programming software such as R to calculate the inverse of a matrix because it can be computationally intensive for large matrices.

There are two common ways to calculate the inverse of a matrix in R:

Method 1: Use the inv() Function from matlib Package

library(matlib)

#calculate inverse of matrix named 'my_matrix'
inv_matrix <- inv(my_matrix)

Method 2: Use the ginv() Function from MASS Package

library(MASS)

#calculate inverse of matrix named 'my_matrix'
inv_matrix <- ginv(my_matrix)

Both of these functions will return the inverse of the matrix named my_matrix.

It’s important to note that the determinant of a matrix must not be equal to zero in order for an inverse of the matrix to exist.

You can use the det() function from base R to calculate the determinant of a matrix before attempting to calculate the inverse of the matrix:

#calculate determinant of matrix
det(my_matrix)

If this function returns a value that is not zero, then you can proceed to calculate the inverse of the matrix.

The following examples show how to use each of these methods in practice to calculate the inverse of a matrix in R.

Example: How to Calculate Inverse of Matrix in R

Suppose we create the following matrix named my_matrix that contains 3 rows and 3 columns:

#create 3x3 matrix
my_matrix <- matrix(c(2, 5, -3, 0, 2, 6, 5, 5, 8), nrow=3)

#view matrix
my_matrix

     [,1] [,2] [,3]
[1,]    2    0    5
[2,]    5    2    5
[3,]   -3    6    8

Since this is a square matrix (same number of rows and columns) we can attempt to calculate the inverse of this matrix.

Before doing so, we can use the det() function to calculate the determinant of the matrix to ensure that it is not equal to zero:

#calculate determinant of matrix
det(my_matrix)

[1] 152

This returns a value of 152, which tells us that the inverse matrix exists since this value is not equal to zero.

We can proceed to use the inv() function from the matlib package to calculate the inverse of this matrix:

library(matlib)

#calculate inverse of matrix named 'my_matrix'
inv_matrix <- inv(my_matrix)

#view inverse matrix
inv_matrix

            [,1]        [,2]        [,3]
[1,] -0.09210526  0.19736842 -0.06578947
[2,] -0.36184211  0.20394737  0.09868421
[3,]  0.23684211 -0.07894737  0.02631579

The inv_matrix object contains the inverse of the matrix named my_matrix.

Note that we also could have used the ginv() function from the MASS package to calculate the inverse of the matrix:

library(MASS)

#calculate inverse of matrix named 'my_matrix'
inv_matrix <- ginv(my_matrix)

#view inverse matrix
inv_matrix

            [,1]        [,2]        [,3]
[1,] -0.09210526  0.19736842 -0.06578947
[2,] -0.36184211  0.20394737  0.09868421
[3,]  0.23684211 -0.07894737  0.02631579

Notice that this returns the same result.

Lastly, we can verify that the inverse matrix has been calculated correctly by multiplying the inverse matrix by the original matrix:

#multiply inverse of matrix by original matrix
my_matrix %*% inv_matrix

      [,1]   [,2]  [,3]
[1,] 1e+00 -1e-08 1e-08
[2,] 3e-08  1e+00 2e-08
[3,] 0e+00  0e+00 1e+00

The result is the identity matrix, which contains 1‘s along the diagonals of the matrix and 0 in all other locations.

This confirms that the inverse matrix was calculated correctly.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Sort a Matrix in R
How to Remove NA from Matrix in R
How to Convert Data Frame to Matrix in R
How to Convert a Table to a Matrix in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *