我使用包MASS中的函数lda()进行了线性判别分析。现在,我将尝试像ade4包(forLDA)中那样绘制一个双线图。你知道我该怎么做吗?
如果我尝试使用biplot()函数,它不起作用。例如,如果我使用Iris数据并进行LDA:
dis2 <- lda(as.matrix(iris[, 1:4]), iris$Species)然后我可以使用函数plot()绘制它,但是如果我使用函数biplot(),它就不起作用了:
biplot(dis2)
Error in nrow(y) : argument "y" is missing, with no default如何绘制变量的箭头?
发布于 2013-06-22 01:01:19
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)
}对于您的示例:
dis2 <- lda(as.matrix(iris[, 1:4]), iris$Species)
plot(dis2, asp = 1)
lda.arrows(dis2, col = 2, myscale = 2)箭头的长度相对于lda图是任意的(当然,不是彼此之间的!)。如果需要更长或更短的箭头,请相应地更改myscale的值。默认情况下,这将绘制第一个和第二个轴的箭头。如果要绘制其他轴,请更改choices以反映这一点。
发布于 2015-08-07 00:35:48
我的理解是线性判别分析的双线图是可以做的,它实际上也是在R包ggbiplot中实现的,参见https://github.com/vqv/ggbiplot/tree/experimental和包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)

此外,M. Greenacre的书"Biplots in practice“有一章(第11章)介绍了它,在图11.5中,它显示了虹膜数据集的线性判别分析的双线图:

发布于 2018-05-25 03:50:38
您可以使用github的ggord包来实现这一点。使用的数据集是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))https://stackoverflow.com/questions/17232251
复制相似问题