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.

4

1 回答 1

1

我在没有class = "factor"参数的情况下运行代码,pr将正确返回。

这对我有用:

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返回:

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

Standard deviations:
  Comp.1 
14.95359 

 1  variables and  10 observations.

解决方案

取消数据分类:

tempVar <- unclass(data[iIndexes,])

并在 tempVar 上运行代码

于 2016-03-27T18:53:47.133 回答