I tried reprex by Mark Rieke and I got an error for the last command (conf_mat).
load tidymodels & rules
library(tidymodels)
library(rules)
#>
#> Attaching package: 'rules'
#> The following object is masked from 'package:dials':
#>
#> max_rules
# example training dataset
cars_train <- as_tibble(mtcars)
# change the number of cylinders to character for predicting as a class
cars_train <-
cars_train %>%
mutate(cyl = as.character(cyl))
# training df
cars_train
#> # A tibble: 32 × 11
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # … with 22 more rows
# setup recipe with no preprocessing
cars_rec <-
recipe(cyl ~ ., data = cars_train)
# specify c5 model; no need to set mode (can only be used for classification)
cars_spec <-
C5_rules() %>%
set_engine("C5.0")
# create workflow
cars_wf <-
workflow() %>%
add_recipe(cars_rec) %>%
add_model(cars_spec)
# fit workflow
cars_fit <- fit(cars_wf, data = cars_train)
# add predictions to df
cars_preds <-
predict(cars_fit, new_data = cars_train) %>%
bind_cols(cars_train) %>%
select(.pred_class, cyl)
cars_preds
#> # A tibble: 32 × 2
#> .pred_class cyl
#> <fct> <chr>
#> 1 6 6
#> 2 6 6
#> 3 4 4
#> 4 6 6
#> 5 8 8
#> 6 6 6
#> 7 8 8
#> 8 4 4
#> 9 4 4
#> 10 6 6
#> # … with 22 more rows
# confusion matrix
cars_preds %>%
conf_mat(truth = cyl,
estimate = .pred_class)
#> Error in `yardstick_table()`:
#> ! `truth` must be a factor.
#> ℹ This is an internal error in the yardstick package, please report it to the package authors.
#> Backtrace:
#> ▆
#> 1. ├─cars_preds %>% conf_mat(truth = cyl, estimate = .pred_class)
#> 2. ├─yardstick::conf_mat(., truth = cyl, estimate = .pred_class)
#> 3. └─yardstick:::conf_mat.data.frame(., truth = cyl, estimate = .pred_class)
#> 4. └─yardstick:::yardstick_table(truth = truth, estimate = estimate, case_weights = case_weights)
#> 5. └─rlang::abort("`truth` must be a factor.", .internal = TRUE)