2016-10-24 2 views
1

Dies scheint ein triviales Problem, aber ich kann das Problem nicht lösen!Dynamische Auswahl der wichtigsten Komponenten aus der PCA-Ausgabe

Ich habe numerische Spalten von Irisdaten ..then normalisiert es wie unten

newiris<-iris[,1:4] 
iris.norm<-data.frame(scale(newiris)) 
head(iris.norm) 
    Sepal.Length Sepal.Width Petal.Length Petal.Width 
1 -0.8976739 1.01560199 -1.335752 -1.311052 
2 -1.1392005 -0.13153881 -1.335752 -1.311052 
3 -1.3807271 0.32731751 -1.392399 -1.311052 
4 -1.5014904 0.09788935 -1.279104 -1.311052 
5 -1.0184372 1.24503015 -1.335752 -1.311052 
6 -0.5353840 1.93331463 -1.165809 -1.048667 

# performed PCA now 
pccomp <- prcomp(iris.norm) 
summary(pccomp) 
a <- summary(pccomp) 
df<- as.data.frame(a$importance) 
df <- t(df) 
df 
##  Standard deviation Proportion of Variance Cumulative Proportion 
## PC1   1.7083611    0.72962    0.72962 
## PC2   0.9560494    0.22851    0.95813 
## PC3   0.3830886    0.03669    0.99482 
## PC4   0.1439265    0.00518    1.00000 

Jetzt Umwandlung rownames in eine Spalte für df gesetzt genommen, so dass PCs, die rownames bildet die erste Säule zur weiteren Manipulation

waren
library(tibble) 
    library(dplyr) 
    df<-rownames_to_column(as.data.frame(df), var="PrinComp") %>% head 
    df 
    ## PrinComp Standard deviation Proportion of Variance Cumulative Proportion 
    ## 1  PC1   1.7083611    0.72962    0.72962 
    ## 2  PC2   0.9560494    0.22851    0.95813 
    ## 3  PC3   0.3830886    0.03669    0.99482 
    ## 4  PC4   0.1439265    0.00518    1.00000 

# Now will be selecting only those PCs where the cumulative proportion is say less than 96% 
# subsetting 
pcs<-as.vector(as.character(df[which(df$`Cumulative Proportion`<0.96),][,1])) # cumulative prop less than 96% 
pcs 
## [1] "PC1" "PC2" 

Jetzt bin ich die Schaffung eines PC-Datenrahmen statisch von Vektor-Noten aus den ersten zwei Hauptkomponenten, die wir aus der obigen Bedingung bekam (cum prop < 0,96)

Mein Problem ist, wie kann ich die oben genannten pc Datenrahmen dynamisch erstellen, sobald ich die Anzahl der PCs basierend auf Bedingung wie kumulativen Anteil sagen, weniger als 0,95 sein?

Antwort

1

Sie können einfach eine while-Schleife im Feld df's cumulative proportion ausführen und den umgewandelten Wert anhängen, bis er unter dem erforderlichen Schwellenwert liegt.

threshold = 0.96 
pcdf = list() 
i = 1 
while(df$`Cumulative Proportion`[i]<threshold){ 
    pcdf[[i]] = pccomp$x[,i] 
    i = i +1 
} 
pcdf = as.data.frame(pcdf) 

names(pcdf) = paste("x",c(1:ncol(pcdf)),sep="") 

Der Ausgang

> head(pcdf) 
     x1   x2 
1 -2.257141 -0.4784238 
2 -2.074013 0.6718827 
3 -2.356335 0.3407664 
4 -2.291707 0.5953999 
5 -2.381863 -0.6446757 
6 -2.068701 -1.4842053 

wenn die threshold = 0.999 den gleichen Code ausgeführt

gibt
> head(pcdf) 
     x1   x2   x3 
1 -2.257141 -0.4784238 0.12727962 
2 -2.074013 0.6718827 0.23382552 
3 -2.356335 0.3407664 -0.04405390 
4 -2.291707 0.5953999 -0.09098530 
5 -2.381863 -0.6446757 -0.01568565 
6 -2.068701 -1.4842053 -0.02687825 

UPDATE

Angenommen, Sie die Anzahl der Hauptkomponente wissen Sie sagen wollen i .you

a <- sapply(X = c(1:i),FUN = function(X){pcdf[[X]] = pccomp$x[,X]}) 

anstelle des gesamten while loop section verwenden. so für i = 2 Sie erhalten

> head(a) 
      [,1]  [,2] 
[1,] -2.257141 -0.4784238 
[2,] -2.074013 0.6718827 
[3,] -2.356335 0.3407664 
[4,] -2.291707 0.5953999 
[5,] -2.381863 -0.6446757 
[6,] -2.068701 -1.4842053 

wo ein Ihr Ergebnis ist.

+0

... fantastico ... tats wat, die ich suchte .... – Nishant

+0

wud luv zu knw, wenn die Lösungen mit Anwendung der Familienfunktionen weiter verkürzen können? – Nishant

+0

Angenommen, Sie kennen die Nummer der Hauptkomponente, die Sie sagen wollen, 'i', können Sie einfach die Sapply-Funktion anwenden, um dies zu erreichen. Ich werde es als Update für meine Antwort verwenden. – 9Heads

1

Unter der Annahme, dass Sie immer mindestens ein PC wollen, hier ist eine einzeilige Version

p <- 0.96 
pccomp$x[,1:nrow(df[which(df$`Cumulative Proportion`<p),])] # first two PCs 
p <- 0.75 
pccomp$x[,1:nrow(df[which(df$`Cumulative Proportion`<p),])] # first PC 
+1

, @ 9Heads ... einfach genial ....... – Nishant

+0

@sandipan ... einfach genial ...... – Nishant

0

Zusätzlich zu der gr8 Lösung bereitgestellt oben:

pcs<-as.vector(as.character(df1[which(df1$`Cumulative Proportion`<0.96),][,1])) # cumulative prop less than 96% 
pcs 
## [1] "PC1" "PC2" 
i=length(pcs) # we get the no of PCs fulfilling the cum prop condition 
a <- sapply(X = c(1:i),FUN = function(X){pcdf[[X]] = pccomp$x[,X]}) 
head(a) 

> head(a) 
     [,1]  [,2] 
[1,] -2.257141 -0.4784238 
[2,] -2.074013 0.6718827 
[3,] -2.356335 0.3407664 
[4,] -2.291707 0.5953999 
[5,] -2.381863 -0.6446757 
[6,] -2.068701 -1.4842053 

Fertig!

Verwandte Themen