I do not understand the mtry parameter of rfcv() of randomForest package in R:

rfcv(trainx, trainy, cv.fold=5, scale = "log", step = 0.5,
     mtry = function(p) max(1, floor(sqrt(p))), recursive = FALSE, ...)

The randomForest package explains mtry as follows:

A function of number of remaining predictor variables to use as the mtry parameter in the randomForest call.

For my understanding, if beginning 64 variables, then the mtry used in the randomForest call is 8; the next one is 32 variables because of step = 0.5, then mtry is 5. I wonder if my understanding is right or wrong?


1 Answer 1


The default function for mtry is max(1, floor(sqrt(p))). So if p = 64, the sequence of variables used is 8, 5, 4, 2, 2, 1, assuming you take the scale default which is log and step for 0.5.

The code is

p <- ncol(trainx)
k <- floor(log(p, base = 1/step))
n.var <- round(p * step^(0:(k - 1)))
sub.rf <- randomForest(trainx[idx != i, imp.idx, 
            drop = FALSE], trainy[idx != i], trainx[idx == 
            i, imp.idx, drop = FALSE], trainy[idx == i], 
        --> mtry = mtry(n.var[j]), importance = recursive, 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.