Module 5: Doing math with R. Part 1

This week's assignment we will focus on matrices and the linear algebra we can do using R.

We are given two matrices specifications:
A=matrix(1:100, nrow=10)
B=matrix(1:1000, nrow=10)
And told to find an inverse of a matrix and determinant of a matrix using these values.
The goal here is starting with these values, we will manipulate these in order for us to attain an inverse matrix and a determinant.

Github link : https://github.com/Ant-nguyen/Intro_r_2021/blob/main/Module%205.R


Let us begin


First we must create matrices A and B in R. To do this we simply assign the matrices to the corresponding variables:

A <- matrix(1:100, nrow=10)

B <- matrix(1:1000, nrow=10)

We get the following matrices:

A <- matrix(1:100, nrow = 10)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1   11   21   31   41   51   61   71   81    91
 [2,]    2   12   22   32   42   52   62   72   82    92
 [3,]    3   13   23   33   43   53   63   73   83    93
 [4,]    4   14   24   34   44   54   64   74   84    94
 [5,]    5   15   25   35   45   55   65   75   85    95
 [6,]    6   16   26   36   46   56   66   76   86    96
 [7,]    7   17   27   37   47   57   67   77   87    97
 [8,]    8   18   28   38   48   58   68   78   88    98
 [9,]    9   19   29   39   49   59   69   79   89    99
[10,]   10   20   30   40   50   60   70   80   90   100

B <- matrix(1:1000,nrow = 10)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
 [1,]    1   11   21   31   41   51   61   71   81    91   101   111   121   131   141   151
 [2,]    2   12   22   32   42   52   62   72   82    92   102   112   122   132   142   152
 [3,]    3   13   23   33   43   53   63   73   83    93   103   113   123   133   143   153
 [4,]    4   14   24   34   44   54   64   74   84    94   104   114   124   134   144   154
 [5,]    5   15   25   35   45   55   65   75   85    95   105   115   125   135   145   155
 [6,]    6   16   26   36   46   56   66   76   86    96   106   116   126   136   146   156
 [7,]    7   17   27   37   47   57   67   77   87    97   107   117   127   137   147   157
 [8,]    8   18   28   38   48   58   68   78   88    98   108   118   128   138   148   158
 [9,]    9   19   29   39   49   59   69   79   89    99   109   119   129   139   149   159
[10,]   10   20   30   40   50   60   70   80   90   100   110   120   130   140   150   160
...
...
...
      [,77] [,78] [,79] [,80] [,81] [,82] [,83] [,84] [,85] [,86] [,87] [,88] [,89] [,90] [,91]
 [1,]   761   771   781   791   801   811   821   831   841   851   861   871   881   891   901
 [2,]   762   772   782   792   802   812   822   832   842   852   862   872   882   892   902
 [3,]   763   773   783   793   803   813   823   833   843   853   863   873   883   893   903
 [4,]   764   774   784   794   804   814   824   834   844   854   864   874   884   894   904
 [5,]   765   775   785   795   805   815   825   835   845   855   865   875   885   895   905
 [6,]   766   776   786   796   806   816   826   836   846   856   866   876   886   896   906
 [7,]   767   777   787   797   807   817   827   837   847   857   867   877   887   897   907
 [8,]   768   778   788   798   808   818   828   838   848   858   868   878   888   898   908
 [9,]   769   779   789   799   809   819   829   839   849   859   869   879   889   899   909
[10,]   770   780   790   800   810   820   830   840   850   860   870   880   890   900   910
      [,92] [,93] [,94] [,95] [,96] [,97] [,98] [,99] [,100]
 [1,]   911   921   931   941   951   961   971   981    991
 [2,]   912   922   932   942   952   962   972   982    992
 [3,]   913   923   933   943   953   963   973   983    993
 [4,]   914   924   934   944   954   964   974   984    994
 [5,]   915   925   935   945   955   965   975   985    995
 [6,]   916   926   936   946   956   966   976   986    996
 [7,]   917   927   937   947   957   967   977   987    997
 [8,]   918   928   938   948   958   968   978   988    998
 [9,]   919   929   939   949   959   969   979   989    999
[10,]   920   930   940   950   960   970   980   990   1000

One condition to obtaining a determinant or inverse of a matrix is the matrix must be a square matrix. What this means is that the rows and columns of the matrix must be the same. Since Matrix A is already square(10x10), we will begin first with Matrix A.

