| Function | Works |
|---|---|
tidypredict_fit(), tidypredict_sql(),
parse_model()
|
|
tidypredict_to_column() |
|
tidypredict_test() |
|
parsnip |
How it works
Here is a simple C5.0() classification model using the
mtcars dataset:
Under the hood
C5.0 stores its fitted tree as text in the tree element
of the model object. tidypredict parses that text directly,
so the model can be translated even when it was fitted through the x/y
interface (as parsnip does).
The tree is transformed into a dplyr, a.k.a Tidy Eval,
formula. The decision tree becomes a nested
dplyr::case_when() statement.
tidypredict_fit(model)
#> case_when(cyl <= 6 ~ case_when(cyl <= 4 ~ "1", .default = case_when(wt <=
#> 2.875 ~ "0", .default = "1")), .default = "0")From there, the Tidy Eval formula can be used anywhere where it can
be operated. tidypredict provides three paths:
- Use directly inside
dplyr,mutate(mtcars2, !! tidypredict_fit(model)) - Use
tidypredict_to_column(model)to a piped command set - Use
tidypredict_sql(model)to retrieve the SQL statement
Categorical predictors
C5.0 handles categorical predictors natively. The
generated formula uses %in% for categorical splits, and
multi-way splits become nested case_when() branches:
mtcars3 <- mtcars2
mtcars3$gear <- factor(mtcars3$gear)
model_cat <- C5.0(mtcars3[, c("wt", "gear", "mpg")], mtcars3$vs)
tidypredict_fit(model_cat)
#> case_when(mpg <= 21 ~ "0", .default = "1")parsnip
tidypredict also supports C5.0 model
objects fitted via the parsnip package.
library(parsnip)
parsnip_model <- decision_tree(mode = "classification") |>
set_engine("C5.0") |>
fit(vs ~ wt + cyl + mpg, data = mtcars2)
tidypredict_fit(parsnip_model)
#> case_when(cyl <= 6 ~ case_when(cyl <= 4 ~ "1", .default = case_when(wt <=
#> 2.875 ~ "0", .default = "1")), .default = "0")