2016-10-19 2 views
1

선형 차별 분석 (LDA)을위한 biplot을 만들려고합니다. 여기에서 얻은 코드의 수정 된 버전을 사용하고 있습니다. https://stats.stackexchange.com/questions/82497/can-the-scaling-values-in-a-linear-discriminant-analysis-lda-be-used-to-plot-eLDA 기여 biplot

그러나 80 개의 변수가있어서 바이 플롯을 읽기가 매우 어렵습니다. 화살표 길이가 매우 길고 나머지 레이블이 가운데에 표시되므로 변수가 높으면이 값이 나빠집니다. 그래서 달성하고자하는 것은 모든 변수 화살표의 길이가 같고 상대적 기여도 (비율)가 단계적 색상으로 구분되는 biplot입니다. 지금까지 나는 단계별 색상을 얻을 수 있었지만 화살표 길이를 동일하게 만드는 방법을 찾을 수 없습니다. 내가 이해 한 것으로부터, geom_textgeom_segment길이가이고 방향이 인 화살표를 결정하기 위해 LD1과 LD2 값을 사용합니다. 길이를 어떻게 재정의 할 수 있습니까?

enter image description here

CODE :

library(ggplot2) 
library(grid) 
library(MASS) 
data(iris) 


iris.lda <- lda(as.factor(Species)~., 
       data=iris) 

#Project data on linear discriminants 
iris.lda.values <- predict(iris.lda, iris[,-5]) 

#Extract scaling for each predictor and 
data.lda <- data.frame(varnames=rownames(coef(iris.lda)), coef(iris.lda)) 

#coef(iris.lda) is equivalent to iris.lda$scaling 

data.lda$length <- with(data.lda, sqrt(LD1^2+LD2^2)) 

#Plot the results 
p <- qplot(data=data.frame(iris.lda.values$x), 
      main="LDA", 
      x=LD1, 
      y=LD2, 
      colour=iris$Species)+stat_ellipse(geom="polygon", alpha=.3, aes(fill=iris$Species)) 
p <- p + geom_hline(aes(yintercept=0), size=.2) + geom_vline(aes(xintercept=0), size=.2) 
p <- p + theme(legend.position="right") 
p <- p + geom_text(data=data.lda, 
        aes(x=LD1, y=LD2, 
         label=varnames, 
         shape=NULL, linetype=NULL, 
         alpha=length, position="identity"), 
        size = 4, vjust=.5, 
        hjust=0, color="red") 
p <- p + geom_segment(data=data.lda, 
         aes(x=0, y=0, 
          xend=LD1, yend=LD2, 
          shape=NULL, linetype=NULL, 
          alpha=length), 
         arrow=arrow(length=unit(0.1,"mm")), 
         color="red") 
p <- p + coord_flip() 

print(p) 

답변

1

어떻게 이런 일에 대해? 우리는 길이가 동일 해 지도록 삼각법을 수행해야합니다. 평등은 플롯 좌표이므로 실제로 같은 크기로 표시하려면 coord_equal을 추가해야합니다.

는 (그것의 많은 아주 엉망 때부터, 당신의 플로팅 코드를 정리.)

rad <- 3 # This sets the length of your lines. 
data.lda$length <- with(data.lda, sqrt(LD1^2+LD2^2)) 
data.lda$angle <- atan2(data.lda$LD1, data.lda$LD2) 
data.lda$x_start <- data.lda$y_start <- 0 
data.lda$x_end <- cos(data.lda$angle) * rad 
data.lda$y_end <- sin(data.lda$angle) * rad 

#Plot the results 
ggplot(cbind(iris, iris.lda.values$x), 
     aes(y = LD1, x = LD2, colour = Species)) + 
    stat_ellipse(aes(fill = Species), geom = "polygon", alpha = .3) + 
    geom_point() + 
    geom_hline(yintercept = 0, size = .2) + 
    geom_vline(xintercept = 0, size = .2) + 
    geom_text(aes(y = y_end, x = x_end, label = varnames, alpha = length), 
      data.lda, size = 4, vjust = .5, hjust = 0, colour = "red") + 
    geom_spoke(aes(x_start, y_start, angle = angle, alpha = length), data.lda, 
      color = "red", radius = rad, size = 1) + 
    ggtitle("LDA") + 
    theme(legend.position = "right") 

enter image description here

+1

정말 놀라운를! 나는 이걸 생각해 내지 못했을거야. 고맙습니다! –