qr {base} | R Documentation |
The QR Decomposition of a Matrix
Description
qr
computes the QR decomposition of a matrix.
Usage
qr(x, ...)
## Default S3 method:
qr(x, tol = 1e-07 , LAPACK = FALSE, ...)
qr.coef(qr, y)
qr.qy(qr, y)
qr.qty(qr, y)
qr.resid(qr, y)
qr.fitted(qr, y, k = qr$rank)
qr.solve(a, b, tol = 1e-7)
## S3 method for class 'qr'
solve(a, b, ...)
is.qr(x)
as.qr(x)
Arguments
x |
a numeric or complex matrix whose QR decomposition is to be computed. Logical matrices are coerced to numeric. |
tol |
the tolerance for detecting linear dependencies in the
columns of |
qr |
a QR decomposition of the type computed by |
y , b |
a vector or matrix of right-hand sides of equations. |
a |
a QR decomposition or ( |
k |
effective rank. |
LAPACK |
logical. For real |
... |
further arguments passed to or from other methods. |
Details
The QR decomposition plays an important role in many
statistical techniques. In particular it can be used to solve the
equation \bold{Ax} = \bold{b}
for given matrix \bold{A}
,
and vector \bold{b}
. It is useful for computing regression
coefficients and in applying the Newton-Raphson algorithm.
The functions qr.coef
, qr.resid
, and qr.fitted
return the coefficients, residuals and fitted values obtained when
fitting y
to the matrix with QR decomposition qr
.
(If pivoting is used, some of the coefficients will be NA
.)
qr.qy
and qr.qty
return Q %*% y
and
t(Q) %*% y
, where Q
is the (complete) \bold{Q}
matrix.
All the above functions keep dimnames
(and names
) of
x
and y
if there are any.
solve.qr
is the method for solve
for qr
objects.
qr.solve
solves systems of equations via the QR decomposition:
if a
is a QR decomposition it is the same as solve.qr
,
but if a
is a rectangular matrix the QR decomposition is
computed first. Either will handle over- and under-determined
systems, providing a least-squares fit if appropriate.
is.qr
returns TRUE
if x
is a list
and inherits
from "qr"
.
It is not possible to coerce objects to mode "qr"
. Objects
either are QR decompositions or they are not.
The LINPACK interface is restricted to matrices x
with less
than 2^{31}
elements.
qr.fitted
and qr.resid
only support the LINPACK interface.
Unsuccessful results from the underlying LAPACK code will result in an error giving a positive error code: these can only be interpreted by detailed study of the FORTRAN code.
Value
The QR decomposition of the matrix as computed by LINPACK(*) or LAPACK. The components in the returned value correspond directly to the values returned by DQRDC(2)/DGEQP3/ZGEQP3.
qr |
a matrix with the same dimensions as |
qraux |
a vector of length |
rank |
the rank of |
pivot |
information on the pivoting strategy used during the decomposition. |
Non-complex QR objects computed by LAPACK have the attribute
"useLAPACK"
with value TRUE
.
*)
dqrdc2
instead of LINPACK's DQRDC
In the (default) LINPACK case (LAPACK = FALSE
), qr()
uses a modified version of LINPACK's DQRDC, called
‘dqrdc2
’. It differs by using the tolerance tol
for a pivoting strategy which moves columns with near-zero 2-norm to
the right-hand edge of the x matrix. This strategy means that
sequential one degree-of-freedom effects can be computed in a natural
way.
Note
To compute the determinant of a matrix (do you really need it?),
the QR decomposition is much more efficient than using eigenvalues
(eigen
). See det
.
Using LAPACK (including in the complex case) uses column pivoting and does not attempt to detect rank-deficient matrices.
Source
For qr
, the LINPACK routine DQRDC
(but modified to
dqrdc2
(*)) and the LAPACK
routines DGEQP3
and ZGEQP3
. Further LINPACK and LAPACK
routines are used for qr.coef
, qr.qy
and qr.qty
.
LAPACK and LINPACK are from https://netlib.org/lapack/ and https://netlib.org/linpack/ and their guides are listed in the references.
References
Anderson E, Bai Z, Bischof C, Blackford S, Demmel J, Dongarra J, Du Croz J, Greenbaum A, Hammerling S, McKenney A, Sorensen D (1999). LAPACK Users' Guide, series Software, Environments, and Tools, Third edition. Society for Industrial and Applied Mathematics, Philadelphia, PA. ISBN 9780898714470. doi:10.1137/1.9781611971811. https://netlib.org/lapack/lug/lapack_lug.html.
Becker RA, Chambers JM, Wilks AR (1988). The New S Language. Chapman and Hall/CRC, London.
Dongarra J, Bunch J, Moler C, Stewart G (1979). LINPACK Users' Guide, series Other Titles in Applied Mathematics. Society for Industrial and Applied Mathematics. ISBN 9780898711721. doi:10.1137/1.9780898719604.
See Also
qr.Q
, qr.R
, qr.X
for
reconstruction of the matrices.
lm.fit
, lsfit
,
eigen
, svd
.
det
(using qr
) to compute the determinant of a matrix.
Examples
hilbert <- function(n) { i <- 1:n; 1 / outer(i - 1, i, `+`) }
h9 <- hilbert(9); h9
qr(h9)$rank #--> only 7
qrh9 <- qr(h9, tol = 1e-10)
qrh9$rank #--> 9
##-- Solve linear equation system H %*% x = y :
y <- 1:9/10
x <- qr.solve(h9, y, tol = 1e-10) # or equivalently :
x <- qr.coef(qrh9, y) #-- is == but much better than
#-- solve(h9) %*% y
h9 %*% x # = y
## overdetermined system
A <- matrix(runif(12), 4)
b <- 1:4
qr.solve(A, b) # or solve(qr(A), b)
solve(qr(A, LAPACK = TRUE), b)
# this is a least-squares solution, cf. lm(b ~ 0 + A)
## underdetermined system
A <- matrix(runif(12), 3)
b <- 1:3
qr.solve(A, b)
solve(qr(A, LAPACK = TRUE), b)
# solutions will have one zero, not necessarily the same one