简体   繁体   English

如何绘制LDA的结果

[英]how to plot the results of a 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 我有两个问题

I try to simulate a data for you 我尝试为您模拟数据

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")

then I do as follows: 然后我做如下:

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

I end up with groups appearing in two different plots. 我最终得出的组出现在两个不同的情节中。

how to plot my results in one figure like eg Linear discriminant analysis plot Linear discriminant analysis plot using ggplot2 如何绘制我的结果在例如像一个数字线性判别分析图 使用GGPLOT2线性判别分析图

or any other beautiful plot ? 还是其他美丽的情节?

The plot() function actually calls plot.lda(), the source code of which you can check by running getAnywhere("plot.lda"). plot()函数实际上调用plot.lda(),您可以通过运行getAnywhere(“ plot.lda”)来检查其源代码。 This plot() function does quiet a lot of processing of the LDA object that you pass in before plotting. 该plot()函数可以使您在绘制之前传递的LDA对象的许多处理变得安静。 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. 结果,如果要自定义绘图的外观,则可能必须编写自己的函数,该函数从lda对象中提取信息,然后将其传递给绘图功能。 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). 这是一个示例(我对LDA不太了解,因此我只是修剪了默认plot.lda的源代码,并使用ggplot2包(非常灵活)来创建大量图)。

#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()

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. 下面的示例不适用于您的示例,因为您没有LD2,但这等效于您提供的外部示例中的散点图。 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()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM