Leave "One Out Cross Validation" "manually" using the "for" loop in R

I am trying to use a loop for

to reproduce the behavior of a function cv.glm()

in a library boot

:

library(ISLR)
attach(Weekly) # this has 1089 rows

cutoff <- .5 # determines the prediction

wrong <- 0 # we'll record the number of wrong predictions here
for (i in 1:1089) {
  fit <- glm(Direction~Lag1+Lag2, data=Weekly[-i,], family=binomial) # leaves out the i-th observation
  prob <- predict(fit,Weekly[i,],type="response")
  if (prob<cutoff) pred <- "Down" else pred <- "Up"
  if (pred!=Direction[i]) wrong <- wrong +1
}

wrong / 1089 # approximately 45% LOOCV error rate

# Let try the same with the cv.glm()
library(boot)
cv.glm(Weekly,glm(Direction~Lag1+Lag2, data=Weekly, family=binomial))$delta # approximately 25%, why??

      

As you can see, my problem is that I don't get similar results in two cases. My best guess is that I might confuse the meaning delta

in the case of logistic regression.

Thanks in advance.

+3


source to share





All Articles