R 中的 Princomp 错误:协方差矩阵不是非负定的 [英] Princomp error in R : covariance matrix is not non-negative definite

查看:162
本文介绍了R 中的 Princomp 错误:协方差矩阵不是非负定的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this script which does a simple PCA analysis on number of variables and at the end attaches two coordinates and two other columns(presence, NZ_Field) to the output file. I have done this many times before but now its giving me this error:

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

I understand that it means there are negative eigenvalues. I looked at similar posts which suggest to use na.omit but it didn't work. I have uploaded the "biodata.Rdata" file here:

协方差矩阵不是非负定的

covariance matrix is not non-negative definite

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

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

I am pretty sure it is not because of missing values in data because I have used the same data with different "presence" and "NZ_Field" column.

非常感谢任何帮助.

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)

推荐答案

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

This does appear to be an issue with missing data so there are a few ways to deal with it. One way is to manually do listwise deletion on the data before running the PCA which in your case would be:

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

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

The other option is to use another package, specifically psych seems to work well here and you can use principal(biovars), and while the output is bit different it does work using pairwise deletion, so basically it comes down to whether or not you want to use pairwise or listwise deletion. Thanks!

这篇关于R 中的 Princomp 错误:协方差矩阵不是非负定的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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