当协方差矩阵为零时,如何在 R 中使用 princomp () 函数?

发布于 2024-12-22 07:24:53 字数 267 浏览 1 评论 0原文

在 R 中使用princomp()函数时,遇到以下错误:“协方差矩阵不是非负定的”

我认为,这是由于协方差矩阵中的某些值为零(实际上接近于零,但在舍入期间变为零)。

当协方差矩阵包含零时,是否有解决方法可以继续进行 PCA?

[仅供参考:获取协方差矩阵是 princomp() 调用中的中间步骤。可以从此处下载重现此错误的数据文件 - http://tinyurl.com/6rtxrc3]

While using princomp() function in R, the following error is encountered : "covariance matrix is not non-negative definite".

I think, this is due to some values being zero (actually close to zero, but becomes zero during rounding) in the covariance matrix.

Is there a work around to proceed with PCA when covariance matrix contains zeros ?

[FYI : obtaining the covariance matrix is an intermediate step within the princomp() call. Data file to reproduce this error can be downloaded from here - http://tinyurl.com/6rtxrc3]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2024-12-29 07:24:53

第一个策略可能是减少容忍争论。在我看来,princomp 不会传递容差参数,但 prcomp 确实接受“tol”参数。如果无效,这应该识别协方差几乎为零的向量:

nr0=0.001
which(abs(cov(M)) < nr0, arr.ind=TRUE)

这将识别具有负特征值的向量:

which(eigen(M)$values < 0)

使用帮助(qr)页面上的 h9 示例:

> which(abs(cov(h9)) < .001, arr.ind=TRUE)
      row col
 [1,]   9   4
 [2,]   8   5
 [3,]   9   5
 [4,]   7   6
 [5,]   8   6
 [6,]   9   6
 [7,]   6   7
 [8,]   7   7
 [9,]   8   7
[10,]   9   7
[11,]   5   8
[12,]   6   8
[13,]   7   8
[14,]   8   8
[15,]   9   8
[16,]   4   9
[17,]   5   9
[18,]   6   9
[19,]   7   9
[20,]   8   9
[21,]   9   9
> qr(h9[-9,-9])$rank  
[1] 7                  # rank deficient, at least at the default tolerance
> qr(h9[-(8:9),-(8:9)])$ take out only the vector  with the most dependencies
[1] 6                   #Still rank deficient
> qr(h9[-(7:9),-(7:9)])$rank
[1] 6

另一种方法可能是使用别名 功能:

alias( lm( rnorm(NROW(dfrm)) ~ dfrm) )

The first strategy might be to decrease the tolerance argument. Looks to me that princomp won't pass on a tolerance argument but that prcomp does accept a 'tol' argument. If not effective, this should identify vectors which have nearly-zero covariance:

nr0=0.001
which(abs(cov(M)) < nr0, arr.ind=TRUE)

And this would identify vectors with negative eigenvalues:

which(eigen(M)$values < 0)

Using the h9 example on the help(qr) page:

> which(abs(cov(h9)) < .001, arr.ind=TRUE)
      row col
 [1,]   9   4
 [2,]   8   5
 [3,]   9   5
 [4,]   7   6
 [5,]   8   6
 [6,]   9   6
 [7,]   6   7
 [8,]   7   7
 [9,]   8   7
[10,]   9   7
[11,]   5   8
[12,]   6   8
[13,]   7   8
[14,]   8   8
[15,]   9   8
[16,]   4   9
[17,]   5   9
[18,]   6   9
[19,]   7   9
[20,]   8   9
[21,]   9   9
> qr(h9[-9,-9])$rank  
[1] 7                  # rank deficient, at least at the default tolerance
> qr(h9[-(8:9),-(8:9)])$ take out only the vector  with the most dependencies
[1] 6                   #Still rank deficient
> qr(h9[-(7:9),-(7:9)])$rank
[1] 6

Another approach might be to use the alias function:

alias( lm( rnorm(NROW(dfrm)) ~ dfrm) )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文