Matrix A

We will be using the following functions in R to obtain the determinant and inverse matrix :

det() # will return the determinant of a matrix

solve()# will return an inverse matrix if an invertible matrix is inputted

In order for a matrix to have an inverse matrix it must have a determinant , so we will first find the determinant of Matrix A with :

>det(A)

[1] 0

Uh oh, a determinant value of 0 indicates that the matrix does not have an inverse matrix. We can test this with solve(A):

>solve(A)

Error in solve.default(A) : 

  Lapack routine dgesv: system is exactly singular: U[6,6] = 0

As expected, R is telling us the matrix is singular and can not produce an inverse matrix. 

So now what?!

Well one thing we can do to our current matrix is transpose it. What this essentially does is flip the rows and columns of the matrix (i.e: element in [1,2] would be swapped with element in [2,1]), hopefully this gives us a matrix with a determinant and therefore an inverse.

To do this in R, we simply use the t() function.

>a <-t(A)# we assign the transpose of matrix A to variable a


This is our result:

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    2    3    4    5    6    7    8    9    10
 [2,]   11   12   13   14   15   16   17   18   19    20
 [3,]   21   22   23   24   25   26   27   28   29    30
 [4,]   31   32   33   34   35   36   37   38   39    40
 [5,]   41   42   43   44   45   46   47   48   49    50
 [6,]   51   52   53   54   55   56   57   58   59    60
 [7,]   61   62   63   64   65   66   67   68   69    70
 [8,]   71   72   73   74   75   76   77   78   79    80
 [9,]   81   82   83   84   85   86   87   88   89    90
[10,]   91   92   93   94   95   96   97   98   99   100

Now we can test to see if this matrix has a determinant:

>det(a)

[1] 3.49659e-113


Since matrix a has a determinant it technically has an inverse matrix,

but when we try solve(a), we get the following error:

Error in solve.default(a) : 

  system is computationally singular: reciprocal condition number = 2.46047e-19

What does this error mean?!
Well from looking online and searching the RStudios help menu(See reference below if interested). The error is essentially telling us that by default solve() has a reciprocal condition number limit, numbers lower than that limit R considers “computationally singular”. Luckily solve() has an argument called tol which allows us to bypass this limit. R tells us what the reciprocal condition number of matrix a is (2.46047e-19) and knowing this we can make the limit lower than this value, such as 1e-20.

> solve(a,tol= 1e-20)

Our result:

               [,1]          [,2]          [,3]          [,4]          [,5]          [,6]
 [1,] -5.634484e+14  5.802766e+14 -8.248661e+13 -1.235905e+11  5.587555e+13  1.652204e+14
 [2,] -7.327203e+14  8.295997e+14 -4.955475e+13  2.505479e+12  8.134810e+13  9.409855e+13
 [3,]  1.864798e+15 -1.926228e+15  2.100562e+14 -4.309536e+12 -3.315649e+14 -4.114933e+14
 [4,]  1.566059e+15 -1.700085e+15  1.035485e+14 -2.868815e+12 -1.650109e+14 -2.013593e+14
 [5,]  4.431985e+15 -6.553898e+15  3.332248e+15 -8.857555e+12 -3.354985e+14 -2.143182e+15
 [6,] -7.114063e+15  9.350894e+15 -3.598828e+15  1.572532e+13  7.501241e+14  2.662606e+15
 [7,] -3.110972e+14  3.223602e+14 -1.541846e+14  1.406863e+14  1.001476e+14  2.699667e+13
 [8,]  5.753854e+14 -6.767767e+14  2.734311e+14 -1.413169e+14  1.113297e+13 -2.642284e+14
 [9,] -5.438296e+15  7.577206e+15 -3.303103e+15 -1.295905e+14  5.116423e+14  2.361788e+15
[10,]  5.721398e+15 -7.803349e+15  3.268873e+15  1.281498e+14 -6.781964e+14 -2.290447e+15
               [,7]          [,8]          [,9]         [,10]
 [1,]  1.960053e+13 -1.082916e+12  1.007563e+13 -1.839072e+14
 [2,] -1.110828e+14 -5.184998e+13 -5.665069e+13 -5.693305e+12
 [3,]  7.002996e+13  1.203892e+14  3.503181e+13  3.732902e+14
 [4,]  8.180262e+13  1.062553e+14 -2.890647e+13  2.405648e+14
 [5,] -3.486459e+14  3.392499e+14 -1.116162e+14  1.398215e+15
 [6,]  3.074136e+14 -5.844309e+14  1.624414e+14 -1.951882e+15
 [7,] -5.194450e+13 -2.014751e+13  4.357206e+13 -9.638895e+13
 [8,]  4.459921e+13  4.229854e+13 -4.751712e+13  1.829919e+14
 [9,]  3.114425e+14 -4.383910e+14  1.633743e+14 -1.616072e+15
