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

标签 r statistics pca eigenvector princomp

在R中使用princomp()函数时,遇到以下错误:"covariance matrix is not non-negative definite"

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

当协方差矩阵包含零时,是否可以进行PCA处理?

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

最佳答案

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

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

这将确定具有负特征值的向量:
which(eigen(M)$values < 0)

使用help(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函数:
alias( lm( rnorm(NROW(dfrm)) ~ dfrm) )

关于r - 当协方差矩阵为零时,如何在R中使用princomp()函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8562286/

相关文章:

r - 你能在r中的gt表中为相邻单元格着色吗?

r - 将彩色箭头添加到ggplot2的轴(部分在绘图区域之外)

python - 检测折线图分段的正确算法是什么?

matlab - 在 Matlab 中使用 PCA 降低训练数据的维度

c++ - 使用特征库进行主成分分析

Python PCA - 投影到低维空间

r - 以编程方式删除 dplyr 中的 `group_by` 字段

r - 如何将 !duplicate 与 rpy2 一起使用?

python - 在 Python 中使用 3D 数据实现 2 样本 KS 测试

php - PHP中特定计数的非重复组合