Cross Validation on Random Forest

r
machine_learning
crossvalidation
random_forest
data_science

#1

Hi all,
I have a data frame which a part of that looks like the following and I want to do classification for Alzheimer and Control group with random forest and then do a 5fold cross validation:
str(data)

‘data.frame’: 499 obs. of 606 variables:
$ Gender : int 0 0 0 0 0 1 1 1 1 1 …

$ NumOfWords : num 157 111 163 176 100 124 201 100 76 101

$ NumofLivings : int 6 6 9 4 3 5 3 3 4 3 …

$ NumofStopWords: num 77 45 87 91 46 64 104 37 32 41 …

$ Group : Factor w/ 2 levels “Alzheimer”,“Control”,“Control”…:

I created the 5 fold cross validation algorithm as the following:
k <- 5
folds <- cvFolds(NROW(data), K=k)
data$holdoutpred <- rep(0,nrow(data))
for(i in 1:k){
train <- data[folds$subsets[folds$which != i], ]
validation <- data[folds$subsets[folds$which == i], ]
modelFit <- randomForest(Group~.,data=train,importance=TRUE,ntree=200)
newpred <- predict(modelFit,newdata=validation)
data[folds$subsets[folds$which == i], ]$holdoutpred <- newpred
}

data $holdoutpred
now my question is how can I add a line to this algorithm to show the accuracy in each fold?

Thanks for any help!
Eli