英文:
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 <- 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.
I can get the call to gbm
function by :
mdl$call
gbm(formula=Sepal.Length ~ Sepal.Width + Petal.Length, distribution="bernoulli",
data=iris)
Since some error can happen in: mdl<-do.call("gbm",lstParams)
(e.g some NAs found in the target variable or wrong formula):
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 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 <- 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)
Call
g
# gbm::gbm(formula = Sepal.Length ~ Sepal.Width + Petal.Length,
# data = iris, distribution = "bernoulli")
Evaluation
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.
You can still use 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论