psolve: Pseudo-Solve a System of Equations

View source: R/psolve.R

psolveR Documentation

Pseudo-Solve a System of Equations

Description

This generic function solves the equation a %*% x = b for x, where b can be either a vector or a matrix. This implementation is similar to solve, but uses a pseudo-inverse if the system is computationally singular.

Usage

psolve(a, b, tol)

Arguments

a

a rectangular numeric matrix containing the coefficients of the linear system.

b

a numeric vector or matrix giving the right-hand side(s) of the linear system. If missing, b is taken to be an identity matrix and solve will return the (pseudo-)inverse of a.

tol

the tolerance for detecting linear dependencies in the columns of a. The default is .Machine$double.eps.

Details

If a is a symmetric matrix, eigen is used to compute the (pseudo-)inverse. This assumes that a is a positive semi-definite matrix. Otherwise svd is used to compute the (pseudo-)inverse for rectangular matrices.

Value

If b is missing, returns the (pseudo-)inverse of a. Otherwise returns psolve(a) %*% b.

Note

The pseudo-inverse is calculated by inverting the eigen/singular values that are greater than the first value multiplied by tol * min(dim(a)).

Author(s)

Nathaniel E. Helwig <helwig@umn.edu>

References

Moore, E. H. (1920). On the reciprocal of the general algebraic matrix. Bulletin of the American Mathematical Society, 26, 394-395. doi: 10.1090/S0002-9904-1920-03322-7

Penrose, R. (1955). A generalized inverse for matrices. Mathematical Proceedings of the Cambridge Philosophical Society, 51(3), 406-413. doi: 10.1017/S0305004100030401

See Also

msqrt

Examples

# generate X
set.seed(0)
X <- matrix(rnorm(100), 20, 5)
X <- cbind(X, rowSums(X))

# pseudo-inverse of X  (dim = 6 by 20)
Xinv <- psolve(X)

# pseudo-inverse of crossprod(X)  (dim = 6 by 6)
XtXinv <- psolve(crossprod(X))


npreg documentation built on July 21, 2022, 1:06 a.m.

Related to psolve in npreg...