Search code examples
rrandom-forest

How to prevent do.call() from printing all data frame entries for class "call" in a model?


If I use do.call() to run a model with parameters supplied as a list, the "call" returned with the model lists all entries within any data frames of the parameters. This prints an extremely long model output for large datasets.

library(randomForest)
data(iris)
do.call(randomForest, list(Species ~ ., data=iris))
#Call:
# randomForest(formula = Species ~ ., data = structure(list(Sepal.Length = c(5.1,  4.9, 4.7,
#4.6, 5, 5.4, 4.6, 5, 4.4, 4.9, 5.4, 4.8,...

Is it possible to prevent printing the data frame entries so the output matches a normal model call, for example, to randomForest?

randomForest(Species ~ ., data=iris)
#Call:
# randomForest(formula = Species ~ ., data = iris) 

I could try to reconstruct and replace the "call" slot in the model object after assigning it, or set it to NULL, but this seems like a bad solution.

mod <- do.call(randomForest, list(Species ~ ., data=iris))
mod$call <- 'randomForest(formula = Species ~ ., data = iris)'
mod
#Call:
# "randomForest(formula = Species ~ ., data = iris)"

I'm sure there is a better and simpler solution, but I cannot find it. Thanks in advance for any help.


Solution

  • Use quote :

    do.call("randomForest", list(Species ~ ., data = quote(iris)))
    

    giving:

    Call:
     randomForest(formula = Species ~ ., data = iris) 
                   Type of random forest: classification
                         Number of trees: 500
    No. of variables tried at each split: 2
    
            OOB estimate of  error rate: 4.67%
    Confusion matrix:
               setosa versicolor virginica class.error
    setosa         50          0         0        0.00
    versicolor      0         47         3        0.06
    virginica       0          4        46        0.08