run*_*rds 1 arguments r function
如何将可选参数传递给 R 中的函数?
这方面的一个例子是,我可能希望从模型的特定超参数组合中创建一个函数。但是,我不想配置所有超参数,因为在大多数情况下许多超参数都不相关。
有时,我希望能够手动传入一个我想更改的超参数。我经常在函数中看到 ... ,但无法弄清楚这是否与这种情况有关或至少如何使用它们。
library(gbm)
library(ggplot)
data('diamonds', package = 'ggplot2')
example_function = function(n.trees = 5){
model=gbm(formula = price~ ., n.trees = 5, data = diamonds)
}
# example of me passing in an unplanned argument
example_function(n.trees = 5, shrinkage = 0.02)
Run Code Online (Sandbox Code Playgroud)
这有可能以智能方式处理吗?
您可以使用...参数(记录在 中?dots)从调用函数传递参数。在你的情况下,试试这个:
library(gbm)
library(ggplot2)
data('diamonds', package = 'ggplot2')
example_function <- function(n.trees = 5, ...){
gbm(formula = price~ ., n.trees = 5, data = diamonds, ...)
}
# Pass in the additional 'shrinkage' argument
example_function(n.trees = 5, shrinkage = 0.02)
## Distribution not specified, assuming gaussian
## gbm(formula = price ~ ., data = diamonds, n.trees = 5, shrinkage = 0.02)
## A gradient boosted model with gaussian loss function.
## 5 iterations were performed.
There were 9 predictors of which 2 had non-zero influence.
Run Code Online (Sandbox Code Playgroud)