Why Implementation Choices Matter
The randomForest and ranger packages can both fit random forests in R, but their interfaces use different argument names and do not guarantee identical fits. A setting such as the number of trees has a straightforward counterpart; sampling and importance settings need more care. A comparison is meaningful only when the analyst identifies which choices have been matched and which remain implementation differences.
The focus here is the correspondence between controls, not a claim that the packages produce the same predictions. Both implementations involve random sampling and random feature selection, so even matching settings and setting the same R seed does not generally make their tree-by-tree results identical. For a controlled model comparison, specify the relevant settings explicitly and evaluate the fitted models using a common assessment procedure.
Argument Names and Their Roles
The main arguments have the following counterparts. The table gives the practical mapping; it does not assert that all details of the algorithms are identical.
| Choice | randomForest | ranger | Interpretation |
|---|---|---|---|
| Number of trees | ntree | num.trees | How many trees contribute to the forest prediction. |
| Predictors considered at a split | mtry | mtry | The number of candidate predictors sampled for a split. |
| Node-size control | nodesize | min.node.size | A control on how small nodes may become during tree growth. |
| Sampling with replacement | replace and sampsize | replace and sample.fraction | Whether observations can be selected more than once within a tree’s sample, and how many are selected. |
| Importance | importance | importance | Whether and how variable-importance measures are calculated. |
The number of predictors \(p\) is the number of candidate input variables supplied to the forest. The exact default for mtry depends on the task and package conventions; classification commonly uses a value near \(\sqrt{p}\), while regression commonly uses a value near \(p/3\). These are starting conventions, not a reason to leave the setting implicit. If two fits are meant to be compared, calculate the intended integer value and pass it to both functions.
The node-size arguments have similar purposes, but the packages’ tree-growing details are not identical. Conventional defaults are often 1 for classification and 5 for regression. Do not infer that assigning the same number forces the same terminal nodes or tree structure. In particular, these arguments are controls on growth, not a promise that every terminal node will have exactly that many observations.
Sampling: Match the Scheme, Not Just the Number
With replacement, a tree’s sample may include repeated copies of an observation. In randomForest, specify the sample size with sampsize; in ranger, specify the fraction of the data with sample.fraction. A common bootstrap setup draws \(n\) observations with replacement from \(n\) training rows. The corresponding explicit settings are replace = TRUE, sampsize = n and replace = TRUE, sample.fraction = 1, respectively.
For sampling without replacement, set replace = FALSE in both packages and specify the desired sample size. In randomForest, use an integer in sampsize; in ranger, use the sample size divided by \(n\) in sample.fraction. Be explicit: a default sample size in one package should not be assumed to reproduce a chosen sampling fraction in the other.
The omitted observations in a tree’s sample can be used to form out-of-bag predictions. With bootstrap sampling, the number omitted varies from tree to tree; with a fixed-size sample without replacement, that number is fixed for each tree. Consequently, matching the expected omitted fraction is not the same as matching the sampling process or its variability.
Worked Example: Matching a Regression-Forest Setup
An analyst has 360 training rows and 15 numeric candidate predictors for a hypothetical claim-cost response. The analyst wants 700 trees, the common regression starting value \(mtry=\lfloor p/3 \rfloor\), a node-size setting of 5, and bootstrap sampling. Write corresponding calls in both packages and check the values.
Method. Calculate the shared intended settings first, then translate the argument names. Here \(p=15\), so \(p/3=5\), and \(\lfloor p/3 \rfloor=5\). The bootstrap draws 360 observations per tree, with replacement.
The randomForest call is:
The corresponding ranger call is:
For the second call, the sample size is \(360 \times 1=360\). Both calls request 700 trees, 5 candidate predictors per split, node-size control 5, and 360 bootstrap draws per tree. The choices are matched at the level of the requested settings. They do not establish that the fitted trees or predictions will be identical.
Common mistake avoided. Using nodesize = 5 in both calls is not possible: ranger uses min.node.size. More importantly, merely matching the node-size number should not be described as matching every detail of tree growth.
Translating a Sample Without Replacement
A sample size specified as a count in one interface must be converted to a fraction in the other. The count and fraction represent the same intended sample size when the fraction is the count divided by the number of available training rows.
Worked Example: Matching a Classification Sample Size
A hypothetical fraud-screening data set has 250 training rows and 25 numeric predictors. The analyst wants 900 classification trees. For each tree, sample 158 distinct rows without replacement, use \(mtry=\lfloor\sqrt{p}\rfloor\), and set the node-size control to 2. Give the corresponding sampling arguments and verify the conversion.
Method. First, \(\sqrt{25}=5\), so the intended \(mtry\) is 5. Next, the sample fraction is the sample count divided by the training-row count:
The randomForest sampling settings are replace = FALSE and sampsize = 158. The ranger settings are replace = FALSE and sample.fraction = 0.632. The rest of the corresponding settings are ntree = 900, mtry = 5, and nodesize = 2 for randomForest; and num.trees = 900, mtry = 5, and min.node.size = 2 for ranger.
The sample-size check is \(250 \times 0.632=158\). Since sampling is without replacement, each tree has \(250-158=92\) observations omitted from its sample. That count is fixed per tree under this setup; it is not the variable number omitted by a bootstrap sample.
Common mistake avoided. Setting sample.fraction = 0.632 while leaving replace = TRUE changes the interpretation: it requests a fraction-sized sample with replacement, not 158 distinct rows. The replacement setting must be stated as well as the size.
Importance Settings Are Not Interchangeable Scores
Importance calculations describe how a fitted forest uses or depends on predictors; they do not directly measure out-of-sample predictive accuracy. In randomForest, request importance calculations with importance = TRUE. The returned measures can include permutation-based importance and a measure based on accumulated split impurity. For a classification forest, importance(fit, type = 1) selects the permutation measure commonly reported as mean decrease in accuracy, while type = 2 selects mean decrease in Gini impurity. For regression, the corresponding measures are commonly labelled percent increase in MSE and increase in node purity.
In ranger, select the calculation with the importance argument. Use importance = "permutation" to request permutation importance or importance = "impurity" for impurity-based importance. The default is not to calculate importance. The package also offers other choices, but a comparison should name the method actually used rather than refer vaguely to “importance.”
Permutation importance measures a change in prediction performance when a predictor’s values are permuted according to the implementation’s procedure. Impurity importance accumulates the improvement in a split criterion attributed to a predictor. The methods answer different questions and their numerical scales are not directly interchangeable. Even when both packages use permutation importance, their reported values need not match: details of the implementation, scaling, and fitted forest can differ.
Worked Example: Selecting an Importance Method for Comparison
A hypothetical insurer is comparing two classification forests for predicting whether a policy renews. Both use the same 12 numeric predictors, 1,000 trees, \(mtry=4\), node-size control 1, and bootstrap samples of size \(n\). The analysis question is which predictors matter to predictive performance, not which predictors produced the largest total split impurity. Specify an appropriate importance method and explain how to compare the results.
Method. The requested interpretation is about predictive performance, so choose permutation importance in each package. In randomForest, use importance = TRUE, then inspect the permutation measure with importance(fit, type = 1). In ranger, use importance = "permutation". Keep the other settings explicit: 1,000 trees, \(mtry=4\), node-size control 1, replacement enabled, and a full-size sample with replacement.
Check the feature-count calculation: \(\sqrt{12}\) is about 3.464, so rounding down would give 3, not 4. The specified \(mtry=4\) is therefore an intentional choice rather than the rounded square-root convention. Both calls should pass 4 explicitly so a package default cannot silently change that choice.
The analyst should interpret each package’s importance output according to its own definition and scale. A ranking within a fit may be informative, but a value from one package should not be subtracted from a value in the other and presented as a directly comparable effect. If the practical decision is based on predictive performance, assess the forests on the same held-out observations using the same metric as well.
Common mistake avoided. Choosing importance = "impurity" in ranger would answer a different question from the requested permutation-based assessment. Likewise, treating the magnitude of a permutation score as a universal measure of business value would overstate what the statistic establishes.
A Practical Comparison Workflow
Use the same response, training rows, and intended candidate predictors. State whether the task is classification or regression.
Set the tree count, \(mtry\), node-size control, replacement setting, and sample size or fraction in both calls.
For a performance-oriented question, request permutation importance in both packages; for split-criterion summaries, request impurity importance and label it clearly.
Use the same validation data and metric. Do not infer equivalent predictive performance from matching arguments or similar importance rankings alone.
For reproducibility, record the package versions, random seed, data preparation, and all nondefault controls. A shared seed is useful for repeating a fit in the same environment, but it does not make random-number use identical across packages. Also check that the chosen settings are accepted by the installed package version and that the fitted object reports the intended configuration.
Common Implementation Traps
- Transferring an argument name literally. Use
ntreeversusnum.trees, andnodesizeversusmin.node.size. - Leaving defaults implicit. Defaults may depend on the task or package. Set \(mtry\), sample settings, and node-size controls explicitly for a comparison.
- Confusing a sample fraction with a sample count. Convert using sample count divided by training-row count, and state whether sampling is with replacement.
- Equating matching controls with identical forests. Different implementations can yield different trees, predictions, and importance values even when the intended settings correspond.
- Comparing importance numbers as though they shared a scale. Name the method and interpret each output according to its definition; use common validation metrics for predictive comparisons.
Check Your Understanding
For each problem, identify the relevant package settings and check any conversion before consulting the answer.
- A regression data set has 300 rows and 18 numeric predictors. You want 600 trees, \(mtry=\lfloor 18/3 \rfloor\), node-size control 5, and bootstrap samples of size 300. What are the corresponding tree-count, \(mtry\), node-size, and sampling arguments in each package? (Answer:
randomForest:ntree=600,mtry=6,nodesize=5,replace=TRUE,sampsize=300;ranger:num.trees=600,mtry=6,min.node.size=5,replace=TRUE,sample.fraction=1.) - A classification forest uses 400 rows and should sample 252 distinct rows per tree. What sample fraction should be specified in
rangerif sampling is without replacement, and how many rows are omitted from each sample? (Answer:sample.fraction=0.63withreplace=FALSE; \(400-252=148\) rows are omitted.) - A classification task has 16 candidate predictors. The intended \(mtry\) is the floor of the square root of the predictor count. What value should be passed explicitly to both packages? (Answer: \(\lfloor\sqrt{16}\rfloor=4\), so set
mtry=4.) - You want importance measures that reflect changes in predictive performance when predictor values are permuted. Which importance settings should you use, and should raw values necessarily match across packages? (Answer: use
importance=TRUEand inspect type 1 inrandomForest; useimportance="permutation"inranger. No: implementation and scale differences mean raw values need not match.) - A team requests a sample of 70% of 500 rows, without replacement, and sets
replace=TRUEin both packages. Is that specification consistent with the request, and what count corresponds to 70%? (Answer: no; setreplace=FALSE. The sample count is \(0.70\times500=350\), so usesampsize=350orsample.fraction=0.70.)