将逻辑回归应用于r中的函数

发布于 2025-02-11 09:52:26 字数 582 浏览 1 评论 0原文

我想运行多个参数的逻辑回归并存储不同的指标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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伊面 2025-02-18 09:52:26

您的功能不起作用的主要原因是您试图将对象调用为公式。您可以使用粘贴公式函数来修复它,但这最终是非常有限的。

我建议您考虑使用更新。这使您可以通过多个变量组合更改更改,或更改训练数据集,而不会破坏功能。

model = glm(extry~a,family=binomial("logit"),data = train_df)
new.model = update(model, .~X0)


new.function <- function(model){
  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) 
}


les <- new.function(new.model)

可以通过将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.

model = glm(extry~a,family=binomial("logit"),data = train_df)
new.model = update(model, .~X0)


new.function <- function(model){
  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) 
}


les <- new.function(new.model)

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.

眉目亦如画i 2025-02-18 09:52:26

要以您预期的方式运行功能,您需要使用非标准评估来捕获符号并将其插入公式。这可以使用match.callas.formula来完成。这是一个使用虚拟数据的完全可复制的示例:

new.function <- function(a) {
  
  # Convert symbol to character
  a <- as.character(match.call()$a)
  
  # Build formula from character strings
  form <- as.formula(paste("extry", a, sep = "~"))
  
  model <- glm(form, family = binomial("logit"), data = train_df)
  pred.prob <- predict(model, test_df, type = 'response')
  predictFull <- ROCR::prediction(pred.prob, test_df$extry)
  auc_ROCR <- ROCR::performance(predictFull, "auc")

  list("AUC" =  auc_ROCR)
}

现在我们可以以您预期的方式调用该功能:

new.function(X0)
#> $AUC
#> A performance instance
#>   'Area under the ROC curve'

new.function(X1)
#> $AUC
#> A performance instance
#>   'Area under the ROC curve'

如果您想查看曲线下的实际区域,则需要进行:

new.function(X0)[email protected][[1]]
#> [1] 0.6599759

因此,您可能希望修改您的功能,以便列表包含>而不是auc_rocr


使用的数据

set.seed(1)

train_df <- data.frame(X0 = sample(100), X1 = sample(100))
train_df$extry <- rbinom(100, 1, (train_df$X0 + train_df$X1)/200)

test_df  <- data.frame(X0 = sample(100), X1 = sample(100))
test_df$extry <- rbinom(100, 1, (test_df$X0 + test_df$X1)/200)

在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 and as.formula. Here's a fully reproducible example using dummy data:

new.function <- function(a) {
  
  # Convert symbol to character
  a <- as.character(match.call()$a)
  
  # Build formula from character strings
  form <- as.formula(paste("extry", a, sep = "~"))
  
  model <- glm(form, family = binomial("logit"), data = train_df)
  pred.prob <- predict(model, test_df, type = 'response')
  predictFull <- ROCR::prediction(pred.prob, test_df$extry)
  auc_ROCR <- ROCR::performance(predictFull, "auc")

  list("AUC" =  auc_ROCR)
}

Now we can call the function in the way you intended:

new.function(X0)
#> $AUC
#> A performance instance
#>   'Area under the ROC curve'

new.function(X1)
#> $AUC
#> A performance instance
#>   'Area under the ROC curve'

If you want to see the actual area under the curve you would need to do:

new.function(X0)[email protected][[1]]
#> [1] 0.6599759

So you may wish to modify your function so that the list contains [email protected][[1]] rather than auc_ROCR


Data used

set.seed(1)

train_df <- data.frame(X0 = sample(100), X1 = sample(100))
train_df$extry <- rbinom(100, 1, (train_df$X0 + train_df$X1)/200)

test_df  <- data.frame(X0 = sample(100), X1 = sample(100))
test_df$extry <- rbinom(100, 1, (test_df$X0 + test_df$X1)/200)

Created on 2022-06-29 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文