手动创建折叠以进行K折交叉验证R.

时间:2016-11-04 14:37:40

标签: r cross-validation

我正在尝试使用K = 5制作K-fold CV回归模型。我尝试使用" boot"包cv.glm函数,但我的电脑内存不足,因为启动包总是在它旁边计算一个LOOCV MSE。所以我决定手动完成,但我遇到了以下问题。我尝试将我的数据帧划分为5个相等长度的矢量,其中包含我的df的1/5 rownumbers的样本,但是我从第3个折叠中获得无法解释的长度。

a <- sample((d<-1:1000), size = 100, replace = FALSE)
b <- sample((d<-1:1000), size = 100, replace = FALSE)
c <- sample((d<-1:1000), size = 100, replace = FALSE)
df <- data.frame(a,b,c)
head(df)

# create first fold (correct: n=20)
set.seed(5)
K1row <- sample(x = nrow(df), size = (nrow(df)/5), replace = FALSE, prob = NULL)
str(K1row) # int [1:20] 21 68 90 28 11 67 50 76 88 96 ...

# create second fold (still going strong: n=20)
set.seed(5)
K2row <- sample(x = nrow(df[-K1row,]), size = ((nrow(df[-K1row,]))/4), replace = FALSE, prob = NULL)
str(K2row) # int [1:20] 17 55 72 22 8 53 40 59 69 76 ...

# create third fold (this is where it goes wrong: n=21)
set.seed(5)
K3row <- sample(x = nrow(df[-c(K1row,K2row),]), size = ((nrow(df[-c(K1row,K2row),]))/3), replace = FALSE, prob = NULL)
str(K3row) # int [1:21] 13 44 57 18 7 42 31 47 54 60 ...

# create fourth fold (and it gets worse: n=26)
set.seed(5)
K4row <- sample(x = nrow(df[-c(K1row,K2row,K3row),]), size = ((nrow(df[-c(K1row,K2row,K3row),]))/2), replace = FALSE, prob = NULL)
str(K4row) # int [1:26] 11 35 46 14 6 33 25 37 43 5 ...

矢量长度似乎从K = 3增加。谁能向我解释我做错了什么?!我的代码(和推理)似乎合乎逻辑,但结果却另有说明......我非常感谢提前!

1 个答案:

答案 0 :(得分:1)

因为K1row和K2row有一些共同的元素。你有效地采样替换。下面的方法使用modulo来均匀地分割行。

set.seed(5)
rand <- sample(nrow(df))

K1row <- rand[rand %% 5 + 1 == 1]
K2row <- rand[rand %% 5 + 1 == 2]
K3row <- rand[rand %% 5 + 1 == 3]
K4row <- rand[rand %% 5 + 1 == 4]
K5row <- rand[rand %% 5 + 1 == 5]