qr
is generic.
qr(x, ...) qr.default(x, tol=1e-7) is.qr(o) as.qr(o)
x
(see
rank
below).
qr
returns a list representing
the QR numerical decomposition of the matrix
x
.
The components of this list are as follows:
x
.
The upper triangle (including the diagonal) is the R matrix.
The lower part contains most of a compact representation of the Q matrix.
ncol(x)
containing part of the compact representation of the Q matrix.
x
as computed by the decomposition.
(If you really want to estimate the rank,
the function
svd
might be preferable.)
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.
Unlike regression functions,
qr
does not add an intercept term.
Bind on a column of
1
s 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.
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.
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.
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.
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) }