https://github.com/tidymodels/lorax
R functions and methods to extract elements of tree- and rule-based models.
https://github.com/tidymodels/lorax
Last synced: about 1 month ago
JSON representation
R functions and methods to extract elements of tree- and rule-based models.
- Host: GitHub
- URL: https://github.com/tidymodels/lorax
- Owner: tidymodels
- License: other
- Created: 2026-03-14T15:01:44.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2026-06-07T15:55:20.000Z (about 1 month ago)
- Last Synced: 2026-06-09T01:34:20.839Z (about 1 month ago)
- Language: R
- Size: 1.43 MB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 7
-
Metadata Files:
- Readme: README.Rmd
- Changelog: NEWS.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# lorax
[](https://github.com/topepo/lorax/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/topepo/lorax)
The goal of lorax is to help look at different aspects of tree- and rule-based models.
lorax supports a few APIs:
- `as.party()` converts trees to the format used by `partykit::ctree()`, mostly because it has an amazing `plot()` method.
- `extract_rules()` helps write out the logical paths to the terminal nodes.
- `active_predictors()` enumerates which predictors were actually used in a split.
- `var_imp()` is a wrapper for any importance method contained in the package. This is a wrapper that enables a common interface to the scores.
Here is a list of which classes have which methods:
```{r}
#| echo: false
library(lorax)
active_predictors_mthd <-
tibble::tibble(
method = "active_predictors",
class = ls(pattern = "^active_predictors", envir = asNamespace("lorax")) |>
gsub("active_predictors\\.", "", x = _)
)
extract_rules_mthd <-
tibble::tibble(
method = "extract_rules",
class = ls(pattern = "^extract_rules", envir = asNamespace("lorax")) |>
gsub("extract_rules", "", x = _) |>
gsub("^\\.", "", x = _)
)
party_mthd <-
tibble::tibble(
method = "as.party",
class = ls(pattern = "^as.party", envir = asNamespace("lorax")) |>
gsub("as.party\\.", "", x = _)
)
imp_mthd <-
tibble::tibble(
method = "var_imp",
class = ls(pattern = "^var_imp", envir = asNamespace("lorax")) |>
gsub("var_imp", "", x = _) |>
gsub("^\\.", "", x = _)
)
dplyr::bind_rows(
imp_mthd,
active_predictors_mthd,
party_mthd,
extract_rules_mthd
) |>
dplyr::filter(method != class & !grepl("^new_", class) & class != "") |>
dplyr::mutate(here = cli::symbol$tick) |>
tidyr::pivot_wider(
id_cols = class,
names_from = method,
values_from = here,
values_fill = cli::symbol$cross
) |>
dplyr::arrange(tolower(class)) |>
dplyr::mutate(
as.party = ifelse(class %in% c("rpart", "cforest", "party"), "n/a", as.party),
var_imp = ifelse(class %in% c("bart", "C5.0"), "n/a", var_imp)
) |>
knitr::kable()
```
Note that `as.party.rpart()` is in the partykit package and that cforest is made out of party objects.
## Installation
You can install the development version of lorax like so:
``` r
require(pak)
pak::pak("topepo/lorax")
```
## Example
```{r}
#| label: startup
#| include: false
library(lorax)
library(tibble)
library(ranger)
library(palmerpenguins)
```
```{r}
#| label: ranger-ex
#| results: hide
set.seed(822)
rngr_fit <- ranger(species ~ ., data = penguins, max.depth = 3, num.trees = 10)
```
```{r}
#| label: ranger-party
rngr_party <- as.party(rngr_fit, data = penguins, tree = 1)
rngr_party
plot(rngr_party)
```
```{r}
#| label: ranger-rules
all_rules <- extract_rules(rngr_party, trees = 10)
# An expression
all_rules$rules[[1]]
# Text
all_rules$rules[[1]] |> rule_text()
# Substitutions
new_names <-
tribble(
~ original, ~ label,
"flipper_length_mm", "Flipper Length",
"body_mass_g", "Body Mass"
)
all_rules$rules[[1]] |> rule_text(key = new_names)
# Bullets:
all_rules$rules[[1]] |>
rule_text(key = new_names, bullets = TRUE) |>
cat()
```
## Code of Conduct
Please note that the lorax project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/1/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.