简体   繁体   English

如何在r中绘制LDA的双标图?

[英]How can I plot a biplot for LDA in r?

I did a linear discriminant analysis using the function lda() from the package MASS. 我使用MASS包中的函数lda()进行了线性判别分析。 Now I would try to plot a biplot like in ade4 package (forLDA). 现在我将尝试绘制一个像ade4包(forLDA)中的双标图。 Do you know how can I do this? 你知道我怎么能这样做吗?

If I try to use the biplot() function it doesn't work. 如果我尝试使用biplot()函数它不起作用。 For example, if I use the Iris data and make LDA: 例如,如果我使用Iris数据并生成LDA:

dis2 <- lda(as.matrix(iris[, 1:4]), iris$Species)

then I can plot it using the function plot() , but if I use the function biplot() it doesn't work: 然后我可以使用函数plot()绘制它,但如果我使用函数biplot()它不起作用:

biplot(dis2)
Error in nrow(y) : argument "y" is missing, with no default

How can I plot the arrows of variables? 如何绘制变量的箭头?

I wrote the following function to do this: 我写了以下函数来做到这一点:

lda.arrows <- function(x, myscale = 1, tex = 0.75, choices = c(1,2), ...){
  ## adds `biplot` arrows to an lda using the discriminant function values
  heads <- coef(x)
  arrows(x0 = 0, y0 = 0, 
         x1 = myscale * heads[,choices[1]], 
         y1 = myscale * heads[,choices[2]], ...)
  text(myscale * heads[,choices], labels = row.names(heads), 
    cex = tex)
}

For your example: 对于你的例子:

dis2 <- lda(as.matrix(iris[, 1:4]), iris$Species)
plot(dis2, asp = 1)
lda.arrows(dis2, col = 2, myscale = 2)

The length of the arrows is arbitrary relative to the lda plot (but not to each other, of course!). 箭头的长度相对于lda图是任意的(当然不是彼此!)。 If you want longer or shorter arrows, change the value of myscale accordingly. 如果您想要更长或更短的箭头,请相应地更改myscale的值。 By default, this plots arrows for the first and second axes. 默认情况下,这会绘制第一个和第二个轴的箭头。 If you want to plot other axes, change choices to reflect this. 如果要绘制其他轴,请更改choices以反映此情况。

My understanding is that biplots of linear discriminant analyses can be done, it is implemented in fact also in R package ggbiplot, see https://github.com/vqv/ggbiplot/tree/experimental and package ggord, see https://github.com/fawda123/ggord , for your example: 我的理解是可以完成线性判别分析的双线性,它实际上也在R package ggbiplot中实现,参见https://github.com/vqv/ggbiplot/tree/experimental和package ggord,参见https:// github .com / fawda123 / ggord ,举个例子:

install.packages("devtools")
library(devtools)
install_github("fawda123/ggord")
library(ggord)
ord <- lda(Species ~ ., iris, prior = rep(1, 3)/3)
ggord(ord, iris$Species)

在此输入图像描述

Also the book "Biplots in practice" by M. Greenacre has one chapter (chapter 11) on it and in Figure 11.5 it shows a biplot of a linear discriminant analysis of the iris dataset: M. Greenacre的“实践中的Biplots”一书中有一章(第11章),图11.5显示了虹膜数据集的线性判别分析的双标图: 在此输入图像描述

You can achieve this using the ggord package from github. 您可以使用github中的ggord包实现此目的。 The dataset used is IRIS dataset 使用的数据集是IRIS数据集

# --- 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,]    

# --- Prediction --- #
p<- predict(IR.lda, IRTrain)

# --- plotting a biplot --- #
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))

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

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