I need to speed up the for loop through multithreading. I would like to use the libraries for this: foreach and doParallel. I used these packages before but only for processes where one result table was needed. I don't know how to use them to export multiple tables (here results tables). My problem is much more complex and requires exporting many result sets. Here, for simplicity, I use iris data.
library(randomForest)
library(caret)
results_class <- data.frame()
results_overall <- data.frame()
for(i in 1:50){
trainIndex <- caret::createDataPartition(iris$Species, p = 0.5, list = FALSE)
irisTrain <- iris[ trainIndex,]
irisTest <- iris[-trainIndex,]
model <- randomForest(x = irisTrain[,c(1:4)], y = irisTrain[,5], importance = TRUE,
replace = TRUE, mtry = 4, ntree = 500, na.action=na.omit,
do.trace = 100, type = "classification")
pred_test <- predict(model, irisTest[,c(1:4)])
con.mat_test <- confusionMatrix(pred_test, irisTest[,5], mode ="everything")
results_class <- rbind(results_class, con.mat_test[["byClass"]])
results_overall <- rbind(results_overall, con.mat_test[["overall"]])
}