1

(Happy Easter)

I have been trying to work out a PDDP algorithm in R, and I keep running into a single, small error while using the princomp function. Here is the error statement:

Error in cov.wt(z) : 'x' must contain finite values only

From some cursory research, this error comes up whenever there are NA or undefined values in the data in the princomp function. However, the data I am using (wikipedia's iris data) has no NA's in it. This is the line of code with the princomp in it:

pr <- princomp(data[iIndexes,], scores = TRUE, na.action = na.exclude)

From my research, I was told this the standard na.action value (na.omit) is currently bugged, so I was recommended to use na.exclude instead. I was also suggested to include a function as the first parameter, but this did not change the error message either.

Here is where I got that information: Omit NA and data imputation before doing PCA analysis using R

Let me know if you need the rest of the code in context. Thanks

Edit: As requested, here is the output of dput(data[iIndexes,]):

 structure(c(36L, 19L, 10L, 6L, 32L, 52L, 8L, 28L, 2L, 20L, ...), .Label = 
 c("4.3,3,1.1,0.1", "4.4,2.9,1.4,0.2", "4.4,3,1.3,0.2", "4.4,3.2,1.3,0.2", ...), 
 class = "factor")

The ellipses are further sets of data which are similar to the rest, 150 samples each.

Community
  • 1
  • 1
Nathan
  • 194
  • 1
  • 17

1 Answers1

1

I you run the code without the class = "factor" parameter the pr will return correctly.

This worked for me:

a = c(36L, 19L, 10L, 6L, 32L, 52L, 8L, 28L, 2L, 20L)
b = c("stringa", "stringb")

data = structure(a, .Label = b)

pr <- princomp(data, scores = TRUE, na.action = na.exclude)
pr

pr returns:

Call:
princomp(x = data, scores = TRUE, na.action = na.exclude)

Standard deviations:
  Comp.1 
14.95359 

 1  variables and  10 observations.

Solution

Unclass the data:

tempVar <- unclass(data[iIndexes,])

and run the code on tempVar

noumenal
  • 1,077
  • 2
  • 16
  • 36
  • the `class = "factor"` is an output. I never told it do do that in the first place. – Nathan Mar 27 '16 at 18:56
  • I think you are passing `data[iIndexes,]` as an object, when you should be passing a data structure. – noumenal Mar 27 '16 at 19:00
  • Perhaps you could try `tempVar <- unclass(data[iIndexes,])` and run the code on `tempVar` – noumenal Mar 27 '16 at 19:03
  • 1
    I fixed it using the `tempVar` suggestion. I must say R is extremely counter-intuitive as to what counts as data and what does not. – Nathan Mar 27 '16 at 19:21
  • @Nathan The actual problem is clearly your data import. You have comma separated values, but didn't tell R this when you imported the data. – Roland Mar 28 '16 at 15:18