r语言 - UseMethod( "conf_mat" ) 中的错误:没有适用于类 "list" 对象的'conf_mat'方法



我目前正在尝试在破产数据集上创建一个TidyModel逻辑回归模型。我一直在使用丽贝卡·巴特斯的指南来创建设置。这是我的第一个这样的模型,所以任何帮助都是感激的。

当尝试创建conf. matrix时,我得到以下错误:UseMethod("conf_mat")错误:没有适用于类"list">对象的'conf_mat'方法

  1. 谁能帮我从一个列表转换这个?我似乎无法使conf.matrix运行。
  2. 运行"model_performance"行我得到一个rmsersq得分,但我期望得到精度roc_auc

谢谢。

这是我的初始数据帧"debt_data"的头部:结构(list(bankruptcy = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), Interest.bearing.debt.interest.rate = c(0.000725072507250725,0.000647064706470647, 0.0007900790079, 0.000449044904490449,0.000686068606860686, 0.000716071607160716), total_debt_vs_total_net_worth = c(0.0212659243655332,0.0125023937843679、0.0212476860084444、0.00957240171805324、0.00514960012402083, 0.0142131516792967), debt_ratio = c(0.207576261450555,0.171176346101521、0.207515796474892、0.151464764035432、0.106509054630105、0.180427487377936)), row.names = c(NA, 6L), class = "data.frame")

这是代码:

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)

我试着改变模型,并在谷歌上搜索,但我似乎不能得到任何进一步。

修改后的代码:

  1. 用mtcars的数据给出一个可重复的例子。

  2. 将破产转化为要素,这样就可以分类了。

  3. 全型号规格,包括分类设置。

  4. 删除model_predictions到list的转换,因为conf_mat接受一个tibble(这消除了错误)。

    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)
    

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

最新更新