| qrSolve {gnm} | R Documentation |
This function solves the linear system Ax = b, producing
the minimum-length solution if A is not of full rank.
If b is not specified, the Moore-Penrose generalized inverse
(pseudo-inverse) is returned.
The method involves two applications of QR decomposition.
qrSolve(A, b, rank = NULL, ...)
A |
A numeric matrix (m by n, say) |
b |
A numeric vector of length m, or a matrix with m rows |
rank |
Numeric: the algebraic rank of A, if known. |
... |
Other possible arguments to be passed to qr. |
If A is square, the solution will be equivalent to (but should
be faster than) MPinv(A) %*% b.
No check is made on the validity of a specified rank
argument. If rank is left unspecified, the rank of
A is determined numerically by qr.
If the wrong rank is specified, failure (with an unpredictable
error message) is almost certain.
If b is a vector, the result is a vector of length n. If
b is a matrix, the result is a matrix with n rows and the
same number of columns as b.
In either case, the "rank" attribute of the result contains
either the rank argument if specified, or the algebraic rank
as calculated internally by qr(A, ...)$rank.
David Firth
Lecture notes by James P Reilly, at http://www.ece.mcmaster.ca/~reilly/ee731/ch10.ps
set.seed(1) x <- rnorm(3) z <- rnorm(3) xz <- x + z X <- cbind(x, z, xz) XX <- crossprod(X) # has rank 2 MPinv(XX, method = "chol") qrSolve(XX) b <- rnorm(3) crossprod(MPinv(XX), b) qrSolve(XX, b) crossprod(XX, qrSolve(XX, b)) fitted(lm(b ~ -1 + XX))