I have to run multiple simulations of the models. I am trying to run different simulations on different cores and save the results to global env. I have the below code which isn't working. For example I have to run xgboost three times ( with all same parameters, just 3 re-runs and I want to run the three simulations on different cores and return the imp_df_i to global env.
cores=detectCores()
cl <- makeCluster(cores[1]-1) #not to overload your computer
registerDoParallel(cl)
finalMatrix <- foreach(i=1:3, .combine=cbind) %dopar% {
require(xgboost)
xgb_1 = xgboost(dtrain,
params = xgb_params_1,
nrounds = 3, # max number of trees to build
verbose = TRUE,
print_every_n = 1,
early_stop_round = 10) # stop if no improvement within 10 trees
col_names = attr(tr.x, ".Dimnames")[[2]]
imp = xgb.importance(col_names, xgb_1)
imp_df=data.frame(imp)
assign(paste("imp_df",i, sep = "_"),imp_df,envir = .GlobalEnv )
}
#stop cluster
stopCluster(cl)