如何绘制LDA的结果 [英] how to plot the results of a LDA

查看:225
本文介绍了如何绘制LDA的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题有很多答案.不仅在堆栈溢出,而且通过互联网.但是,没有人能解决我的问题.我有两个问题

There are quite some answers to this question. Not only on stack overflow but through internet. However, none could solve my problem. I have two problems

我尝试为您模拟数据

df <- structure(list(Group = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 
2, 2, 2), var1 = c(2, 3, 1, 2, 3, 2, 3, 3, 5, 6, 7, 6, 8, 5, 
5), var2 = c(9, 9, 9, 8, 7, 8, 9, 3, 2, 2, 1, 1, 2, 3, 3), var3 = c(6, 
7, 6, 6, 5, 6, 7, 1, 2, 1, 2, 3, 1, 1, 2)), .Names = c("Group", 
"var1", "var2", "var3"), row.names = c(NA, -15L), class = "data.frame")

然后我按如下操作:

fit <- lda(Group~., data=df)
plot(fit)

最后,我的小组出现在两个不同的情节中.

I end up with groups appearing in two different plots.

如何将我的结果绘制在一个数字中,例如线性判别分析图 使用ggplot2的线性判别分析图

how to plot my results in one figure like e.g. Linear discriminant analysis plot Linear discriminant analysis plot using ggplot2

或任何其他美丽的情节?

or any other beautiful plot ?

推荐答案

plot()函数实际上调用plot.lda(),您可以通过运行getAnywhere("plot.lda")来检查其源代码.该plot()函数可以使您在绘制之前传递的LDA对象的许多处理变得安静.结果,如果要自定义绘图的外观,则可能必须编写自己的函数,该函数从lda对象中提取信息,然后将其传递给绘图功能.这是一个示例(我对LDA不太了解,所以我只是修剪了默认plot.lda的源代码,并使用ggplot2包(非常灵活)来创建一堆图).

The plot() function actually calls plot.lda(), the source code of which you can check by running getAnywhere("plot.lda"). This plot() function does quiet a lot of processing of the LDA object that you pass in before plotting. As a result, if you want to customize how your plots look, you will probably have to write your own function that extracts information from the lda object and then passes it to a plot fuction. Here is an example (I don't know much about LDA, so I just trimmed the source code of the default plot.lda and use ggplot2 package (very flexible) to create a bunch of plots).

#If you don't have ggplot2 package, here is the code to install it and load it
install.packages("ggplot2")
library("ggplot2")
library("MASS")


#this is your code. The only thing I've changed here is the Group labels because you want a character vector instead of numeric labels
df <- structure(list(Group = c("a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b", "b", "b", "b", "b"),
                         var1 = c(2, 3, 1, 2, 3, 2, 3, 3, 5, 6, 7, 6, 8, 5, 5), 
                         var2 = c(9, 9, 9, 8, 7, 8, 9, 3, 2, 2, 1, 1, 2, 3, 3), 
                         var3 = c(6, 7, 6, 6, 5, 6, 7, 1, 2, 1, 2, 3, 1, 1, 2)),
                    .Names = c("Group","var1", "var2", "var3"),
                    row.names = c(NA, -15L), class = "data.frame")
fit <- lda(Group~., data=df)

#here is the custom function I made that extracts the proper information from the LDA object. You might want to write your own version of this to make sure it works with all cases (all I did here was trim the original plot.lda() function, but I might've deleted some code that might be relevant for other examples)

ggplotLDAPrep <- function(x){
  if (!is.null(Terms <- x$terms)) {
    data <- model.frame(x)
    X <- model.matrix(delete.response(Terms), data)
    g <- model.response(data)
    xint <- match("(Intercept)", colnames(X), nomatch = 0L)
    if (xint > 0L) 
      X <- X[, -xint, drop = FALSE]
  }
  means <- colMeans(x$means)
  X <- scale(X, center = means, scale = FALSE) %*% x$scaling
  rtrn <- as.data.frame(cbind(X,labels=as.character(g)))
  rtrn <- data.frame(X,labels=as.character(g))
  return(rtrn)
}

fitGraph <- ggplotLDAPrep(fit)

#Here are some examples of using ggplot to display your results. If you like what you see, I suggest to learn more about ggplot2 and then you can easily customize your plots

#this is similar to the result you get when you ran plot(fit)
ggplot(fitGraph, aes(LD1))+geom_histogram()+facet_wrap(~labels, ncol=1)

#Same as previous, but all the groups are on the same graph
ggplot(fitGraph, aes(LD1,fill=labels))+geom_histogram()

下面的示例不适用于您的示例,因为您没有LD2,但这等效于您提供的外部示例中的散点图.我已在此处将该示例作为演示

The following example won't work with your example because you don't have LD2, but this is equivalent to the scatter plot in the external example you provided. I've loaded that example here as a demo

ldaobject <- lda(Species~., data=iris)
fitGraph <- ggplotLDAPrep(ldaobject)
ggplot(fitGraph, aes(LD1,LD2, color=labels))+geom_point()

我没有对ggplot设置进行太多自定义,但是如果您尝试使用它,则可以使图形看起来像您想要的任何东西.希望这会有所帮助!

I didn't customize ggplot settings much, but you can make your graphs look like anything you want if you play around with it.Hope this helps!

这篇关于如何绘制LDA的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