【问题标题】:how to plot the results of a LDA如何绘制 LDA 的结果
【发布时间】:2015-03-01 08:06:50
【问题描述】:

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

我试着为你模拟一个数据

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)

我最终在两个不同的情节中出现了组。

如何将我的结果绘制在一个图中,例如Linear discriminant analysis plot Linear discriminant analysis plot using ggplot2

或任何其他美丽的情节?

【问题讨论】:

  • 因为你只有两组,所以只计算一个LD。在您的示例图中,计算了两个 LD,因此将一个映射到 x 轴,一个映射到 y 轴是有意义的。对于两组,您只能得到 LD1,因此绘制了单变量条形图。你想要在每个轴上为你想要的情节做什么?

标签: r lda


【解决方案1】:

plot() 函数实际上调用了 plot.lda(),您可以通过运行 getAnywhere("plot.lda") 来查看其源代码。这个 plot() 函数确实在绘图之前对您传入的 LDA 对象进行了很多处理。因此,如果您想自定义绘图的外观,您可能必须编写自己的函数,从 lda 对象中提取信息,然后将其传递给绘图函数。这是一个例子(我对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()

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

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

我并没有过多地自定义 ggplot 设置,但是如果您使用它,您可以使您的图表看起来像您想要的任何东西。希望这会有所帮助!

【讨论】:

  • 你的情节有问题。请看你的数据!您在显示三个班级的情节中有 2 个班级(a 和 b)!奇怪!!!!
  • 我意识到我遗漏了一行代码。现在应该可以工作了。关于情节上的 3 个类,那是因为最后一次 ggplot 调用使用了不同的示例。我现在把它移到一个单独的代码部分。另外请逐行运行代码(或滚动生成的图,因为每个 ggplot 行都覆盖了前一个)
  • 你应该使用类似的东西,例如,ggplot(fitGraph, aes(LD1,fill=labels))+geom_histogram(colour = "darkgreen", fill = "white", binwidth = 0.5) .
猜你喜欢
  • 2012-03-17
  • 2017-01-20
  • 1970-01-01
  • 1970-01-01
  • 2013-04-26
  • 1970-01-01
  • 2016-01-24
  • 2018-06-02
  • 1970-01-01
相关资源
最近更新 更多