如何绘制LDA的结果

时间:2015-02-28 12:49:48

标签: r lda

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

我尝试为您模拟数据

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

还是其他任何美丽的情节?

1 个答案:

答案 0 :(得分:2)

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设置,但如果你玩它,你可以让你的图形看起来像你想要的任何东西。希望这有帮助!