qr
QR Matrix Decomposition
Description
Returns a representation of an orthogonal (or unitary) matrix and
a triangular matrix whose product is the input.
Usage
qr(x, ...)
## Default S3 method:
qr(x, tol = 1e-07, LAPACK = FALSE, ...)
## S3 method for class 'lm':
qr(x, ...)
is.qr(x)
Arguments
x |
a matrix or data.frame of numeric or complex data in qr.default().
Missing values are not accepted. Or a "lm" object for function qr.lm(),
any object for function is.qr().
|
tol |
the tolerance.
|
LAPACK |
logical value to indicate if LAPACK library is used for QR matrix decomposition.
|
... |
other optional arguments pass to or from methods.
|
Details
The function qr is generic. Currently, the default method and an invisible
method for class "lm" are implemented.
In default method, when LAPACK = TRUE the QR decomposition is computed
with the LAPACK function DGEQP3.
See http://www.netlib.org/lapack/ for more details.
In qr.lm() method, the qr component of lm object is extracted if it is not null.
Otherwise function is stopped with error message.
Value
qr | returns an object of class "qr" representing
the QR numerical decomposition of the matrix x.
The components of qr are as follows:
|
is.qr | returns TRUE
if its argument x is of class qr; otherwise,
FALSE. |
Background
A QR decomposition of an
m by
n matrix
X
is a
n by
n permutation matrix P, an
m
by
m orthogonal (or unitary) matrix Q, and an
m
by
n upper triangular matrix
R such that:
A restatement of this equation is:
Conj(t(Q)) %*% X %*% P == R
The QR decomposition is an efficient and numerically stable method for
computing least squares.
Differences between Spotfire Enterprise Runtime for R and Open-source R
- In open-source R, LAPACK as.qr always returns an error.
- In open-source R, when x is a matrix of complex data, the QR factorization is always computed
with the LAPACK function ZGEQP3, regardless of the argument LAPACK.
When x is a matrix of non-complex data, and LAPACK is FALSE,
the QR factorization is computed with a particular LINPACK subroutine
DQRDC2 (modified from LINPACK code DQRDC).
When LAPACK is TRUE, the returned qr contains a logical
attribute useLAPACK, set to TRUE.
- The implementation in Spotfire Enterprise Runtime for R has the linear model version of the QR for
complex matrices. Thus using LAPACK = TRUE assigns useLAPCK for
complex matrices, and qrcomplexmatrix may differ from qrcomplex, matrix, LAPACK = TRUE.
- Open-source R ignores the tolerance argument tol when LAPACK = TRUE and rank is
always equal to min(dim(X)) in this case. In Spotfire Enterprise Runtime for R, we have added a rank estimate
based on the tolerance and LAPACK routine dlaic1 (zlaic1).
Note
This is QR decomposition with pivoting: x[, qr$pivot] == Q %*% R.
Unlike regression functions,
qr does not add an intercept term.
Bind on a column of 1s if you want an intercept.
The results of qr.coef, and so on,
reflect an intercept term if a column of 1s is added.
The default pivoting used is trying to preserve the column order and the pivoting
condition is computed in a scale independent way, i.e. all columns are scaled to
length one inside the pivoting code. A column is pivoted (moved last) if the length of
what is left after subtracting the components in the previous columns is small
compared to the given tolerance tol.
References
Thisted, R. A. 1988. Elements of Statistical Computing. New York, NY: Chapman and Hall.
Anderson, E., et al. 1999. LAPACK Users' Guide. Philadelphia, PA: SIAM Publications.
Golub, G. H. and Van Loan, C. F. 1983. Matrix Computations. Baltimore, MD: Johns Hopkins University Press.
See Also
Examples
x <- matrix(runif(10), 5, 2)
q <- qr(x)
is.qr(x) # FALSE
is.qr(q) # TRUE
x <- runif(10)
y <- rnorm(10)
qr(lm( y~x , qr = TRUE) ) # OK
qr(lm( y~x , qr = FALSE) )
# Error: lm object does not have a proper 'qr' component.