Hank
Hank

Reputation: 51

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

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)

Upvotes: 4

Views: 6289

Answers (1)

costebk08
costebk08

Reputation: 1359

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!

Upvotes: 0

Related Questions