举个例子:

lstParams <- list()
lstParams$formula <- as.formula("Sepal.Length ~ Sepal.Width + Petal.Length")                         
lstParams$data <- substitute(iris)
lstParams$distribution <- "bernoulli"

mdl <- do.call("gbm", lstParams)
mdl
gbm(formula=Sepal.Length ~ Sepal.Width + Petal.Length, distribution="bernoulli", 
    data=iris)
# A gradient boosted model with bernoulli loss function.
# 100 iterations were performed.
# There were 2 predictors of which 0 had non-zero influence.

我可以通过以下方式调用gbm函数:

mdl$call
gbm(formula=Sepal.Length ~ Sepal.Width + Petal.Length, distribution="bernoulli", 
    data=iris)

由于:mdl<-do.call("gbm",lstParams)中可能会发生一些错误(例如,在目标变量中发现了一些NAS或错误的公式):

lstParams <- list()
lstParams$formula <- as.formula("Sepal.Length ~ Sepal.Width + Petal.Lengthx")                        
lstParams$data <- substitute(iris)
lstParams$distribution <- "bernoulli"

mdl <- do.call("gbm", lstParams)
# Error in eval(predvars, data, env) : object 'Petal.Lengthx' not found

I would like to get the call to 100 function in advance,以便进行调试.

有没有办法在行刑前接到那个电话? 谢谢

推荐答案

as.call是你的朋友.quote首先将函数名称作为第一个列表元素.

lstParams <- list()
lstParams$name <- quote(gbm::gbm)
lstParams$formula <- as.formula("Sepal.Length ~ Sepal.Width + Petal.Length")                       
lstParams$data <- substitute(iris)
lstParams$distribution <- "bernoulli"
g <- as.call(lstParams)

打电话

g
# gbm::gbm(formula = Sepal.Length ~ Sepal.Width + Petal.Length, 
#          data = iris, distribution = "bernoulli")

判断

eval(g)
# gbm::gbm(formula = Sepal.Length ~ Sepal.Width + Petal.Length, 
#          distribution = "bernoulli", data = iris)
# A gradient boosted model with bernoulli loss function.
# 100 iterations were performed.
# There were 2 predictors of which 0 had non-zero influence.

你仍然可以使用do.call.

do.call("gbm", as.list(g)[-1])
# A gradient boosted model with bernoulli loss function.
# 100 iterations were performed.
# There were 2 predictors of which 0 had non-zero influence.

R相关问答推荐

按R中不同长度的组将日期时间列值四舍五入到小时

基于2行删除重复项指定每列要执行的操作

如何生成包含可能条目列表而不是计数的表?

如何创建具有总计列和ggplot 2所有条线的百分比标签的堆叠条形图?

IQR()和stats之间四分位距计算的差异::分位数()在R和' ggpubr '

在ggplot Likert条中添加水平线

以R中的正确顺序将日期时间字符列转换为posixct

判断字符串中数字的连续性

如何在xyplot中 for each 面板打印R^2

derrr summarise每个组返回多行?

为什么在ggplot2中添加geom_text这么慢?

二维样条,严格以一个参数递增

在GGPLATE中将突出的点放在前面

从BRM预测价值

您是否可以折叠R中的重复行,同时保留基于所选列的值?

哪一行和行和 Select 特定行,但是考虑到Nas

在数据帧列表上绘制GGPUP

如果条件匹配,则使用Mariate粘贴列名

从线的交点创建面

R仅当存在列时才发生变异