将逻辑回归应用于r中的函数
我想运行多个参数的逻辑回归并存储不同的指标IE AUC。 我在下面编写了该功能,但是当我调用它时会出现错误:即使在我的培训和测试数据集中存在变量,也找不到object'x0'的错误(prefvars,data,env):也找不到对象'x0'。有什么想法吗?
new.function <- function(a) {
model = glm(extry~a,family=binomial("logit"),data = train_df)
pred.prob <- predict(model,test_df, type='response')
predictFull <- prediction(pred.prob, test_df$extry)
auc_ROCR <- performance(predictFull, measure = "auc")
my_list <- list("AUC" = auc_ROCR)
return(my_list)
}
# Call the function new.function supplying 6 as an argument.
les <- new.function(X0)
I want to run logistic regression for multiple parameters and store the different metrics i.e AUC.
I wrote the function below but I get an error when I call it: Error in eval(predvars, data, env) : object 'X0' not found even if the variable exists in both my training and testing dataset. Any idea?
new.function <- function(a) {
model = glm(extry~a,family=binomial("logit"),data = train_df)
pred.prob <- predict(model,test_df, type='response')
predictFull <- prediction(pred.prob, test_df$extry)
auc_ROCR <- performance(predictFull, measure = "auc")
my_list <- list("AUC" = auc_ROCR)
return(my_list)
}
# Call the function new.function supplying 6 as an argument.
les <- new.function(X0)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的功能不起作用的主要原因是您试图将对象调用为公式。您可以使用粘贴公式函数来修复它,但这最终是非常有限的。
我建议您考虑使用
更新
。这使您可以通过多个变量组合更改更改,或更改训练数据集,而不会破坏功能。可以通过将
test_df
作为单独的参数来进一步改进该函数,以便您可以与替代测试数据拟合。The main reason why your function didn't work is that you are trying to call an object into a formula. You can fix it with paste formula function, but that is ultimately quite limiting.
I suggest instead that you consider using
update
. This allow you more flexibility to change with multiple variable combination, or change a training dataset, without breaking the function.The function can be further improved by calling the
test_df
as a separate argument, so that you can fit it with an alternative testing data.要以您预期的方式运行功能,您需要使用非标准评估来捕获符号并将其插入公式。这可以使用
match.call
和as.formula
来完成。这是一个使用虚拟数据的完全可复制的示例:现在我们可以以您预期的方式调用该功能:
如果您想查看曲线下的实际区域,则需要进行:
因此,您可能希望修改您的功能,以便列表包含
>而不是
auc_rocr
使用的数据
在2022-06-29上创建的 reprex软件包(v2.0.1)
To run the function in the way you intended, you would need to use non-standard evaluation to capture the symbol and insert it in a formula. This can be done using
match.call
andas.formula
. Here's a fully reproducible example using dummy data:Now we can call the function in the way you intended:
If you want to see the actual area under the curve you would need to do:
So you may wish to modify your function so that the list contains
[email protected][[1]]
rather thanauc_ROCR
Data used
Created on 2022-06-29 by the reprex package (v2.0.1)