R语言 UseMethod(“conf_mat”)中的错误:“conf_mat”没有适用的方法应用于类“list”的对象

polkgigr  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(156)

我目前正在尝试在破产数据集上创建一个TidyModel逻辑回归模型。我一直使用Rebecca Barters指南来创建设置。这是我第一次使用这样的模型,因此感谢任何帮助。
尝试创建配置矩阵时,我收到以下错误:* * 使用方法("conf_mat")中的错误:"conf_mat"没有适用的方法应用于类"list"的对象**
1.有人能帮我把这个从列表转换过来吗?我似乎不能这样做来让conf.matrix运行。
1.运行"model_performance"行时,我得到了 * rmse * 和 * rsq * 分数,但我期望得到 * accuracy * 和 * roc_auc *。
多谢了。
这是我的初始 Dataframe "debt_data"的头:构造(列表(破产= c(1L,1L,1L,1L,1L,1L),有息债务利率= c(0.00072507250725、0.00064706470647、0.00079007900790079、0.0004490449、0.000686068606866、0.00071607160716),总债务与总净值= c(0.0212659243655332,0.0125023937843679,0.0212476860084444,0.00957240171805324,0.00514960012402083,0.0142131516792967),负债比率= c(0.207576261450555、0.171176346101521、0.207515796474892、0.151464764035432、0.106509054630105、0.180427487377936)),行名称= c(不适用,6L),类=" Dataframe ")
这是密码:

debt_data <- data %>% 
  select(
    Bankrupt.,
    Interest.bearing.debt.interest.rate,
    Total.debt.Total.net.worth,
    Debt.ratio..,
  ) %>% 
  rename(
    bankrupt = Bankrupt.,
    total_debt_vs_total_net_worth = Total.debt.Total.net.worth,
    debt_ratio = Debt.ratio..
  )

set.seed(234589)
debt_split <- initial_split(debt_data, 
                             prop = 3/4)
debt_split 

debt_train <- training(debt_split)
debt_test <- testing(debt_split)

model1_cv <- vfold_cv(debt_test)

debt_recipe <- recipe(bankrupt ~
                      Interest.bearing.debt.interest.rate +
                      total_debt_vs_total_net_worth +
                      debt_ratio, 
                      data = debt_data) %>% 
                      step_normalize(all_numeric_predictors()) %>%
                      step_impute_knn(all_predictors())
debt_recipe

model_workflow <- workflow() %>%
  add_recipe(debt_recipe) %>%
  add_model(linear_reg())

model_fit <- model_workflow %>% 
  last_fit(debt_split)
model_fit

model_performance <- model_fit %>% collect_metrics()
model_performance

model_predictions <- model_fit %>% collect_predictions()
model_predictions

model_predictions <- model_fit %>% pull(.predictions)
model_predictions

# Conf. matrix
model_predictions %>% 
  conf_mat(truth = bankrupt, estimate = .pred_class)

我试着对模型进行修改,并在谷歌上四处搜索,但似乎无法取得任何进展。

5gfr0r5j

5gfr0r5j1#

Ulrik,你的代码做了一些改动:
1.使用mtcars数据给予一个可重复的例子。
1.将破产人转换为要素,以便您进行分类。
1.完整的型号规格,包括分类设置。
1.删除model_predictions到list的转换,因为conf_mat接受tibble(这会删 debugging 误)。

library(tidyverse)
 library(tidymodels)
 debt_data <- tibble(bankrupt = if_else(mtcars$cyl == 8, 1, 0) |> as_factor(),
                     Interest.bearing.dept.interest.rate = mtcars$disp,
                     total_debt_vs_total_net_worth = mtcars$disp,
                     debt_ratio = mtcars$hp)

 debt_split <- initial_split(debt_data, 
                             prop = 3/4)
 debt_split 

 debt_train <- training(debt_split)
 debt_test <- testing(debt_split)

 model1_cv <- vfold_cv(debt_train) # Changed from test

 debt_recipe <- recipe(bankrupt ~
                         Interest.bearing.dept.interest.rate +
                         total_debt_vs_total_net_worth +
                         debt_ratio, 
                       data = debt_data) %>% 
   step_normalize(all_numeric_predictors()) %>%
   step_impute_knn(all_predictors())

 debt_recipe

 logistic_reg_glm_spec <-
   logistic_reg(penalty = 1) |> # randomly chosen penalty
   set_engine('glmnet') |>
   set_mode("classification")

 model_workflow <- workflow() %>%
   add_recipe(debt_recipe) %>%
   add_model(logistic_reg_glm_spec)

 model_workflow

 model_fit <- fit(model_workflow, data = debt_train)

 model_fit <- model_workflow %>% 
   last_fit(debt_split)
 model_fit

 model_performance <- model_fit %>% collect_metrics()
 model_performance
 # Now you get accuracy and roc_auc

 model_predictions <- model_fit %>% collect_predictions()
 model_predictions

 model_predictions |>
 conf_mat(truth = bankrupt,
        estimate = .pred_class)

你非常接近,只有三个小变化!希望这对你有帮助:-)

相关问题