在R中绘制lda-object

时间:2017-03-30 19:32:40

标签: r plot lda

我正在尝试绘制使用lda包的MASS函数创建的lda对象。

我使用的数据在这里: https://pastebin.com/yjfmsE3F

我目前的代码是:

types <- c('GO', 'GO', 'GO', 'GO', 'GO', 'OO', 'OR', 'OO', 'OO', 'GO', 'GR', 'OO', 'OO', 'GO')
bacDummy <- t(bacCountData)
bacDummy2 <- decostand(bacDummy, "freq")
bacDummy3 <- cbind(types, bacDummy2)
colnames(bacDummy3)[1] <- "Site"
bacDummy4 <- as.data.frame(bacDummy3)

decostand包含vegan

然后我继续尝试使用lda - 函数:

bac.lda <- lda(bacDummy4[,2:170], grouping=types)

这给了我以下错误:

Error in lda.default(x, grouping, ...) : 
  variables   4  53 114 138 appear to be constant within groups

然后我删除了令人不安的栏目(尽管我知道这是肮脏的工作):

bacDummy4 <- bacDummy4[,-139]
bacDummy4 <- bacDummy4[,-115]
bacDummy4 <- bacDummy4[,-54]
bacDummy4 <- bacDummy4[,-5]

这让我做LDA,但警告:In lda.default(x, grouping, ...) : variables are collinear

当我尝试在散点图中绘制组时,我收到以下错误:

plot(bac.lda)
Error in FUN(x, aperm(array(STATS, dims[perm]), order(perm)), ...) : 
  non-numeric argument to binary operator

我做错了什么?如果我str(bac.lda)我得到了这个输出:

List of 8
 $ prior  : Named num [1:4] 0.5 0.0714 0.3571 0.0714
  ..- attr(*, "names")= chr [1:4] "GO" "GR" "OO" "OR"
 $ counts : Named int [1:4] 7 1 5 1
  ..- attr(*, "names")= chr [1:4] "GO" "GR" "OO" "OR"
 $ means  : num [1:4, 1:165] 6.14 1 2.2 5 2.14 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:4] "GO" "GR" "OO" "OR"
  .. ..$ : chr [1:165] "ReferenceOTU1484" "ReferenceOTU893" "ReferenceOTU1445" "ReferenceOTU623" ...
 $ scaling: num [1:165, 1:3] -0.01649 -0.03767 -0.00607 0.04141 -0.03966 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:165] "ReferenceOTU1484" "ReferenceOTU893" "ReferenceOTU1445" "ReferenceOTU623" ...
  .. ..$ : chr [1:3] "LD1" "LD2" "LD3"
 $ lev    : chr [1:4] "GO" "GR" "OO" "OR"
 $ svd    : num [1:3] 4.54 3.06 2.03
 $ N      : int 14
 $ call   : language lda(x = bacDummy4[, -1], grouping = bacDummy4$Site)
 - attr(*, "class")= chr "lda"

非常感谢所有帮助!

1 个答案:

答案 0 :(得分:1)

您应该注意在每次转换期间仔细跟踪变量的数据类型。我遇到了您提供的代码的一些问题。这似乎有效

bacCountData <- read.csv("https://pastebin.com/raw/yjfmsE3F", header=T, row.names=1)
types <- c('GO', 'GO', 'GO', 'GO', 'GO', 'OO', 
    'OR', 'OO', 'OO', 'GO', 'GR', 'OO', 'OO', 'GO')

bacDummy <- t(bacCountData)
bacDummy2 <- vegan::decostand(bacDummy, "freq")
bacDummy3 <- data.frame(types=types, bacDummy2)
colnames(bacDummy3)[1] <- "Site"

bac.lda <- MASS::lda(bacDummy3[,-c(1, 5, 54, 115, 139)], grouping=types)
plot(bac.lda)

并产生以下情节

enter image description here