title: LOOCV, Confusion Matrix and KNN in R
body: <p>I am trying to write my own function of KNN. I do not use the built function in R, because I want to use different distances (norms, such as L_0.1) instead of Euclidean distance. In addition, I would like to use LOOCV to separate the dataset. Previously, I separated my data as in below code and everything goes well.But I need to use LOOCV. </p>
  
  <pre><code>wdbc&lt;- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data",sep=",",stringsAsFactors = FALSE)
       wdbc&lt;-wdbc[-1]
       normalize &lt;- function(x) {
         return ((x-min(x)) / (max(x) - min(x)))}
       wdbc_n &lt;- as.data.frame(lapply(wdbc[2:31], normalize))
       wdbc_train&lt;-wdbc_n[1:568,]
       wdbc_test&lt;-wdbc_n[569:569,]
  </code></pre>
  
  <p>I have also try to write the LOOCV code.</p>
  
  <pre><code>LOOOCV_training=list()
  for(i in 1:nrow(wdbc_n)){
    LOOOCV_training[[i]]=wdbc_n[-i,]}
  View(LOOOCV_training)
  
  LOOOCV_testing=list()
  for(i in 1:nrow(wdbc_n)){
    LOOOCV_testing[[i]]=wdbc_n[i,]}
  </code></pre>
  
  <p>My questions </p>
  
  <p>How to put LOOCV separation instead of the separation I did before?
  In other words, I used some codes such as <code>Dist=matrix(0,nrow=nrow(wdbc_train),ncol=nrow(wdbc_test))</code> 
  It was easy to deal with the first split I did before, but now I am not sure because of LOOCV, train the model on all training set except on one test set and repeat this process n times. The dimension of my data is 569 32, that means not easy to do the process one by one.</p>
  
  <p>Also, how to calculate confusion matrix with LOOCV, since LOOCV repeated the process many times, which means I will have many confusion matrices!!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>To be more clear,suppose that I want to use LOOCV split instead in the following code (previous codes).</p>
  
  <pre><code>Dist=matrix(0,nrow=nrow(wdbc_train),ncol=nrow(wdbc_test))
  Dist2=array(0,nrow(wdbc_test))
  for (i in 1:nrow(wdbc_train)){
    for (j in 1:nrow(wdbc_test)){
      Dist[i,j]=norm(as.matrix(wdbc_train[i,]-wdbc_test[j,]),type="i")}}
  </code></pre>