gpt4 book ai didi

r - 计算矩阵的逆 : system is computationally singular [error]

转载 作者:行者123 更新时间:2023-12-02 05:28:15 26 4
gpt4 key购买 nike

我有一个矩阵 m :

(m <- matrix(c(26,14,24,14,20,42,24,42,90), 3))

# [,1] [,2] [,3]
# [1,] 26 14 24
# [2,] 14 20 42
# [3,] 24 42 90

当我运行 solve(m) 来计算矩阵的逆时,我收到此错误消息:

solve(m)

Error in solve.default(m) : system is computationally singular: reciprocal condition number = 6.21104e-18

最佳答案

我们可以从几个方面看出这一定是这样的,每一个都暗示着不可逆性:

1) m 的行列式为零:

> det(m)
[1] -2.685852e-12

2) m 具有零特征值,即 eigen(m)$values[3]。等效地,m 的零空间是非空的——它等于 eigen(m)$vectors[, 3]

跨越的一维空间
> e <- eigen(m); e
$values
[1] 1.180000e+02 1.800000e+01 -6.446353e-15

$vectors
[,1] [,2] [,3]
[1,] -0.2881854 9.486833e-01 0.1301889
[2,] -0.4116935 1.110223e-16 -0.9113224
[3,] -0.8645563 -3.162278e-01 0.3905667

> N <- e$vector[, 3] # nullspace
> m %*% N # shows that N is indeed mapped to zero
[,1]
[1,] 5.329071e-15
[2,] 0.000000e+00
[3,] 0.000000e+00

3) m 的列不是线性独立的。特别是在其他列上回归 m[,1] 给出了一个完美的拟合(即拟合值等于 m[, 1])所以从线性模型的系数我们有 7 * m[,2] - 3 * m[, 3] 等于 m[, 1]

> fm <- lm(m[, 1] ~ m[, 2] + m[, 3] + 0)

> all.equal(fitted(fm), m[, 1]) # perfect fit
[1] TRUE

> coef(fm)
m[, 2] m[, 3]
7 -3

> all.equal(7 * m[, 2] - 3 * m[, 3], m[, 1])
[1] TRUE

4) cholesky 分解在其对角线上有一个零:

> chol(m, pivot = TRUE)
[,1] [,2] [,3]
[1,] 9.486833 2.529822 4.4271887
[2,] 0.000000 4.427189 0.6324555
[3,] 0.000000 0.000000 0.0000000
attr(,"pivot")
[1] 3 1 2
attr(,"rank")
[1] 2
Warning message:
In chol.default(m, pivot = TRUE) :
the matrix is either rank-deficient or indefinite

5) m 不是满秩,即秩小于 3:

> attr(chol(m, pivot = TRUE), "rank")
[1] 2
Warning message:
In chol.default(m, pivot = TRUE) :
the matrix is either rank-deficient or indefinite

注意:输入由以下人员重复提供:

m <- matrix(c(26, 14, 24, 14, 20, 42, 24, 42, 90), 3)

关于r - 计算矩阵的逆 : system is computationally singular [error],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34700685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com