K = 5를 사용하여 K- 배 CV 회귀 모델을 만들려고합니다. "boot"패키지 cv.glm 함수를 사용해 보았습니다.하지만 부트 패키지가 항상 옆에있는 LOOCV MSE를 계산하기 때문에 PC가 메모리 부족했습니다. 그래서 수동으로하기로 결정했지만 다음과 같은 문제가 발생했습니다. 내 데이터 프레임을 5 개의 벡터로 나눕니다. 예를 들어 1/5의 내 df를 포함하는 벡터를 만들었지 만 설명 할 수없는 길이는 3 번째 배가됩니다.K- 폴 교차 유효성 검사를 위해 수동으로 폴드 만들기 R
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으로부터 증가하는 것으로 보인다. 아무도 내가 뭘 잘못 설명 할 수 있습니까?! 내 코드 (및 추론)는 논리적 인 것처럼 보이지만 그렇지 않은 경우 결과가 나타납니다. 미리 감사드립니다.
K1row와 K2row에는 공통된 요소가 있기 때문입니다. 대체품으로 효과적으로 샘플링하고 있습니다. – ddunn801