-
Notifications
You must be signed in to change notification settings - Fork 2.1k
googlegroupsformatter
require(pcaMethods) require(ggplot2)
x = c(runif(50,min=0,max=1),runif(50,min=0.5,max=3)) dim(x) = c(10,10)
PCA<- pca(x, method="nipals", scale="uv", nPcs=3, center=T, completeObs=T, cv="q2") Q2<-Q2(PCA) Q2 PCA@R2 #Need to find the point at which Q2 no longer increases #bio data R2~= 0.3 and Q2 <= R2 +/- 0.2 is fine. PCA@scores PCA@loadings
#loadings are matrix, need data.frame for ggplot loadings<-as.data.frame(PCA@loadings) #carry over names of rows loadings$Names<-rownames(loadings)
#scores=matrix too scores<-data.frame(PCA@scores)
#pure number scaling not very good but works... for now #rescaling Loadings to scores loadings1<-rescale(loadings1, to = c(min(scores1),max(scores1))) loadings2<-rescale(loadings2, to = c(min(scores2),max(scores2))) loadings3<-rescale(loadings3, to = c(min(scores3),max(scores3)))
ggplot()+ geom_point(data=scores, aes(x=PC1, y=PC2))+ geom_segment(data=loadings, aes(x=0, y=0, xend=PC1, yend=PC2) , arrow=arrow(length=unit(0.2,"cm")), alpha=0.25)+ geom_text(data=loadings, aes(x=PC1, y=PC2, label=Names), alpha=0.5, size=3)+ scale_colour_discrete("Variety")+ scale_x_continuous("Principal Component 1")+ scale_y_continuous("Principal Component 2")+ theme_bw()