我有一个主函数,该函数将参数传递给另一个rpart
用于生成模型的函数。我想有可能rpart.control
从使用省略号的主要功能中指定。如果未定义cp
或minbucket
,那么我想对这两个参数使用默认值。
到目前为止,我还没有成功-请参阅下面的草稿。当前,该函数抱怨cp
找不到。我了解为什么但不能弄清楚,如果用户未提供自己的控件,则如何应用默认值。
require(rpart)
a <- function(df, ...) {
b(df, ...)
}
b <- function(df, ...) {
if (is.null(cp)) cp <- 0.001
if (is.null(minbucket)) minbucket = nrow(df) / 20
rcontrol <- rpart.control(cp = cp, minbucket = minbucket, ...)
rcontrol
}
a(iris) # no controls set my defaults for cp and minbucket
a(iris, cp = 0.123) # set cp, use my default for minbucket
a(iris, cp = 0.123, minbucket = 100) # set cp, and minbucket
a(iris, maxcompete = 3) # use my defaults for cp and minbucket, but pass maxcompete
这是对您的功能的一个小修正:请list(...)
首先应用。
b <- function(df, ...) {
l <- list(...)
if (is.null(l$cp)) l$cp <- 1
if (is.null(l$minbucket)) l$minbucket <- 2
print_all(l$cp, l$minbucket, ...)
}
print_all <- function(...) {
print(list(...))
}
运行示例以确保设置了所有默认值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句