Whenever I review linear algebra, I am reminded that the fundamental concepts, or at least the ones that are practically important are pretty simple. There are two general steps involved in evaluating matrices/systems of equations.

  1. Check to see if a system of equations has a solution/ a non-zero determinant.
  2. Solve for the solution.

However, actually doing the calculations for them is incredibly tedious. This time around, I figured I would just do it in R. There are easier ways of solving these problems with a computer than the way I am about to show you, but this step by step approach is good practice with R.

Below is an example from the text book Fundamental Methods of Mathematical Economics (Chiang, 1984), which uses Cramer’s Rule to solve a system of equations. For some reason, I wasn’t taught Cramer’s Rule when I took linear algebra, even though it is easier than solving a system by putting it in reduced row echelon form.

First lets start by creating the matrix in R.

A<-matrix(c(7,10,6,-1,-2,3,-1,1,-2,0,8,7),3,4)
A
##      [,1] [,2] [,3] [,4]
## [1,]    7   -1   -1    0
## [2,]   10   -2    1    8
## [3,]    6    3   -2    7
#When you create a matrix like this, the last two arguments define the dimensions of the matrix, and the elements af the vector are assigned to the matrix by filling out the columns from left to right.

#alternately, A<-rbind(c(7,-1,-1,0), c(10,-2,1,8), c(6,3,-2,7))

This matrix represents a system with the form Ax=d Relative to this matrix, the first three columns represent the variable coefficients contained in A, and the last column represents the constants contained in d.

Next we want to check to see if this matrix even has a solution. If its determinant is non-zero, then it has a solution.

det(A[,-4])
## [1] -61
#"[,-4]" means that I am taking a subset that omits the last column.

Great. Now to move on to finding the solution.

The math behind the Cramer Method is a little abstract, but the application is quite simple. Basically, you substitute the column of constants the column of the variable that you are solving in the coefficient matrix. Then you take its determinant, and divide by the regular determinant. Do this for each column/variable.

Ad<-round(det(A[,-4]), digits=0)

#Because of computer math which involves approximation, the solution that R gives you for these problems can wind up being an irrational number that approaches an integer. Using the round function at this stage to get an integer determinant fixes this problem.



Ad1<-det(A[,c(4,2,3)])

Ad2<-det(A[,c(1,4,3)])

Ad3<-det(A[,c(1,2,4)])

x1<-Ad1/Ad

x2<-Ad2/Ad

x3<-Ad3/Ad

X<-c(x1,x2,x3)
X
## [1] 1 3 4

We can verify that this is the answer by multiplying it by A, which should give us d.

d<-A[,-4] %*% as.integer(X)
d
##      [,1]
## [1,]    0
## [2,]    8
## [3,]    7
#we use the as.integer function here for the same reason that we used round earlier. 

#Heres the easy way to do it, based off the orignial matrix we set up

solve(A[,-4],A[,4])
## [1] 1 3 4
#This is equivalent to A and d for the parameters.

Alternately, you define define two separate matrices for the coefficients and the constants without subsetting.

B<-rbind(c(7,-1,-1), c(10,-2,1), c(6,3,-2))

C<-c(0,8,7)

solve(B,C)
## [1] 1 3 4

As I alluded to at the outset, its good to practice things the hard way with Base R, so that you learn the syntax of R, and can think your way through problems that require more manual work.

Works Cited:

Chiang, A. C. (1984). Fundamental Methods of Mathematical Economics. (3rd ed., pp. 107- 110). McGraw-Hill, Inc.