title: randomForest in R: can fit model and use it for predictions without error, but tuneRF gives diff length error
body: <p>Just messing around with UCI heart disease data: <a href="https://archive.ics.uci.edu/ml/machine-learning-databases/heart-disease/processed.cleveland.data" rel="nofollow noreferrer">https://archive.ics.uci.edu/ml/machine-learning-databases/heart-disease/processed.cleveland.data</a>.  Data is of the format:</p>
  
  <pre><code>A tibble: 6 x 14
      age   sex    cp trestbps  chol   fbs restecg thalach exang oldpeak
    &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;    &lt;int&gt; &lt;int&gt; &lt;dbl&gt;   &lt;int&gt;   &lt;int&gt; &lt;int&gt;   &lt;dbl&gt;
  1    63     1     3      145   233     1       0     150     0     2.3
  2    41     0     1      130   204     0       0     172     0     1.4
  </code></pre>
  
  <p>Growing/fitting the tree on the training set works great, as does using it for predictions on the test set. However, tuneRF gives the error:</p>
  
  <pre><code>Error in randomForest.default(x, y, mtry = mtryStart, ntree = ntreeTry,  : 
    length of response must be the same as predictors 
  </code></pre>
  
  <p>It's R 3.5.0 and randomForest 4.6-14.  </p>
  
  <p>Some notes you'll see in the code:</p>
  
  <p>1) the tuneRF command is using subsets of the same dataset, so the class labels are the same</p>
  
  <p>2) the "target" response variable has been converted to factor before training/test partitioning</p>
  
  <p>I have a feeling it is related to the way I am subsetting, that the results are lists instead of dataframes, maybe?  But I used the same approach for the earlier steps without error.  I found an SO question regarding this before, but can't find it in my history/google now.  Even if I could find it, I don't understand how it applies, since I used the same method of subsetting before without any problem.</p>
  
  <p>Script:</p>
  
  <pre><code>library(tidyverse)
  library(randomForest)
  </code></pre>
  
  <p>I've added the hungarian data, after imputing the missing values (and don't want to use response for imputation) by running:</p>
  
  <pre><code>hungar_heart &lt;- cbind(impute(hungar_heart[,-14]),hungar_heart[,14])
  </code></pre>
  
  <p>I then add colnames to hungar_heart and add it to cleveland data:</p>
  
  <pre><code>hungar_heart&lt;-setNames(hungar_heart, c("age","sex","cp","trestbps","chol","fbs","restecg","thalach","exang","oldpeak","slope","ca","thal","target"))
  heart_total&lt;-rbind(heart_data,hungar_heart)
  
  heart_total$target &lt;- as.factor(heart_total$target)
  
  #Partition new combined dataset into training and test sets after setting seed (123)
  set.seed(123)
  indicator &lt;- sample(2, nrow(heart_total), replace = TRUE, prob = c(.7,.3))
  train &lt;- heart_total[indicator==1,]
  test &lt;- heart_total[indicator==2,]
  
  #Fit random forest to training set, using default values to start.  
  forest &lt;- randomForest(target~., data=train)
  
  #Use trained model on test set
  predict_try &lt;- predict(forest, test)
  
  #so far so good.  now tuneRF gives error:
  
  tune_RF &lt;- tuneRF(train[,-14],train[,14],
     stepFactor = 0.5,
     plot = TRUE,
     ntreeTry = 300,
     improve = 0.05)
  
  Error in randomForest.default(x, y, mtry = mtryStart, ntree = ntreeTry,  : 
  length of response must be the same as predictors
  In addition: Warning message:
  In randomForest.default(x, y, mtry = mtryStart, ntree = ntreeTry,  :
    The response has five or fewer unique values.  Are you sure you want to do regression?
  
  #FWIW, length:
  
  length(train[,-14])
  [1] 13
  
  length(train[,14])
  [1] 1
  </code></pre>
  
  <p>I think it's probably just some uniqueness I didn't expect from my subsetting method.</p>
  
  <p>Thanks</p>