Reputation: 11
I am currently trying to create a TidyModel logistic regression model on a bankruptcy dataset. I've been using Rebecca Barters guide to create the setup. This is my first model like this, so any help is appreciated.
When trying to create the conf. matrix I get the following error: Error in UseMethod("conf_mat") : no applicable method for 'conf_mat' applied to an object of class "list"
Thanks a lot.
This is the head of my initial data frame "debt_data": structure(list(bankrupt = c(1L, 1L, 1L, 1L, 1L, 1L), Interest.bearing.debt.interest.rate = c(0.000725072507250725, 0.000647064706470647, 0.00079007900790079, 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")
This is the code:
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)
I've tried making changes to the model and Googled around, but I can't seem to get any further.
Upvotes: 1
Views: 437
Reputation: 2155
Ulrik, your code with some changes:
Used mtcars data to give a reproducible example.
Conversion of bankrupt to factor, so you can classify.
Full model specification, including set to classify.
Removal of conversion of model_predictions to list, as conf_mat takes a tibble (this removes the error).
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)
You were very close, only three small changes! Hope this helps :-)
Upvotes: 1