I am working on the Random Forest prediction, with the focus on the importance of predictor variables, and have a question regarding understanding of mtry and the actual usage of variables in the trees of Random Forest in R (package randomForest).
I have a sample with over 1000 observations and a response vector with classification into 2 classes: 0 and 1. I have 4 independent variables. For the experiment I have chosen the following parameters:
rftry1=randomForest(x,y,xtest,ytest,mtry=4,ntree=500, importance=TRUE,keep.forest=TRUE,do.trace=TRUE,replace=TRUE,keep.inbag=TRUE,proximity=TRUE)
I then check which variables have been actually used in the forest, and find a very skewed usage:
> varUsed(rftry1)
[1] 2436 1758 1988 1156
The usage actually decreases dramatically from the first to the 4th variable. Would it actually affect prediction? Why does it happen like that?
I have then rerun the experiment with mtry=2, and the usage has reduced overall but still is proportional to the usage of the first one.
MeanDecreaseAccuracy MeanDecreaseGini
1 0.006535855 2.177148 2 0.224706591 127.106268 3 0.006633846 5.020456 4 0.017522580 36.867821
I have noticed that the frequency of usage of the variable in the trees does not affect the importance to the extent of changing the order of importance of variables. So my main 2 questions are:
1) I guess I do not understand what exactly mtry controls ( I have read the number of variables sampled at each split). I have noticed that with mtry 2 and 4 some of the variables have been reused in the tree, while with m=1 they have not been and the trees were much shorter. Do i understand it correctly when I say:
mtry=2 means that at every split instead of firmly choosing one next variable to split on we will randomly choose out of 2? If i have 4 variables overall, and variable 1 was used for very first split - what are my choices for left daugter and right daughter to split on?
2) Is mtry related to the unbalanced use of the variables for split? Why are the variables used like this, and does it affect the prediction outcome to make biased?
All my variables are in the form of factors where 3 have 2 levels and one has 3 levels.
Thank you for your answers!
varUsed()
is internal-only and is not a great measure of anything; a var getting used high up in the tree will give a lower count than if it gets used 2^N times, N levels lower. $\endgroup$