0

In source code of R randomForest package, I find the following code in grow.R. What's the purpose for UseMethod? Why does function grow not have function definition and just grow.default and grow.randomForest have definition? Is this something related to calling C function in the R package?

grow <- function(x, ...) UseMethod("grow")

grow.default <- function(x, ...)
  stop("grow has not been implemented for this class of object")

grow.randomForest <- function(x, how.many, ...) {
  y <- update(x, ntree=how.many)
  combine(x, y)
}

Also, in the randomForest.R file, I only find the following code. There is randomForest.default.R file too. Why is there no randomForest.randomForest definition like function grow?

"randomForest" <-
function(x, ...)
  UseMethod("randomForest")
Salty Gold Fish
  • 431
  • 5
  • 14
  • 1
    Are you familiar with S3 classes? Reading a little bit about S3 dispatch would be a good place to start. [Advanced R has a section on S3](https://adv-r.hadley.nz/s3.html). – Gregor Thomas Feb 16 '23 at 21:20
  • There's nothing in the code you show that has anything to do with calling C code. Why the authors chose to make the `grow()` default an error and the `randomForest` default the main method is hard to answer definitively. Presumably they wanted `grow()` to error early if it gets called on inappropriate input, but still keep it flexible to work with other classes. – Gregor Thomas Feb 16 '23 at 21:25
  • 1
    There's no `randomForest.randomForest` method because you don't use a `randomForest` object as **input** to the `randomForest()` function. But they do use S3 dispatch to handle formula and (default) matrix, and you can see the `randomForest.default` and `randomForest.formula` code in their respective files. – Gregor Thomas Feb 16 '23 at 21:26
  • @BenBolker <> okay, written up :) – Gregor Thomas Feb 16 '23 at 21:45

1 Answers1

3

What's the purpose for UseMethod? Why does function grow not have function definition and just grow.default and grow.randomForest have definition?

I'd suggest reading about S3 dispatch to understand the patterns you see. Advanced R has a chapter on S3. You can also see related questions here on Stack Overflow.

Is this something related to calling C function in the R package?

No.

Why is there no randomForest.randomForest definition like function grow?

This should make sense if you do the recommended reading above. S3 dispatch uses a pattern of function_name.class to call the correct version of the function (method) based on class of the input. You don't give a randomForest object as an input to the randomForest function, so there is no randomForest.randomForest method defined.

grow() does get called on randomForest objects, hence the grow.randomForest() method. Presumably the authors wanted grow() to error early if it gets called on inappropriate input, so the default for other classes is an immediate error, but they still keep dispatch flexible to work with other classes, enabling extensions of the package and nice play with other packages that may have their own grow() implementations.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294