Nested loops - Random Forest, multiple parameters
Reproducible Example

Unfortunately, the code snippet that you gave does not lend itself to being reproducible. So, the advice being given is constrained.

Caches are nice

There are certain times where you should be caching a summation if the value is known to be constant through different iterations. In this particular case, we have: sum(train.target) and sum(TARGET3) that should be cached. Say:

stt = sum(train.target)
st3 = sum(TARGET3)


Knowledge (of size) is Power!

Immediately, one of the key issue you will face is the fact that you are rbind 60 items since you avoid giving stable numerical entries in the data.frame

## parameteres

ntrees &lt;- c(300, 500)
zeros &lt;- sum(train.target) * c(1, 2, 3, 4, 5)
mvars &lt;- c(30, 50, 70, 90, 110, 130)

nitr = length(ntrees)*length(zeros)*length(mvars)

model_eff &lt;- data.frame("ntrees" = numeric(nitr),
                        "zeros"  = numeric(nitr), 
                        "mvars"  = numeric(nitr),
                        "eff"    = numeric(nitr),
                        "0_0"    = numeric(nitr),
                        "0_1"    = numeric(nitr),
                        "1_0"    = numeric(nitr),
                        "1_1"    = numeric(nitr),
                        "predict_sum" = numeric(nitr),
                        "triangle"    = numeric(nitr),
                        stringsAsFactors = F)


Declare count = 1 before the 3x for loops. Then save results using:

model_eff[count,] = c("ntrees" = ntrees[j],
                      "zeros" = zeros[k],
                      "mvars"= mvars[l],
                      "eff" = eff_measures$eff,
                      "0_0" = eff_measures$c.m[1, 1],
                      "0_1" = eff_measures$c.m[1, 2],
                      "1_0" = eff_measures$c.m[2, 1],
                      "1_1" = eff_measures$c.m[2, 2],
                      "predict_sum" = st3 ,
                      "triangle" = eff_measures$triangle))
count = count + 1


Parallel RandomForest via caret

The only other suggestion I have it to parallelize the build of the random forest via:

# caret modeling framework
library(caret)

# Parallel backend
library(doParallel)

# Register a cluster
registerDoParallel(cores = 5)

rf_model = train(train.target~.,data=train,method="rf",
                 prox=TRUE,allowParallel=TRUE)