title: How to predict in kknn function? library(kknn)
body: <p>I try to use <code>kknn</code> + loop to create a leave-out-one cross validation for a model, and compare that with train.kknn.</p>
  
  <p>I have split the data into two parts: training (80% data), and test (20% data). In the training data, I exclude one point in the loop to manually create LOOCV.</p>
  
  <p>I think something gets wrong in <code>predict(knn.fit, data.test)</code>. I have tried to find how to predict in <code>kknn</code> through the kknn package instruction and online but all the examples are "summary(model)" and "table(validation...)" rather than the prediction on a separate test data. The code <code>predict(model, dataset)</code> works successfully in <code>train.kknn</code> function, so I thought I could use the similar arguments in kknn.</p>
  
  <p>I am not sure if there is such a prediction function in <code>kknn</code>. If yes, what arguments should I give? </p>
  
  <p>Look forward to your suggestion. Thank you.</p>
  
  <pre class="lang-r prettyprint-override"><code>library(kknn)
  for (i in 1:nrow(data.train)) {
      train.data &lt;- data.train[-i,]
      validation.data &lt;- data.train[i,]
      knn.fit &lt;- kknn(as.factor(R1)~., train.data, validation.data, k = 40,
                      kernel = "rectangular", scale = TRUE)
      # train.data + validation.data is the 80% data I split.
  }
  
  pred.knn &lt;- predict(knn.fit, data.test) # data.test is 20% data.
  
  </code></pre>
  
  <p>Here is the error message:</p>
  
  <blockquote>
    <p>Error in switch(type, raw = object$fit, prob = object$prob,
    stop("invalid type for prediction")) :    EXPR must be a length 1
    vector</p>
  </blockquote>
  
  <p>Actually I try to compare train.kknn and kknn+loop to compare the results of the leave-out-one CV. I have two more questions: </p>
  
  <p>1) in <code>kknn</code>: is it possible to use another set of data as test data to see the knn.fit prediction?  </p>
  
  <p>2) in <code>train.kknn</code>: I split the data and use 80% of the whole data  and intend to use the rest 20% for prediction. Is it an correct common practice?</p>
  
  <p>2) Or should I just use the original data (the whole data set) for <code>train.kknn</code>, and create a loop: <code>data[-i,]</code> for training, <code>data[i,]</code> for validation in <code>kknn</code>? So they will be the counterparts? </p>
  
  <p>I find that if I use the training data in the <code>train.kknn</code> function and use prediction on test data set, the best k and kernel are selected and directly used in generating the predicted value based on the test dataset. </p>
  
  <p>In contrast, if I use <code>kknn</code> function and build a loop of different k values, the model generates the corresponding prediction results based on
  the test data set each time the k value is changed. Finally, in <code>kknn</code> + loop, the best k is selected based on the best actual prediction accuracy rate of test data. In short, the best k <code>train.kknn</code> selected may not work best on test data.</p>
  
  <p>Thank you. </p>