Suppose I want to iterate over different values with a for loop in a function (e.g. randomForest)
for (i in c(100, 200, 500)){
randomForest(Predictor ~., data = train, ntree = i)}
One of the values, passed to randomForest function that I want to evaluate is the default value (suppose that I don't know that the default value of ntree in randomForest is 500)
How can I specify that in the for loop?
for (i in c(100,200, default)){
randomForest(Predictor ~., data = train, ntree = i)}
You could look up the value with formals
, which gives you a list which includes all the default values. But it comes with its own set of problems, as not all functions handle things the exact same way.
The first problem becomes clear in your example: formals(randomForest)
only gives you x
and ...
, both without defaults. That is because randomForest is a generic method, which accepts different arguments based on the class of the first one. To get the default for ntree
, you need
formals(randomForest:::randomForest.default)$ntree
Some more problems I can think of:
somedataframe[1]
and somedataframe[1,]
or somedataframe[,1]
? What is the default?match.call()
and match.call
with all arguments filled in as specified by the defaults, will give different results.So put all together, I think you're better off just placing a call outside the loop, or calling with an if ... else