title: LDA cross validation and variable selection
body: <p>I have a data frame with 395 observations and 36 variables. I am doing cross validation to select the best few variables to classify the student qualifications. I have written this code:</p>
  
  <pre><code>k&lt;-5
  error &lt;- c()
  for(l in 1:35){
    if(l!=31 &amp;&amp; l!=32 &amp;&amp; l!=33){
      x&lt;-0
      for (i in 1:k){
        train&lt;-rep(TRUE, dim(student.mat)[1])
        for(j in 1:dim(student.mat)[1]/k){
          train[(i-1)*dim(student.mat)[1]/k+j]&lt;-FALSE
        }
        test=!train
        student.test=student.mat[test,]
        student.train=student.mat[train,]
        nota3.test=nota3[test]
        lda.fit&lt;-lda(nota3~student.mat[,i], data=student.mat, subset=train)
        lda.pred&lt;-predict(lda.fit, student.test)
        table(lda.pred$class, nota3.test)
        y&lt;-mean(lda.pred$class!=nota3.test)
        x&lt;-x+y
        #cat("k = ", i, "error: ", y*100,"%", "\n")
      }
      #cat("Media del error = ", x/k*100,"%", "\n")
      error &lt;- c(error, x/k)
    }else{
      error &lt;- c(error, 100)
    }
  }
  error
  names(student.mat)[which.max(error)]
  </code></pre>
  
  <p>and I get this error:</p>
  
  <p><strong>Error in table(lda.pred$class, nota3.test) : 
    all arguments must have the same length
  Also: lost warning messages
  'newdata' had 79 rows but variables found have 395 rows</strong></p>
  
  <p>but if I write the name of one variable of the data set instead of <code>student.mat[,i]</code>, it works.
  The lda function don't read <code>student.mat[,i]</code> correctly.</p>