我正在尝试使用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
的含义感到困惑。
提前致谢。