
Last chance! 50% off unlimited learning
Sale ends in
qr
computes the QR decomposition of a matrix.
qr(x, ...)
"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)
"solve"(a, b, ...)
is.qr(x)
as.qr(x)
x
. Only used if LAPACK
is false and
x
is real.qr
.qr.solve
only) a rectangular matrix.x
, if true use LAPACK
otherwise use LINPACK (the default).x
.
The upper triangle contains the $\bold{R}$ of the decomposition
and the lower triangle contains information on the $\bold{Q}$ of
the decomposition (stored in compact form). Note that the storage
used by DQRDC and DGEQP3 differs.ncol(x)
which contains
additional information on $\bold{Q}$.x
as computed by the decomposition:
always full rank in the LAPACK case."useLAPACK"
with value TRUE
.
qr
, the LINPACK routine DQRDC
and the LAPACK
routines DGEQP3
and ZGEQP3
. Further LINPACK and LAPACK
routines are used for qr.coef
, qr.qy
and qr.aty
. LAPACK and LINPACK are from http://www.netlib.org/lapack and
http://www.netlib.org/linpack and their guides are listed
in the references. 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
with components named qr
, rank
and qraux
and
FALSE
otherwise.
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.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Dongarra, J. J., Bunch, J. R., Moler, C. B. and Stewart, G. W. (1978) LINPACK Users Guide. Philadelphia: SIAM Publications.
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.
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
Run the code above in your browser using DataLab