wen
wen

Reputation: 1935

What was wrong with running princomp() in R?

I was running PCA with princomp(). My dataset is called vt.

pca = princomp(as.matrix(vt))
Error in cov.wt(z) : 'x' must contain finite values only

However, when I check if I got infinite values, there's none.

sum(is.infinite(as.matrix(vt)))
[1] 0

What was wrong with my analysis?

vt is a data.frame with all numeric values.

is.numeric(as.matrix(vt))
[1] TRUE

Upvotes: 0

Views: 1369

Answers (1)

user2357031
user2357031

Reputation:

The solution would appear to be the removal of missing values. There are two ways to remove missing values from the data set. You can either remove them using na.omit() function explicitly before calling princomp() or you can use a formula interface to princomp() with an argument na.action=na.omit. You have used the matrix interface to princomp(), and na.action is not an option for the matrix interface, hence it does not work in your case. See ?princomp for more details.

Consider the following:

# Add one missing value to USArrests data set
USArrests[1,1]<-NA

# Does not work (matrix interface)
princomp(USArrests)
# Error in cov.wt(z) : 'x' must contain finite values only

# Does work (formula interface)
princomp(~., data=USArrests, na.action=na.omit)

# Does work (remove missing values before PCA)
princomp(na.omit(USArrests))

So in your case something like:

pca = princomp(~., data=as.matrix(vt), na.action=na.omit)

should do the trick.

Upvotes: 1

Related Questions