2012-08-22 2 views
3

저는 R과 통계에 매우 익숙하며 PCA 플롯에 다중 신뢰 타원을 추가하는 데 문제가 있습니다.R의 PCA 플롯에 다중 신뢰 타원을 추가하는 방법은 무엇입니까?

내 관심은 95 % 신뢰 타원을 사용하여 PCA 플롯의 잠재적 인 그룹/클러스터를 강조하는 데 있습니다. 나는 R에서 dataEllipse 함수를 사용해 보았지만, PCA 플롯에 여러 개의 타원을 추가하는 방법을 알아낼 수 없었다. (센터는 클러스터를 포함하는 다양한 지점에 놓이게된다.이 경우에는 lithic 소스와 lithic 도구 가능성이 그 소스에서 만든).

감사합니다. {

lithic_final <- LITHIC.DATASHEET.FOR.R.COMPLETE.FORMAT 
lithic_final 

pca1 <- princomp(lithic_final); pca1 

lithic_source <- c("A1", "A1", "A1", "A1", "A2","A2", "A2", "A3","A3","A3","B","B","B","B","B","B","C","C","C","C","C","C","C","D","D","D","D","D","D","D","D","E","E","E","E","E","E","E","E","F","F","G","G","G","G","H","H","H","H","H","H","H","I1","I1","I1","I2","I2","I2","I2","I2","J1","J1","J2","J2","J2","J2","J2","J2","J2","J2","J2","K","K","K","K","K","K","K","L","L","L","L","L","L","L","L","L","L","L","L","L","L","BB1","BB1","BB1","FC","FC","FC","JRPP","JRPP","JRPP","BB2","BB2","BB2","BB2","MWP","MWP","MWP","MWP","RPO","RPO","RPO") 

lithic_source 

summary(pca1) 

plot(pca1) 

#Plotting the scores with the Lithic Source Info 
round(pca1$scores[,1:2], 2) 
pca_scores <-round(pca1$scores[,1:2], 2) 
plot(pca1$scores[,1], pca1$scores[,2], type="n") 
text(pca1$scores[,1], pca1$scores[,2],labels=abbreviate(lithic_source, minlength=3), cex=.45) 



#Plotting PCA Scores of EACH SAMPLE for PCA 2 and 3 with Lithic Source Info 
round(pca1$scores[,2:3], 2) 
pca2_3_scores <-round(pca1$scores[,2:3], 2) 
plot(pca1$scores[,2], pca1$scores[,3], type="n") 
text(pca1$scores[,2], pca1$scores[,3], labels=abbreviate(lithic_source, minlength=3), cex=.45) 

#Plotting PCA Scores of EACH SAMPLE for PCA 3 and 4 with Lithic Source Info 
round(pca1$scores[,3:4], 2) 
pca3_4_scores <-round(pca1$scores[,3:4], 2) 
plot(pca1$scores[,3], pca1$scores[,4], type="n") 
text(pca1$scores[,3], pca1$scores[,4], labels=abbreviate(lithic_source, minlength=3), cex=.45) 

#Plotting PCA Scores of EACH SAMPLE for PCA 1 and 3 with Lithic Source Info 
round(pca1$scores[,1:3], 2) 
pca1_3_scores <-round(pca1$scores[,1:3], 2) 
plot(pca1$scores[,1], pca1$scores[,3], type="n") 
text(pca1$scores[,1], pca1$scores[,3], labels=abbreviate(lithic_source, minlength=3), cex=.45) 

#Plotting PCA Scores of EACH SAMPLE for PCA 1 and 4 with Lithic Source Info 
round(pca1$scores[,1:4], 2) 
pca1_4_scores <-round(pca1$scores[,1:4], 2) 
plot(pca1$scores[,1], pca1$scores[,4], type="n") 
text(pca1$scores[,1], pca1$scores[,4], labels=abbreviate(lithic_source, minlength=3), cex=.45) 

#TRYING TO GET ELLIPSES ADDED TO PCA 1 and 4 scores 
dataEllipse(pca1$scores[,1], pca1$scores[,4],centers=12,add=TRUE,levels=0.9, plot.points=FALSE) 


structure(list(Ca.K12 = c(418L, 392L, 341L, 251L, 297L, 238L, 
258L, 5L, 2L, 37L), Cr.K12 = c(1L, 12L, 15L, 6L, 9L, 6L, 35L, 
7L, 45L, 32L), Cu.K12 = c(89L, 96L, 81L, 63L, 88L, 103L, 104L, 
118L, 121L, 90L), Fe.K12 = c(18627L, 18849L, 18413L, 12893L, 
17757L, 17270L, 16198L, 2750L, 4026L, 3373L), K.K12 = c(20L, 
23L, 28L, 0L, 34L, 17L, 45L, 102L, 150L, 147L), Mn.K12 = c(205L, 
212L, 235L, 120L, 216L, 212L, 246L, 121L, 155L, 115L), Nb.K12 = c(139L, 
119L, 154L, 91L, 122L, 137L, 137L, 428L, 414L, 428L), Rb.K12 = c(99L, 
42L, 79L, 49L, 210L, 243L, 168L, 689L, 767L, 705L), Sr.K12 = c(3509L, 
3766L, 3481L, 2715L, 2851L, 2668L, 2695L, 202L, 220L, 217L), 
    Ti.K12 = c(444L, 520L, 431L, 293L, 542L, 622L, 531L, 82L, 
    129L, 84L), Y.K12 = c(135L, 121L, 105L, 74L, 144L, 79L, 85L, 
    301L, 326L, 379L), Zn.K12 = c(131L, 133L, 108L, 78L, 124L, 
    111L, 114L, 81L, 78L, 59L), Zr.K12 = c(1348L, 1479L, 1333L, 
    964L, 1506L, 1257L, 1296L, 3967L, 4697L, 4427L)), .Names = c("Ca.K12", 
"Cr.K12", "Cu.K12", "Fe.K12", "K.K12", "Mn.K12", "Nb.K12", "Rb.K12", 
"Sr.K12", "Ti.K12", "Y.K12", "Zn.K12", "Zr.K12"), row.names = c(NA, 
10L), class = "data.frame") 
+1

최소한 지금까지 사람들이 [과 같이] –

+0

에 갈 일 (http://stackoverflow.com/questions/5963269/how-to-make-a을 그래서 무엇을 재현하는 몇 가지 코드를 게시하시기 바랍니다 -great-r-reproducible-example) – ROLO

+0

일부 코드가 추가되었습니다. 죄송합니다.이 모든 것에 대해 새로운 것이고, 더 많은 실수와 제 코드에 대한 우아함에 대한 사과입니다. 마지막 줄은 내 데이터 집합에 타원을 추가 할 수있는 가장 가까운 줄입니다. – guido

답변

6

난 당신이 모든 관계없는 물건 대신 귀하의 질문에 집중했다 경우 더 빠른 응답을받은 것 같아요. 당신은 당신의 질문에 아무런 관련이없는 여러 가지 주요 구성 요소를 계획하라는 명령을 주셨습니다. 질문은 그룹별로 타원을 어떻게 그립니다. 10 줄 및 3 그룹의 샘플 데이터는 3 포인트가 데이터 타원을 표시하기에 충분하지 않기 때문에 도움이되지 않습니다.

첫째, 재현 예 : 당신은 당신의 질문에 간단한 대답이 패키지 차에 dataEllipse 기능을 사용하는

set.seed(42) # so you can get the same numbers I get 
source_a <- data.frame(X1=rnorm(25, 50, 5), X2=rnorm(25, 40, 5)) 
source_b <- data.frame(X1=rnorm(25, 20, 5), X2=rnorm(25, 40, 5)) 
source_c <- data.frame(X1=rnorm(25, 35, 5), X2=rnorm(25, 25, 5)) 
lithic_dat <- rbind(source_a, source_b, source_c) 
lithic_source <- c(rep("a", 25), rep("b", 25), rep("c", 25)) 

플롯이 산점도 (과 타원을) 및 텍스트 추가

scatterplot(X2~X1 | lithic_source, data=lithic_dat, pch="", smooth=FALSE, 
    reg.line=FALSE, ellipse=TRUE, levels=.9) 
text(lithic_dat$X1, lithic_dat$X2, lithic_source, cex=.75) 

산점도는 당신이 원하는 모든 일을 쥐게하지만, 그것을 사용하지 않고 타원을 그릴 수도 가능하다 할 수 있습니다

01,231,620,571,714,892,167,732 10

이것은 주 구성 요소 및 기타 모든 데이터에 적용됩니다.

+0

좋은 답변입니다. Carlson 박사에 오신 것을 환영합니다. –

+0

좋아요!정말 고마워요. 완벽하게 작동했습니다. 지저분한 질문에 대해 미안하지만이 도움에 다시 한번 감사드립니다! – guido

3

아이리스 (iris) 데이터와 함께 ggbiplot (github에서 사용 가능)이라는 패키지를 사용하는 간단한 해결책이 있습니다. 나는 이것이 당신이 찾고 있었던 것이기를 바랍니다.

library(devtools);install_github('vqv/ggbiplot') 
library(ggbiplot) 
pca = prcomp(iris[,1:4]) 
ggbiplot(pca,groups = iris$Species,ellipse = T,ellipse.prob = .95) 
관련 문제