【问题标题】:Princomp error in R : covariance matrix is not non-negative definiteR中的Princomp错误:协方差矩阵不是非负定的
【发布时间】:2014-10-16 04:43:59
【问题描述】:

我有这个脚本,它对变量数量进行简单的 PCA 分析,最后将两个坐标和另外两个列(存在,NZ_Field)附加到输出文件。我以前做过很多次,但现在它给了我这个错误:

我知道这意味着存在负特征值。我查看了建议使用 na.omit 的类似帖子,但它没有用。 我在这里上传了“biodata.Rdata”文件:

协方差矩阵不是非负定的

https://www.dropbox.com/s/1ex2z72lilxe16l/biodata.rdata?dl=0

我很确定这不是因为数据中缺少值,因为我使用了具有不同“存在”和“NZ_Field”列的相同数据。

非常感谢任何帮助。

load("biodata.rdata")

#save data separately
coords=biodata[,1:2]
biovars=biodata[,3:21]
presence=biodata[,22]
NZ_Field=biodata[,23]

#Do PCA
bpc=princomp(biovars ,cor=TRUE)

#re-attach data with auxiliary data..coordinates, presence and NZ location data
PCresults=cbind(coords, bpc$scores[,1:3], presence, NZ_Field)
write.table(PCresults,file= "hlb_pca_all.txt", sep= ",",row.names=FALSE)

【问题讨论】:

  • 什么时候出现这个错误?
  • 嗨,当我运行这部分时:bpc=princomp(biovars ,cor=TRUE)
  • 它适用于我(R 版本 3.1.1 已修补,平台:x86_64-unknown-linux-gnu(64 位))。
  • 您也可以使用prcomp 函数代替princomp,它应该适用于您的情况。
  • 来自?prcomp: "The calculation is done by a singular value decomposition of the (centered and possibly scaled) data matrix, not by using eigen on the covariance matrix. This is generally the preferred method for numerical accuracy."

标签: r princomp


【解决方案1】:

这似乎是缺少数据的问题,因此有几种方法可以处理它。一种方法是在运行 PCA 之前手动对数据进行列表删除,在您的情况下是:

biovars<-biovars[complete.cases(biovars),]

另一种选择是使用另一个包,特别是psych 似乎在这里工作得很好,你可以使用principal(biovars),虽然输出有点不同,但它确实可以使用成对删除,所以基本上归结为是否或不想使用成对或列表删除。谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-15
    • 2018-03-05
    相关资源
    最近更新 更多