我正在尝试在 R 中计算任意 N x J 矩阵 S
的投影矩阵 P
:
P = S (S'S) ^ -1 S'
我一直在尝试使用以下函数执行此操作:
P <- function(S){
output <- S %*% solve(t(S) %*% S) %*% t(S)
return(output)
}
但是当我使用这个我得到的错误看起来像这样:
# Error in solve.default(t(S) %*% S, t(S), tol = 1e-07) :
# system is computationally singular: reciprocal condition number = 2.26005e-28
我认为这是数值下溢和/或不稳定性的结果,正如许多地方讨论的那样r-help 和 此处,但我使用 SVD 或 QR 的经验还不够分解以解决问题,或者将现有代码付诸实践。我也尝试过建议的代码,即将解决方案编写为系统:
output <- S %*% solve (t(S) %*% S, t(S), tol=1e-7)
但它仍然不起作用。任何建议将不胜感激。
我非常确定我的矩阵应该是可逆的并且不具有任何共线性,如果只是因为我尝试使用正交虚拟变量矩阵来测试它,但它仍然不起作用。
另外,我想将其应用于相当大的矩阵,所以我正在寻找一个简洁的通用解决方案。
I'm trying to calculate in R a projection matrix P
of an arbitrary N x J matrix S
:
P = S (S'S) ^ -1 S'
I've been trying to perform this with the following function:
P <- function(S){
output <- S %*% solve(t(S) %*% S) %*% t(S)
return(output)
}
But when I use this I get errors that look like this:
# Error in solve.default(t(S) %*% S, t(S), tol = 1e-07) :
# system is computationally singular: reciprocal condition number = 2.26005e-28
I think that this is a result of numerical underflow and/or instability as discussed in numerous places like r-help and here, but I'm not experienced enough using SVD or QR decomposition to fix the problem, or else put this existing code into action. I've also tried the suggested code, which is to write solve as a system:
output <- S %*% solve (t(S) %*% S, t(S), tol=1e-7)
But still it doesn't work. Any suggestions would be appreciated.
I'm pretty sure that my matrix should be invertible and does not have any co-linearities, if only because I have tried testing this with a matrix of orthogonal dummy variables, and it still doesn't work.
Also, I'd like to apply this to fairly large matrices, so I'm looking for a neat general solution.
发布评论
评论(1)
虽然OP已经一年多没有活跃了,但我仍然决定发布一个答案。我会使用
X
而不是S
,因为在统计中,我们经常需要线性回归上下文中的投影矩阵,其中X
是模型矩阵,y
是响应向量,而H = X(X'X)^{-1}X'
是帽子/投影矩阵,因此Hy
给出预测值。这个答案假设了普通最小二乘的背景。对于加权最小二乘,请参阅从 QR 分解获取帽子矩阵以进行加权最小二乘回归。
概述
求解
基于一般方阵的 LU 分解。对于X'X
(应由 R 中的crossprod(X)
而不是t(X) %*% X
计算,请阅读?crossprod
了解更多)这是对称的,我们可以使用基于 Choleksy 分解的chol2inv
。然而,三角分解不如 QR 分解稳定。这并不难理解。如果
X
具有条件数kappa
,则X'X
将具有条件数kappa ^ 2
。这可能会导致很大的数值困难。您收到的错误消息:只是告诉我们这一点。
kappa ^ 2
约为e-28
,远小于e-16
左右的机器精度。在容差tol = .Machine$double.eps
的情况下,X'X
将被视为秩不足,因此 LU 和 Cholesky 分解将失败。通常,在这种情况下我们会改用 SVD 或 QR,但旋转 Cholesky 分解是另一种选择。
下面我将解释这三种方法。
使用 QR 分解
请注意,投影矩阵是排列无关的,即,我们是否执行带有或不带有旋转的 QR 分解并不重要。
在 R 中,
qr.default
可以调用 LINPACK 例程DQRDC
进行非透视 QR 分解,并调用 LAPACK 例程DGEQP3
进行块透视 QR 分解。让我们生成一个玩具矩阵并测试这两个选项:请注意,两个对象的
$pivot
是不同的。现在,我们定义一个包装函数来计算
QQ'
:我们将看到
qr_linpack
和qr_lapack
给出相同的投影矩阵:使用奇异值分解
在 R 中,
svd
计算奇异值分解。我们仍然使用上面的示例矩阵 X:我们再次得到相同的投影矩阵。
使用枢轴 Cholesky 分解
为了演示,我们仍然使用上面的示例
X
。我们再次得到相同的投影矩阵。
Although OP has not been active for more than a year, I still decide to post an answer. I would use
X
instead ofS
, as in statistics, we often want projection matrix in linear regression context, whereX
is the model matrix,y
is the response vector, whileH = X(X'X)^{-1}X'
is hat / projection matrix so thatHy
gives predictive values.This answer assumes the context of ordinary least squares. For weighted least squares, see Get hat matrix from QR decomposition for weighted least square regression.
An overview
solve
is based on LU factorization of a general square matrix. ForX'X
(should be computed bycrossprod(X)
rather thant(X) %*% X
in R, read?crossprod
for more) which is symmetric, we can usechol2inv
which is based on Choleksy factorization.However, triangular factorization is less stable than
QR
factorization. This is not hard to understand. IfX
has conditional numberkappa
,X'X
will have conditional numberkappa ^ 2
. This can cause big numerical difficulty. The error message you get:is just telling this.
kappa ^ 2
is aboute-28
, much much smaller than machine precision at arounde-16
. With tolerancetol = .Machine$double.eps
,X'X
will be seen as rank deficient, thus LU and Cholesky factorization will break down.Generally, we switch to SVD or QR in this situation, but pivoted Cholesky factorization is another choice.
In the following, I will explain all three methods.
Using QR factorization
Note that the projection matrix is permutation independent, i.e., it does not matter whether we perform QR factorization with or without pivoting.
In R,
qr.default
can call LINPACK routineDQRDC
for non-pivoted QR factorization, and LAPACK routineDGEQP3
for block pivoted QR factorization. Let's generate a toy matrix and test both options:Note the
$pivot
is different for two objects.Now, we define a wrapper function to compute
QQ'
:We will see that
qr_linpack
andqr_lapack
give the same projection matrix:Using singular value decomposition
In R,
svd
computes singular value decomposition. We still use the above example matrixX
:Again, we get the same projection matrix.
Using Pivoted Cholesky factorization
For demonstration, we still use the example
X
above.Again, we get the same projection matrix.