首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LDA贡献双程图

LDA贡献双程图
EN

Stack Overflow用户
提问于 2016-10-19 03:16:22
回答 2查看 2.3K关注 0票数 3

我试图为线性判别分析(LDA)创建一个双图。我正在使用从这里获得的代码的修改版本https://stats.stackexchange.com/questions/82497/can-the-scaling-values-in-a-linear-discriminant-analysis-lda-be-used-to-plot-e

然而,我有80个变量,使得双情节极难读懂。由于它们的箭头长度很长,其余的标签在中间被缩小,高贡献变量使这种情况更加恶化。因此,我试图实现的是一个双图,所有可变箭头都是相同长度的,它们的相对贡献(标度)用分级颜色来区分。到目前为止,我已经得到了分级的颜色,但我无法找到使箭头长度相同的方法。据我所知,geom_textgeom_segment使用LD1和LD2值来确定箭头的长度方向。我怎样才能覆盖长度?

代码:

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-19 13:01:39

像这样的怎么样?我们得做一些三角学才能使长度相等。请注意,等式位于绘图坐标中,因此,如果您希望实际以相同的大小出现,则需要添加coord_equal

(我清理了你的绘图代码,因为很多代码都很乱。)

代码语言:javascript
复制
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")

票数 4
EN

Stack Overflow用户

发布于 2018-05-24 19:25:27

我想我有一个更简单的代码来实现双情节。我希望下面的代码能有所帮助。我使用了IRIS数据集进行分析。

代码语言:javascript
复制
library(readr)
IR <- read_csv("D:/Keerthesh/R Folder/DataSet/Iris.csv")

# --- data partition -- #
set.seed(555)
IRSam <- sample.int(n = nrow(IR), size = floor(.60*nrow(IR)), replace = FALSE, prob = NULL)
IRTrain <- IR[IRSam,]
IRTest <- IR[-IRSam,]

library(MASS)
IR.lda <- lda(Species~. -Id, data=IRTrain)
print(IR.lda)

要绘制双图,您需要从github安装ggord包。

代码语言:javascript
复制
library(devtools)
install_github('fawda123/ggord') --- Used to install ggord from github we need to run devtools to achieve this.
library(ggord)
ggord(IR.lda, IRTrain$Species, ylim=c(-5,5), xlim=c(-10,10))

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40121516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档