2
$\begingroup$

I am using the randomForest package in R with categorical co-variates. The documentation advises against the formula interface for large data. Following this advice, I prepare the outcome variable and a model.matrix object, M.

My question is does the algorithm treat the columns of M independently, so the mtry variables chosen may be biased to selecting from a factor with many levels? Or is the original factor (available via terms(M)) used when selecting the mtry variables?

I am familiar with cforest and the papers by Strobl et al. that address the bias in randomForest based on the domain of the variables. My question is does the choice of arguments (formula vs matrix) in randomForest exacerbate this bias.

thanks.

$\endgroup$

1 Answer 1

3
$\begingroup$

tl;dr

randomForest will include and exclude dummy-encoded columns independently if using a model matrix, and together if using a data.frame and formula. So yes, the treatment is different (better?) using the formula interface.

Evidence

randomForest under the hood doesn't break categorical variables into multiple columns. It encodes them as one numeric column with their factor code (using data.matrix). It uses ncat/cat internally to keep track of which variables are numeric and categorical.

For matrix input, this just gets set to 'numeric' for every variable. mtry then causes the sampling of each column with equal probability.

$\endgroup$
2
  • 2
    $\begingroup$ Thanks for the source links. This is a rather critical issue that should be called out in the documentation. $\endgroup$
    – Chris
    Commented Nov 8, 2016 at 16:23
  • $\begingroup$ How could it be possible to jump over the less than 53 categories? Using model.matrix is an option? $\endgroup$
    – vog
    Commented Feb 6, 2023 at 17:02

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.