Linear algebra calculations

Install required packages

expm, mvtnorm, pracma

Matrix algebra

Transpose

     [,1] [,2]
[1,]   20   29
[2,]   26   27
[3,]   10   20
[4,]   19   12
     [,1] [,2] [,3] [,4]
[1,]   20   26   10   19
[2,]   29   27   20   12

Extracting the diagnoal and creating a diagonal matrix

Extract the diagonal of a given matrix.

[1] 43.58333 59.33333

Create a diagonal matrix based on a vector.

     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    2    0
[3,]    0    0    3
     [,1] [,2]
[1,]    1    0
[2,]    0    1
     [,1]
[1,]    5

Multiplication

      [,1]  [,2]  [,3]  [,4]
[1,]  0.75 -0.25 -0.25 -0.25
[2,] -0.25  0.75 -0.25 -0.25
[3,] -0.25 -0.25  0.75 -0.25
[4,] -0.25 -0.25 -0.25  0.75
      [,1] [,2]
[1,]  1.25    7
[2,]  7.25    5
[3,] -8.75   -2
[4,]  0.25  -10
       [,1] [,2]
[1,] 130.75   60
[2,]  60.00  178
       [,1] [,2]
[1,] 130.75   60
[2,]  60.00  178
         [,1]     [,2]
[1,] 43.58333 20.00000
[2,] 20.00000 59.33333
         [,1]     [,2]
[1,] 43.58333 20.00000
[2,] 20.00000 59.33333
          [,1]      [,2]
[1,] 1.0000000 0.3932968
[2,] 0.3932968 1.0000000
          [,1]      [,2]
[1,] 1.0000000 0.3932968
[2,] 0.3932968 1.0000000
     [,1] [,2]
[1,]   38   59
[2,]   50   55
[3,]   18   41
[4,]   36   25
          [,1]      [,2]
[1,] 0.5101443 0.6307329
[2,] 0.6631876 0.5872341
[3,] 0.2550722 0.4349882
[4,] 0.4846371 0.2609929
          [,1]      [,2]
[1,] 0.5101443 0.6307329
[2,] 0.6631876 0.5872341
[3,] 0.2550722 0.4349882
[4,] 0.4846371 0.2609929

Power

     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    7    8    0
[3,]    7    0    8
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    7    8    0
[3,]    7    0    8

Cross product

[1] -3  6 -3

Solving linear equations and calculating the inverse

Inverse

     [,1] [,2]
[1,]  0.5  0.5
[2,]  0.5 -0.5
     [,1] [,2]
[1,]    1    0
[2,]    0    1

Moore-Penrose generalized inverse

     [,1] [,2]
[1,]    1    0
[2,]    0    1

Solving linear equations

[1] -3.0 -6.4
     [,1]
[1,]    5
[2,]   -3

Norms and distances of matrices and vectors

Norm

        [,1]
[1,] 9.69536
[1] 9.69536
      a1       a2 
 9.69536 18.16590 
      a1       a2 
 9.69536 18.16590 
[1] 20.59126
         [,1]
[1,] 20.59126

Distance

Length of difference vector

         [,1]
[1,] 35.62303
         1        2        3        4
1  0.00000 35.62303 20.80865 30.09983
2 35.62303  0.00000 24.61707 28.58321
3 20.80865 24.61707  0.00000 11.09054
4 30.09983 28.58321 11.09054  0.00000

Mahalanobis-transformation

     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1
[1]  6.994405e-17  8.366571e-17 -2.466777e-17

Mahalanobis-distance

       y1        y2 
 3.192715 12.257083 
         [,1]
[1,] 3.192715
         [,1]
[1,] 12.25708
[1] 0
[1] 74
[1] 1 2 3
         [,1]
[1,] 3.192715
         [,1]
[1,] 12.25708

Trace, determinant, rank, null space, condition index

Trace

     [,1] [,2]
[1,]    9    1
[2,]    1    4
[1] 13
[1] 99
[1] 99
[1] 99

Determinant

[1] 35
[1] TRUE
[1] 24
[1] TRUE

Rank

[1] 2
eigen() decomposition
$values
[1] 9.192582 3.807418

$vectors
           [,1]       [,2]
[1,] -0.9819564  0.1891075
[2,] -0.1891075 -0.9819564
     [,1] [,2]
[1,]    1    0
[2,]    0    1
[1] 13
[1] 35

Null space (kernel)

             [,1] [,2]
[1,] 6.661338e-15    0
[2,] 5.773160e-15    0

Condition index

We need to specify base::norm() here because after attaching package expm above, there is another function present with the same name.

[1] 7.931935
[1] 7.931935
[1] 7.931935
[1] 7.931935 1.502511 1.000000

Matrix decompositions

Eigenvalues and eigenvectors

          [,1]      [,2]      [,3]
[1,]  43.58333  20.00000 -14.00000
[2,]  20.00000  59.33333 -23.33333
[3,] -14.00000 -23.33333  18.66667
          [,1]      [,2]      [,3]
[1,]  43.58333  20.00000 -14.00000
[2,]  20.00000  59.33333 -23.33333
[3,] -14.00000 -23.33333  18.66667

Singular value decomposition

[1] TRUE
[1] TRUE

Cholesky decomposition

[1] TRUE

\(QR\)-decomposition

We need to specify base::qr.Q() here because after attaching package expm above, there is another function present with the same name.

[1] TRUE

Square-root

$B
          [,1]      [,2]      [,3]
[1,]  6.372708  1.294488 -1.138518
[2,]  1.294488  7.328159 -1.988899
[3,] -1.138518 -1.988899  3.662611

$Binv
            [,1]        [,2]       [,3]
[1,]  0.16819129 -0.01820349 0.04239706
[2,] -0.01820349  0.16201802 0.08232174
[3,]  0.04239706  0.08232174 0.33091129

$k
[1] 7

$acc
[1] 2.313559e-10
          [,1]      [,2]      [,3]
[1,]  6.372708  1.294488 -1.138518
[2,]  1.294488  7.328159 -1.988899
[3,] -1.138518 -1.988899  3.662611
          [,1]      [,2]      [,3]
[1,]  43.58333  20.00000 -14.00000
[2,]  20.00000  59.33333 -23.33333
[3,] -14.00000 -23.33333  18.66667

\(X = N N^{t}\)

          [,1]      [,2]      [,3]
[1,]  43.58333  20.00000 -14.00000
[2,]  20.00000  59.33333 -23.33333
[3,] -14.00000 -23.33333  18.66667

Orthogonal projections

Direct implementation of \((X^{t} X)^{-1} X^{t}\)

      [,1] [,2] [,3]
[1,] 18.75   22   23
[2,] 18.75   22   23
[3,] 18.75   22   23
[4,] 18.75   22   23
[1] TRUE
[1] TRUE
     [,1] [,2] [,3]
[1,]   20   29    0
[2,]   26   27    0
[3,]   10   20    0

Numerically stable implementation using the \(QR\)-decomposition

We need to specify base::qr.Q() here because after attaching package expm above, there is another function present with the same name.

[1] TRUE
[1] TRUE

Detach (automatically) loaded packages (if possible)

Get the article source from GitHub

R markdown - markdown - R code - all posts