Princomp error in R : covariance matrix is not non-negative definite

久未见 提交于 2021-02-19 02:36:18

问题


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:

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

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.

Any help is highly appreciated.

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)

回答1:


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),]

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!



来源:https://stackoverflow.com/questions/26396356/princomp-error-in-r-covariance-matrix-is-not-non-negative-definite

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!