-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCV_RF.R
32 lines (26 loc) · 950 Bytes
/
CV_RF.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#####################################################################
# code to train RF
#####################################################################
library(randomForest)
library(foreach)
library(doMC)
registerDoMC(nb_cores)
set.seed(seed)
out<-list()
out$fit<-list()
nb_folds<-length(K)
for (i in 1:nb_folds)
eval(parse(text=paste("x_",i,"<-if (is.data.frame(x)) x else x[[i]]",sep="")))
out$fit <- foreach (k = 1:nb_folds) %dopar% {
set.seed(seed+k)
eval(parse(text=paste("randomForest(x=x_",k,"[-K[[k]],],y=y[-K[[k]]],
ntree=RF_ntree,mtry=RF_mtry,maxnodes=RF_maxnodes,
nodesize=RF_nodesize,importance=RF_importance,replace=F,
do.trace=F)",sep="")))
}
out$yhatV<-rep(0,length(y))
for (i in 1:length(K)) {
eval(parse(text=paste("
out$yhatV[K[[i]]]<-predict(out$fit[[i]],x_",i,"[K[[i]],])",sep="")))
rm(list=paste("x_",i,sep=""))
}