构建一个用于执行 do.call 的表达式调用。

huangapple go评论74阅读模式
英文:

Build a call to get expression for do.call execution

问题

Here is the translated portion:

在这个示例中:

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)
# 使用伯努利损失函数的梯度提升模型。
# 进行了100次迭代。
# 共有2个预测变量,其中有0个具有非零影响。


我可以通过以下方式获取对“gbm”函数的调用:

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

由于在执行“mdl<-do.call("gbm",lstParams)”时可能会出现一些错误(例如在目标变量中找到一些NA值或者错误的公式):

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

mdl <- do.call("gbm", lstParams)
# 错误信息:在eval(predvars, data, env)中找不到对象'Petal.Lengthx'

**我想提前获取对“gbm”函数的调用**以便进行调试。
是否有办法在执行之前获取该调用?
谢谢

Please note that I have provided the translated portion of your text as requested. If you need any further assistance or have specific questions, feel free to ask.

英文:

Giving this example:

lstParams &lt;- list()
lstParams$formula &lt;- as.formula(&quot;Sepal.Length ~ Sepal.Width + Petal.Length&quot;)						 
lstParams$data &lt;- substitute(iris)
lstParams$distribution &lt;- &quot;bernoulli&quot;

mdl &lt;- do.call(&quot;gbm&quot;, lstParams)
mdl
gbm(formula=Sepal.Length ~ Sepal.Width + Petal.Length, distribution=&quot;bernoulli&quot;, 
    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.

I can get the call to gbm function by :

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

Since some error can happen in: mdl&lt;-do.call(&quot;gbm&quot;,lstParams) (e.g some NAs found in the target variable or wrong formula):

lstParams &lt;- list()
lstParams$formula &lt;- as.formula(&quot;Sepal.Length ~ Sepal.Width + Petal.Lengthx&quot;)						 
lstParams$data &lt;- substitute(iris)
lstParams$distribution &lt;- &quot;bernoulli&quot;

mdl &lt;- do.call(&quot;gbm&quot;, lstParams)
# Error in eval(predvars, data, env) : object &#39;Petal.Lengthx&#39; not found

I would like to get the call to gbm function in advance in order to debug it.

Is there any way to get that call before the execution?
Thanks

答案1

得分: 1

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

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)
# 一个带有伯努利损失函数的梯度提升模型。
# 进行了100次迭代。
# 共有2个预测变量,其中0个具有非零影响。

你仍然可以使用 do.call

do.call("gbm", as.list(g)[-1])
# 一个带有伯努利损失函数的梯度提升模型。
# 进行了100次迭代。
# 共有2个预测变量,其中0个具有非零影响。
英文:

as.call is your friend. quote function name as first list element first.

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

Call

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

Evaluation

eval(g)
# gbm::gbm(formula = Sepal.Length ~ Sepal.Width + Petal.Length, 
#          distribution = &quot;bernoulli&quot;, 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.

You can still use do.call.

do.call(&quot;gbm&quot;, 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.

huangapple
  • 本文由 发表于 2023年5月21日 06:45:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定