Calculate the inverse of a non-square matrix in R

I am new to R and trying to figure out how you can calculate an inverse matrix that is not square. (non-square? irregular? I'm not sure of the correct terminology).

From my book and a quick Google search (see source ), I found that you can use the solve(a)inverse matrix to search if it a is square.

The matrix I created is, and from what I understand, it’s not a square:

  > matX<-matrix(c(rep(1, 8),2,3,4,0,6,4,3,7,-2,-4,3,5,7,8,9,11), nrow=8, ncol=3);
  > matX

       [,1] [,2] [,3]
  [1,]    1    2   -2
  [2,]    1    3   -4
  [3,]    1    4    3
  [4,]    1    0    5
  [5,]    1    6    7
  [6,]    1    4    8
  [7,]    1    3    9
  [8,]    1    7   11
  > 

Is there a function to solve a matrix of this size or do I need to do something for each element? as the function solve()gives this error:

  Error in solve.default(matX) : 'a' (8 x 3) must be square

The calculation that I am trying to achieve from the above matrix is ​​as follows: (matX'matX)^-1

Thanks in advance.

+4
2

ginv ginv MASS . , :

> library(MASS)
> inv <- ginv(matX)
>
> # test it out
> inv %*% matX
              [,1]         [,2]          [,3]
[1,]  1.000000e+00 6.661338e-16  4.440892e-15
[2,] -8.326673e-17 1.000000e+00 -1.110223e-15
[3,]  6.938894e-17 8.326673e-17  1.000000e+00

, zapsmall:

> zapsmall(inv %*% matX)
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1

matX'matX :

> tcrossprod(inv)
             [,1]         [,2]         [,3]
[1,]  0.513763935 -0.104219636 -0.002371406
[2,] -0.104219636  0.038700372 -0.007798748
[3,] -0.002371406 -0.007798748  0.006625269

, , matX'matX, . MASS:

> solve(crossprod(matX))
             [,1]         [,2]         [,3]
[1,]  0.513763935 -0.104219636 -0.002371406
[2,] -0.104219636  0.038700372 -0.007798748
[3,] -0.002371406 -0.007798748  0.006625269

svd svd :

> with(svd(matX), v %*% diag(1/d^2) %*% t(v))
             [,1]         [,2]         [,3]
[1,]  0.513763935 -0.104219636 -0.002371406
[2,] -0.104219636  0.038700372 -0.007798748
[3,] -0.002371406 -0.007798748  0.006625269

.

+5

, " Moore-Penrose pseudoverse". exp.mat, . , .

exp.mat():

#The exp.mat function performs can calculate the pseudoinverse of a matrix (EXP=-1)
#and other exponents of matrices, such as square roots (EXP=0.5) or square root of 
#its inverse (EXP=-0.5). 
#The function arguments are a matrix (MAT), an exponent (EXP), and a tolerance
#level for non-zero singular values.
exp.mat<-function(MAT, EXP, tol=NULL){
    MAT <- as.matrix(MAT)
    matdim <- dim(MAT)
    if(is.null(tol)){
        tol=min(1e-7, .Machine$double.eps*max(matdim)*max(MAT))
    }
    if(matdim[1]>=matdim[2]){ 
        svd1 <- svd(MAT)
        keep <- which(svd1$d > tol)
        res <- t(svd1$u[,keep]%*%diag(svd1$d[keep]^EXP, nrow=length(keep))%*%t(svd1$v[,keep]))
    }
    if(matdim[1]<matdim[2]){ 
        svd1 <- svd(t(MAT))
        keep <- which(svd1$d > tol)
        res <- svd1$u[,keep]%*%diag(svd1$d[keep]^EXP, nrow=length(keep))%*%t(svd1$v[,keep])
    }
    return(res)
}

:

source("exp.mat.R")
X <- matrix(c(rep(1, 8),2,3,4,0,6,4,3,7,-2,-4,3,5,7,8,9,11), nrow=8, ncol=3)
iX <- exp.mat(X, -1)
zapsmall(iX %*% X) # results in identity matrix
[,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1
+2

Source: https://habr.com/ru/post/1523779/


All Articles