使用R中的“for”循环“手动”保留一次交叉验证

时间:2017-03-23 14:30:28

标签: r logistic-regression glm cross-validation

我正在尝试使用for周期来复制cv.glm()库中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'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??

正如您所看到的,我的问题是在这两种情况下我得不到类似的结果。 我最好的猜测是,在逻辑回归的情况下,我可能会对delta的含义感到困惑。

提前致谢。

0 个答案:

没有答案