使用qr()对矩阵QR分解
QR分解法
是三种将 矩阵分解
的方式之一。这种方式,把 矩阵
分解成一个 半正交矩阵
与一个 上三角矩阵
的积。QR分解经常用来解 线性最小二乘法
问题。QR分解也是特定 特征值算法
即 QR算法
的基础。
A为m×n矩阵可以进行QR分解,A=QR,其中:Q'Q=I,在R中可以用函数qr()进行QR分解,例如:
> A=matrix(1:16,4,4)
> qr(A)
$qr
[,1][,2][,3][,4]
[1,] -5.4772256 -12.7801930 -2.008316e+01 -2.738613e+01
[2,] 0.3651484 -3.2659863 -6.531973e+00 -9.797959e+00
[3,] 0.5477226 -0.3781696 2.641083e-15 2.056562e-15
[4,] 0.7302967 -0.9124744 8.583032e-01 -2.111449e-16$rank
[1] 2$qraux
[1] 1.182574e+00 1.156135e+00 1.513143e+00 2.111449e-16$pivot
[1] 1 2 3 4attr(,"class")
[1] "qr"
rank项返回矩阵的秩,qr项包含了矩阵Q和R的信息,要得到矩阵Q和R,可以用函数qr.Q()和qr.R()作用qr()的返回结果, 例如:
> qr.R(qr(A))
[,1][,2][,3][,4]
[1,] -5.477226 -12.780193 -2.008316e+01 -2.738613e+01
[2,] 0.000000 -3.265986 -6.531973e+00 -9.797959e+00
[3,] 0.0000000.000000 2.641083e-15 2.056562e-15
[4,] 0.0000000.000000 0.000000e+00 -2.111449e-16> qr.Q(qr(A))
[,1][,2][,3][,4]
[1,] -0.1825742 -8.164966e-01 -0.4000874 -0.37407225
[2,] -0.3651484 -4.082483e-01 0.2546329 0.79697056
[3,] -0.5477226 -8.131516e-19 0.6909965 -0.47172438
[4,] -0.7302967 4.082483e-01 -0.5455419 0.04882607
验证 :A=QR
> qr.Q(qr(A))%*%qr.R(qr(A))
[,1] [,2] [,3] [,4]
[1,]15913
[2,]261014
[3,]371115
[4,]481216
验证 Q'Q=I
> t(qr.Q(qr(A)))%*%qr.Q(qr(A)) [,1][,2][,3][,4] [1,] 1.000000e+00 -1.457168e-16 -6.760001e-17 -7.659550e-17 [2,] -1.457168e-16 1.000000e+00 -4.269046e-17 7.011739e-17 [3,] -6.760001e-17 -4.269046e-17 1.000000e+00 -1.596437e-16 [4,] -7.659550e-17 7.011739e-17 -1.596437e-16 1.000000e+00 > round(t(qr.Q(qr(A)))%*%qr.Q(qr(A))) [,1] [,2] [,3] [,4] [1,]1000 [2,]0100 [3,]0010 [4,]0001
> t(qr.Q(qr(A)))%*%qr.Q(qr(A))
[,1][,2][,3][,4]
[1,] 1.000000e+00 -1.457168e-16 -6.760001e-17 -7.659550e-17
[2,] -1.457168e-16 1.000000e+00 -4.269046e-17 7.011739e-17
[3,] -6.760001e-17 -4.269046e-17 1.000000e+00 -1.596437e-16
[4,] -7.659550e-17 7.011739e-17 -1.596437e-16 1.000000e+00
> round(t(qr.Q(qr(A)))%*%qr.Q(qr(A)))
[,1] [,2] [,3] [,4]
[1,]1000
[2,]0100
[3,]0010
[4,]0001
qr.X(qr(A))也可以得到A
> qr.X(qr(A))
[,1] [,2] [,3] [,4]
[1,]15913
[2,]261014
[3,]371115
[4,]481216
[参考] 1.http://zh.wikipedia.org/zh-cn/QR%E5%88%86%E8%A7%A3 2. > help(qr)
qrpackage:baseR DocumentationThe QR Decomposition of a MatrixDescription:‘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 ‘x’. Only used if ‘LAPACK’ is false and ‘x’ is
real.qr: a QR decomposition of the type computed by ‘qr’.y, b: a vector or matrix of right-hand sides of equations.a: a QR decomposition or (‘qr.solve’ only) a rectangular matrix.k: effective rank.LAPACK: logical.For real ‘x’, if true use LAPACK otherwise use
LINPACK (the default)....: further arguments passed to or from other methodsDetails:The QR decomposition plays an important role in many statistical
techniques.In particular it can be used to solve the equation Ax
= b for given matrix A, and vector 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) 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.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/DGEQP3/ZGEQP3.qr: a matrix with the same dimensions as ‘x’.The upper triangle
contains the R of the decomposition and the lower triangle
contains information on the Q of the decomposition (stored in
compact form).Note that the storage used by DQRDC and
DGEQP3 differs.qraux: a vector of length ‘ncol(x)’ which contains additional
information on Q.rank: the rank of ‘x’ as computed by the decomposition: always full
rank in the LAPACK case.pivot: information on the pivoting strategy used during the
decomposition.
Non-complex QR objects computed by LAPACK have the attribute
‘"useLAPACK"’ with value ‘TRUE’.Note:To compute the determinant of a matrix (do you _really_ need it?),
the QR decomposition is much more efficient than using Eigen
values (‘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’ 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
and and their guides are
listed in the references.References:Anderson. E. and ten others (1999) _LAPACK Users' Guide_. Third
Edition. SIAM.
Available on-line at .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.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 systemH %*% 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
推荐阅读
- 布丽吉特,人生绝对的赢家
- 进必趋|进必趋 退必迟,问起对 视勿移
- 对称加密和非对称加密的区别
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- 对抗抑郁最好的方法
- 装聋作哑,关系融洽