当协方差矩阵为零时,如何在R中使用princomp()函数? [英] How to use princomp () function in R when covariance matrix has zero's?

查看:262
本文介绍了当协方差矩阵为零时,如何在R中使用princomp()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

[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]

推荐答案

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

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)

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

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

另一种方法可能是使用alias函数:

Another approach might be to use the alias function:

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

这篇关于当协方差矩阵为零时,如何在R中使用princomp()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