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

ale*_*ulo 5 r

我使用lda()MASS包中的函数进行了线性判别分析.现在我将尝试绘制一个像ade4包(forLDA)中的双标图.你知道我怎么能这样做吗?

如果我尝试使用该biplot()功能它不起作用.例如,如果我使用Iris数据并生成LDA:

dis2 <- lda(as.matrix(iris[, 1:4]), iris$Species)
Run Code Online (Sandbox Code Playgroud)

然后我可以使用该函数绘制它plot(),但如果我使用该函数biplot()它不起作用:

biplot(dis2)
Error in nrow(y) : argument "y" is missing, with no default
Run Code Online (Sandbox Code Playgroud)

如何绘制变量的箭头?

Tyl*_*ler 4

我编写了以下函数来执行此操作:

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)
}
Run Code Online (Sandbox Code Playgroud)

对于你的例子:

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

箭头的长度相对于 lda 图是任意的(当然,箭头之间的长度不是任意的!)。如果您想要更长或更短的箭头,请相应地更改 的值myscale。默认情况下,这会绘制第一轴和第二轴的箭头。如果您想绘制其他轴,请进行更改choices以反映这一点。