title: Leave One Out Cross Validation "by hand" using a "for" cycle in R
body: <p>I'm trying to use a <code>for</code> cycle in order to replicate the behavior of the <code>cv.glm()</code> function in the <code>boot</code> library:</p>
  
  <pre><code>library(ISLR)
  attach(Weekly) # this has 1089 rows
  
  cutoff &lt;- .5 # determines the prediction
  
  wrong &lt;- 0 # we'll record the number of wrong predictions here
  for (i in 1:1089) {
    fit &lt;- glm(Direction~Lag1+Lag2, data=Weekly[-i,], family=binomial) # leaves out the i-th observation
    prob &lt;- predict(fit,Weekly[i,],type="response")
    if (prob&lt;cutoff) pred &lt;- "Down" else pred &lt;- "Up"
    if (pred!=Direction[i]) wrong &lt;- wrong +1
  }
  
  wrong / 1089 # approximately 45% LOOCV error rate
  
  # Let's try the same with the cv.glm()
  library(boot)
  cv.glm(Weekly,glm(Direction~Lag1+Lag2, data=Weekly, family=binomial))$delta # approximately 25%, why??
  </code></pre>
  
  <p>As you can see, my problem is that I don't get similar results in the two cases.
  My best guess is that I might be confused about the meaning of <code>delta</code> in the case of a logistic regression.</p>
  
  <p>Thanks in advance.</p>