title: Train test split in `r`'s `caret` package
body: <p>I'm getting familiar with <code>r</code>'s <code>caret</code> package, but, coming from other programming language, it thorougly confused me.</p>
  
  <p>What I want to do now is a fairly simple machine learning workflow, which is:</p>
  
  <ol>
  <li>Take a training set, in my case the iris dataset </li>
  <li>Split it into a training and test set (a 80-20 split)</li>
  <li>For every <code>k</code> from <code>1</code> to <code>20</code>, train the <code>k</code> nearest neighbor classifier on the training set</li>
  <li>Test it on the test set</li>
  </ol>
  
  <p>I understand how to do the first part, since <code>iris</code> is already loaded. Then, the second part is done by calling</p>
  
  <pre><code>a &lt;- createDataPartition(iris$Species, list=FALSE)
  training &lt;- iris[a,]
  test &lt;- iris[-a,]
  </code></pre>
  
  <p>Now, I also know that I can train the model by calling</p>
  
  <pre><code>library(caret)
  knnFit &lt;- train()
  knnFit &lt;- train(Species~., data=training, method="knn")
  </code></pre>
  
  <p>However, this will result in <code>r</code> already performing some optimisation on the parameter <code>k</code>. Of course, I can limit what values of <code>k</code> the method should try, with something like</p>
  
  <pre><code>knnFit &lt;- train(Species~., data=training, method="knn", tuneGrid=data.frame(k=1:20))
  </code></pre>
  
  <p>which works just fine, but it still doesn't to exactly what I want it to do. This code will now do, for each <code>k</code>:</p>
  
  <ol>
  <li>take a bootstrap sample from the <code>test</code>.</li>
  <li>Asses the performance of the <code>k</code>-nn method using the given sample</li>
  </ol>
  
  <p>What I want it to do:</p>
  
  <ol>
  <li>For each <code>k</code>, train the model <strong>on the same train set which I constructed earlier</strong></li>
  <li>Asses the performance **on the same test set which I constructed earlier.</li>
  </ol>
  
  <p>So I would need something like</p>
  
  <pre><code>knnFit &lt;- train(Species~., training_data=training, test_data=test, method="knn", tuneGrid=data.frame(k=1:20))
  </code></pre>
  
  <p>but this of course does not work.</p>
  
  <p>I understand I should do something with the <code>trainControl</code> parameter, but I see its possible methods are:</p>
  
  <pre><code>"boot", "boot632", "cv", "repeatedcv", "LOOCV", "LGOCV", "none"
  </code></pre>
  
  <p>and none of these seems to do what I want.</p>