本人参考吴喜之先生的《复杂数据统计方法(第二版)》中的5折交叉验证的方法,对自己的数据做5折交叉验证,采用的分类树模型,因变量有51个水平(没办法……),有14个自变量。
在把数据平均分为5份,设立下标集的过程中出现错误:
Error in `*tmp*`[] : subscript out of bounds
查了谷歌,但至今还没明白是什么问题,该怎么完善,希望论坛的个人能帮帮小弟,指点迷津。
详细代码如下:
w <- read.csv('D:/R code/animal2.csv',header = T)
names(w)
w$cluster_ward50 <- as.factor(w$cluster_ward50)
w$VEGETATION <- as.factor(w$VEGETATION)
# prepare to 5-fold cross validation
d <- 1:370827
dd <- list()
Z <- 5
nn <- levels(w$cluster_ward50)
KL <- length(nn)
for(i in 1:KL)
dd[] <- d[w$cluster_ward50==nn]
kk<- NULL
for(i in 1:KL)
kk <- c(kk,round(length(dd[])/Z))
set.seed(1111)
yy <- list(NULL,NULL,NULL)
for(i in 1:KL){xx <- list()
uu<-dd[]
for(j in 1:(Z-1)){xx[[j]] <- sample(uu,kk)
uu<- setdiff(uu,xx[[j]])}
xx[[Z]] <- uu
for(k in 1:Z)
yy[][[k]] <- xx[[k]]}
此处出现:Error in `*tmp*`[] : subscript out of bounds
mm <- list(NULL,NULL,NULL,NULL,NULL)
for(i in 1:Z)
for(j in 1:KL)
mm[] <- c(mm[],yy[[j]][])
此处出现:Error in yy[[j]] : subscript out of bounds
另外,我想用这样的5折交叉验证代码做Adaboost分类、Bagging分类、RandonForest分类等模型,不知道这样的思路是否合理,求各位帮忙解答,谢谢了!


雷达卡





京公网安备 11010802022788号







