【问题标题】:LDA cross validation and variable selectionLDA 交叉验证和变量选择
【发布时间】:2015-06-25 14:16:13
【问题描述】:

我有一个包含 395 个观察值和 36 个变量的数据框。我正在进行交叉验证以选择最好的几个变量来对学生资格进行分类。我写了这段代码:

k<-5
error <- c()
for(l in 1:35){
  if(l!=31 && l!=32 && l!=33){
    x<-0
    for (i in 1:k){
      train<-rep(TRUE, dim(student.mat)[1])
      for(j in 1:dim(student.mat)[1]/k){
        train[(i-1)*dim(student.mat)[1]/k+j]<-FALSE
      }
      test=!train
      student.test=student.mat[test,]
      student.train=student.mat[train,]
      nota3.test=nota3[test]
      lda.fit<-lda(nota3~student.mat[,i], data=student.mat, subset=train)
      lda.pred<-predict(lda.fit, student.test)
      table(lda.pred$class, nota3.test)
      y<-mean(lda.pred$class!=nota3.test)
      x<-x+y
      #cat("k = ", i, "error: ", y*100,"%", "\n")
    }
    #cat("Media del error = ", x/k*100,"%", "\n")
    error <- c(error, x/k)
  }else{
    error <- c(error, 100)
  }
}
error
names(student.mat)[which.max(error)]

我得到这个错误:

表中的错误(lda.pred$class, nota3.test): 所有参数必须具有相同的长度 另外:丢失警告消息 'newdata' 有 79 行,但找到的变量有 395 行

但是如果我写数据集的一个变量的名称而不是student.mat[,i],它就可以工作。 lda 函数无法正确读取student.mat[,i]

【问题讨论】:

  • 最近似乎有很多涉及 R 的 LDA 问题。有没有分配分类问题的课程?
  • 这是我大学学位的科目

标签: r machine-learning dataframe cross-validation lda


【解决方案1】:

您可以通过编程方式创建公式:

lda.fit<-lda(paste0("nota3~", names(student.mat)[i]), data=student.mat, subset=train)

【讨论】:

  • 它不起作用,我收到此错误:Error en lda.default(paste0("nota3~", names(student.mat)[i]), data = student.mat, : 'x' 不是矩阵
猜你喜欢
  • 2017-10-23
  • 2016-05-26
  • 2013-11-08
  • 2013-08-24
  • 2016-09-01
  • 2020-08-29
  • 2018-04-02
  • 2020-10-04
  • 1970-01-01
相关资源
最近更新 更多