Generalized Inverse of a Matrix

DESCRIPTION:

Returns the Moore-Penrose generalized inverse of a matrix.

USAGE:

ginverse(x, tol=sqrt(.Machine$double.eps))

REQUIRED ARGUMENTS:

x
matrix, or vector (which is treated as a matrix with one column).

OPTIONAL ARGUMENTS:

tol
singular values of x less than this tolerance times the largest singular value are considered numerically equivalent to zero.

VALUE:

matrix g such that xgx = x, gxg = g, (xg)' = xg, and (gx)' = gx, where ' indicates transpose in the real case and conjugate transpose in the complex case.

The result has an attribute "rank" that gives the rank of the result (which is the same as the estimated rank of x).

BACKGROUND:

The Moore-Penrose generalized inverse may be used to find a solution to a system of consistent linear equations possibly of less than full rank, or a least squares solution to an overdetermined system, not necessarily of maximum rank. Where the solution is not unique the one with minimum Euclidean norm is selected.

This function is intended for generalized inverses, and is overkill (and slow) for doing routine matrix inversion; solve is quicker for obtaining the inverse of a square full-rank matrix.

Better yet, matrix inverses can usually be avoided, e.g. using solve(x,b) instead of solve(x) %*% b and qr.coef(qr(x), y) instead of ginverse(t(x) %*% x) %*% t(x) %*% y or ginverse(x) %*% y.

REFERENCES:

Dodge, Y. (1985). Analysis of Experiments With Missing Data. Wiley, New York.

Pringle, R. M. and Rayner, A. A. (1971). Generalized Inverse Matrices with Applications to Statistics. Griffin.

Rao, C. R. and Mitra, S. K. (1971) Generalized Inverse of Matrices and its Applications. Wiley, New York.

Thisted, R. (1988). Elements of Statistical Computing. Chapman and Hall, New York.

SEE ALSO:

, .

EXAMPLES:

x <- cbind(1, matrix(rnorm(40),10), 1)
solve(x)           # fails, due to singular matrix
g <- ginverse(x)
round(g %*% x, 3)
attr(g,"rank")
y <- rnorm(20)
x <- rmvnorm(20, d=4)
coef(lm(y ~ x - 1))  # linear regression without an intercept
qr.coef(qr(x), y)      # same
ginverse(x) %*% y       # same