| qr | R Documentation |
qr computes the QR decomposition of a matrix.
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)
x | a numeric or complex matrix whose QR decomposition is to becomputed. Logical matrices are coerced to numeric. |
tol | the tolerance for detecting linear dependencies in thecolumns 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 |
The QR decomposition plays an important role in manystatistical techniques. In particular it can be used to solve theequation\bold{Ax} = \bold{b} for given matrix\bold{A},and vector\bold{b}. It is useful for computing regressioncoefficients and in applying the Newton-Raphson algorithm.
The functionsqr.coef,qr.resid, andqr.fittedreturn the coefficients, residuals and fitted values obtained whenfittingy to the matrix with QR decompositionqr.(If pivoting is used, some of the coefficients will beNA.)qr.qy andqr.qty returnQ %*% y andt(Q) %*% y, whereQ is the (complete)\bold{Q} matrix.
All the above functions keepdimnames (andnames) ofx andy if there are any.
solve.qr is the method forsolve forqr objects.qr.solve solves systems of equations via the QR decomposition:ifa is a QR decomposition it is the same assolve.qr,but ifa is a rectangular matrix the QR decomposition iscomputed first. Either will handle over- and under-determinedsystems, providing a least-squares fit if appropriate.
is.qr returnsTRUE ifx is alistandinherits from"qr".
It is not possible to coerce objects to mode"qr". Objectseither are QR decompositions or they are not.
The LINPACK interface is restricted to matricesx with lessthan2^31 elements.
qr.fitted andqr.resid only support the LINPACK interface.
Unsuccessful results from the underlying LAPACK code will result in anerror giving a positive error code: these can only be interpreted bydetailed study of the FORTRAN code.
The QR decomposition of the matrix as computed by LINPACK(*) or LAPACK.The components in the returned value correspond directlyto 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 duringthe decomposition. |
Non-complex QR objects computed by LAPACK have the attribute"useLAPACK" with valueTRUE.
*)dqrdc2 instead of LINPACK's DQRDCIn the (default) LINPACK case (LAPACK = FALSE),qr()uses amodified version of LINPACK's DQRDC, called‘dqrdc2’. It differs by using the tolerancetolfor a pivoting strategy which moves columns with near-zero 2-norm tothe right-hand edge of the x matrix. This strategy means thatsequential one degree-of-freedom effects can be computed in a naturalway.
To compute the determinant of a matrix (do youreally need it?),the QR decomposition is much more efficient than using Eigen values(eigen). Seedet.
Using LAPACK (including in the complex case) uses column pivoting anddoes not attempt to detect rank-deficient matrices.
Forqr, the LINPACK routineDQRDC (but modified todqrdc2(*)) and the LAPACKroutinesDGEQP3 andZGEQP3. Further LINPACK and LAPACKroutines are used forqr.coef,qr.qy andqr.aty.
LAPACK and LINPACK are fromhttps://www.netlib.org/lapack/ andhttps://www.netlib.org/linpack/ and their guides are listedin the references.
Anderson. E. and ten others (1999)LAPACK Users' Guide. Third Edition. SIAM.
Available on-line athttps://www.netlib.org/lapack/lug/lapack_lug.html.
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 forreconstruction of the matrices.lm.fit,lsfit,eigen,svd.
det (usingqr) to compute the determinant of a matrix.
hilbert <- function(n) { i <- 1:n; 1 / outer(i - 1, i, "+") }h9 <- hilbert(9); h9qr(h9)$rank #--> only 7qrh9 <- qr(h9, tol = 1e-10)qrh9$rank #--> 9##-- Solve linear equation system H %*% x = y :y <- 1:9/10x <- qr.solve(h9, y, tol = 1e-10) # or equivalently :x <- qr.coef(qrh9, y) #-- is == but much better than #-- solve(h9) %*% yh9 %*% x # = y## overdetermined systemA <- matrix(runif(12), 4)b <- 1:4qr.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 systemA <- matrix(runif(12), 3)b <- 1:3qr.solve(A, b)solve(qr(A, LAPACK = TRUE), b)# solutions will have one zero, not necessarily the same oneAdd the following code to your website.
For more information on customizing the embed code, readEmbedding Snippets.