[R] PCA : Error in eigen(cv,

Daniel Malter daniel at umd.edu
Tue Jul 1 05:46:24 CEST 2008


Hi, yes a loop would work. Maybe there are other options also. Bootstrapping
a PCA with a loop can be done as follows. You will have to adjust the
dimension of the matrices/array according to the dimensions of your data and
according to the values that you want to retrieve from your PCA.

####Start of example

##Iris data
data=iris

##Inspect data
data

##Perform pca on columns 1:4
x=prcomp(data[,1:4])

##Inspect results of pca
summary(x)
summary(x)$importance

trials=10 ##we want 10 bootstraps

##We want to bootstrap pca
##and, say, know some information
##contained in "summary(x)$importance"
##Thus, we create an array that has the
##dimension of "summary(x)$importance"
##and depth of "trials"
importance=array(0,c(3,4,trials))

##Bootstrap the PCA
for(i in 1:trials){
    ##sample IDs of original data rows
    sam=sample(1:length(data[,1]),replace=T)

    ##sample new data from old data
    ##using the data row IDs from above
    newdata=data[c(sam),]

    ##store the results of the 
    ##importance matrix resulting from PCA
    ##performed on columns 1:4 
    ##of our bootstrapped data
    importance[,,i]=summary(prcomp(newdata[,1:4]))$importance
    }
    
##Now we can assess, for example, 
##the mean and SD
##of the accounted variance of the
##first principal component

##mean of the variance accounted for
##by the first PC over all trials
mean(importance[2,1,])

##sd of the variance accounted for
##by the first PC over all trials
sd(importance[2,1,])

####End of example

Best,
Daniel

-------------------------
cuncta stricte discussurus
-------------------------

-----Ursprüngliche Nachricht-----
Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im
Auftrag von Tanya Yatsunenko
Gesendet: Monday, June 30, 2008 10:09 PM
An: r-help at r-project.org
Betreff: [R] PCA : Error in eigen(cv,

Hi all,

I am doing bootstrap on a distance matrix, in which samples have been drawn
with replacement. After that I do PCA on a resulted matrix, and these 2
steps are repeated 1000 times.

pca(x) is a vector where I wanted to store all 1000 PCAs; and x is from
1 to 1000
SampleD is a new matrix after resampling;

I am getting the following error message, which I don't understand:
....
+pca(x)<-princomp(SampleD[i,j])
+ }
Error in eigen(cv, symmetric = TRUE) : infinite or missing values in 'x'

Should I maybe not use a vector, but matrix instead?
Thanks!

--
Tanya.


	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list