[10,] -3.232152e+14  4.877093e+14 -1.698048e+14  1.658882e+15

It may not be pretty but at least we were able to get an inverse matrix using Matrix A.


Matrix B

Now for Matrix B, the first thing that stands out is that Matrix B is not a square matrix. It is 10x100.

One way to fix this issue is to matrix multiple Matrix B with a 100x10, the product of this matrix will be a 10x10 matrix. One 100x10 matrix that come to mind is the transpose of Matrix B.

Again we do this by using the t() function:

> b <- t(B)

Next we matrix multiple Matrix B with its transpose with the following line, we shall call the new 10x10 Matrix Bb.

> Bb <- B %*%b

Excellent, now we have a square matrix.

We again check to see if our Matrix Bb has a determinant. It does!

> det(Bb)

[1] -7.168207e-62

When we try using default solve() on Matrix Bb we had the same issue as with the reciprocal condition number. This time our reciprocal condition number was 2.00809e-22. So we again use the tol= argument.

>solve(Bb,tol = 1e-23)

We get the following result:

               [,1]       [,2]          [,3]          [,4]          [,5]          [,6]
 [1,] -5.024464e+08  436218813 -2.181114e+08  5.518944e+08 -6.622717e+07 -2.348820e+08
 [2,] -3.046496e+08  167780554  5.032692e+07  1.563031e+08 -1.368673e+08  3.355234e+07
 [3,]  4.865526e+08 -369109760  5.033856e+07 -3.523341e+08  1.174416e+08  3.355622e+07
 [4,]  2.967027e+08 -268443756  2.684398e+08 -3.249584e+08  2.825681e+07  2.479161e+03
 [5,]  1.851943e+12  -74178902 -1.501079e+08 -3.703764e+12  1.851996e+12  2.295835e+08
 [6,]  2.825615e+08 -268435510  2.684376e+08 -2.966900e+08  1.412905e+07 -2.856441e+03
 [7,] -3.704025e+12  349674560 -3.373140e+08  7.407639e+12 -3.703661e+12  1.059898e+07
 [8,]  6.861096e+08 -436217465  2.181122e+08 -6.507888e+08 -1.854142e+07  2.348797e+08
 [9,]  1.851632e+12  194264908 -1.501145e+08 -3.703411e+12  1.851685e+12 -3.885154e+07
[10,] -4.944995e+08  268446559 -7.275947e+03  4.521143e+08  4.238336e+07 -2.684369e+08
               [,7]          [,8]          [,9]         [,10]
 [1,]  2.675599e+08  1.497880e-07 -2.843345e+08  5.032854e+07
 [2,]  6.976372e+07  2.684355e+08 -8.653936e+07 -2.181057e+08
 [3,] -1.845568e+08 -1.192093e-07  1.677773e+08  5.033446e+07
 [4,] -2.826220e+07 -2.684355e+08  2.826075e+07  2.684372e+08
 [5,] -1.851946e+12 -1.342177e+08  1.851952e+12 -5.121302e+07
 [6,] -2.825631e+08 -8.385764e-08  2.825624e+08  8.892463e+02
 [7,]  3.703965e+12  4.111875e-04 -3.703674e+12 -2.666709e+08
 [8,] -4.512249e+08 -1.106332e-07  1.995639e+08  2.181072e+08
 [9,] -1.851636e+12  1.342177e+08  1.851641e+12 -5.121569e+07
[10,]  2.260583e+08  8.470367e-08 -2.260559e+08 -2.964275e+03

Awesome! Hopefully through this assignment I was able to demonstrate how to manipulate matrices in R using built-in math functions. We also learned how using the help menu in R and looking online we can solve issues and default limitations R may have.

Comments

Popular posts from this blog

R Final project package: Introducing muMotif

Module 8 : I/O, string manipulation and plyr package

Module 9 : Visualization