[R] pca with missing values

Angel Lopez angel_lul at hotmail.com
Wed Jun 30 20:37:01 CEST 2004


Thanks for the pointers.
I've read them with care but I don't seem capable of making it work.
For example, if I do:

data(USArrests)
USArrests2<-USArrests
USArrests2[1,1]<-NA
princomp(USArrests2, cor = TRUE, na.action = "na.omit")

I get the error message:
Error in cov.wt(z) : x must contain finite values only

I've tried changing the 'options' na.action and using other values than 
na.omit with no success.

The only way that I can make it work in some way was if I did:

USArrestsNA<-na.omit(USArrests)
princomp(USArrestsNA, cor = TRUE)


I've also obtained the same by giving the correlation matrix instead of 
the data frame:
princomp(covmat=cor(USArrestsNA))
Both solutions do the job by not using the row with the NA.
After more reading I thought I would get the same result by doing:
princomp(covmat=cor(USArrests2,use="complete.obs"))
but the result is slightly different. I can not manage to understand the 
difference.
Can someone give me some more light to keep going?
P.S:Using the solution above with na.omit would not be very good in my 
real world problem because it is relatively common to have an NA in a 
row. Maybe using 
princomp(covmat=cor(USArrests2,use="pairwise.complete.obs"))
would be a solution but I would like to understand the above before 
doing this next step.
Thanks,
Ange


Prof Brian Ripley wrote:
> On Wed, 30 Jun 2004, Angel Lopez wrote:
> 
> 
>>I need to perform a principal components analysis on a matrix with 
>>missing values.
>>I've searched the R web site but didn't manage to find anything.
> 
> 
>     ?princomp
> 
> has a description of an na.action argument, and 
> 
>     help.search("missing values")
> 
> comes up with several relevant entries.
>




More information about the R-help mailing list