QR Matrix Decomposition

DESCRIPTION:
Returns a representation of an orthogonal (or unitary) matrix and a triangular matrix whose product is the input. The function qr has been made generic. In particular, there is a qr method in the Matrix library that differs (both in argument list, as well as in returned value) from the S-PLUS default.

USAGE:
qr(x, ...)
qr.default(x, tol = 1e-7)
is.qr(o)
as.qr(o)

REQUIRED ARGUMENTS:
x:
matrix of numeric or complex data. Missing values are not accepted.

o:
any S-PLUS object.

OPTIONAL ARGUMENTS:
tol:
tolerance for detecting linear dependencies among columns of x (see rank below).

VALUE:
qr returns a list representing the QR numerical decomposition of the matrix x. The components of this list are as follows:
qr:
a matrix the same size as x. The upper triangle (including the diagonal) is the R matrix. The lower part contains most of a compact representation of the Q matrix.
qraux:
a vector of length ncol(x) containing part of the compact representation of the Q matrix.
rank:
the rank of x as computed by the decomposition. (If you really want to estimate the rank, the function svd might be preferable.)
pivot:
the pivoting of columns of x used to produce the decomposition. This is used to adjust the dimnames attribute.

is.qr returns TRUE if its argument has qr and qraux components, FALSE otherwise.

as.qr returns its argument if is.qr(x) is TRUE, and qr(x) otherwise.


NOTE:
Unlike regression functions, qr does not add an intercept term. Bind on a column of 1s if you want an intercept. However, the results of qr.coef, etc., will reflect an intercept term, if used, when they take the decomposition as computed by a regression function.

DETAILS:
If umat is created from the qr component of the output by putting zeros above the diagonal and qraux along the diagonal, then the Q matrix is equal to H1 %*% H2 %*% ...%*% Hk where Hj is diag(nrow(qr)) - outer(umat[,j], umat[,j])/qraux[j]. See the Linpack Users' Guide (pages 9.13-9.16) for more details.

The method used is the Householder successive reflection procedure, adapted from the implementation used by the LINPACK library. The decomposition is performed in double precision arithmetic.


BACKGROUND:
A QR decomposition of an n by p matrix X is an n by n orthogonal (or unitary) matrix Q and an n by p upper triangular matrix R such that X == Q %*% R. A restatement of this equation is: Conj(t(Q)) %*% X == R. The QR decomposition is an efficient and numerically stable method for computing least squares.

REFERENCES:
Dongarra, J. J., Bunch, J. R., Moler, C. B. and Stewart, G. W. (1979). LINPACK Users' Guide. SIAM, Philadelphia.

Golub, G. H. and Van Loan, C. F. (1983). Matrix Computations. Johns Hopkins University Press, Baltimore.

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


SEE ALSO:
qr.coef , qr.R , qr.X , eigen , svd , chol , backsolve , solve , lsfit , Matrix , library(help = Matrix).

EXAMPLES:
q <- qr(state.x77)

# below is a logical but inefficient function to get an explicit Q and R # it is presented to explain the output of qr rather than to be used per se # (the functions qr.Q and qr.R do this efficiently) function(x) { qrl <- qr(x) r <- qrl$qr r[row(r) > col(r)] <- 0 u <- qrl$qr u[row(u) < col(u)] <- 0 u[row(u) == col(u)] <- qrl$qraux q <- diag(nrow(u)) for(j in 1:ncol(u)) { h <- diag(nrow(u)) - outer(u[, j], u[, j])/qrl$qraux[j] q <- q %*% h } list(q,r) }