knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(dplyr) library(tidypredict) library(parsnip) library(aorsf) set.seed(100)
| Function |Works|
|---------------------------------------------------------------|-----|
|tidypredict_fit(), tidypredict_sql(), parse_model() | ✔ |
|tidypredict_to_column() | ✗ |
|tidypredict_test() | ✔ |
|tidypredict_interval(), tidypredict_sql_interval() | ✗ |
|parsnip | ✔ |
Only regression models with numeric predictors are supported. Classification requires a voting mechanism that cannot be expressed as a single formula, and oblique splits on categorical predictors operate on an internal encoding that is not reproduced here.
Here is a simple orsf() model using the mtcars dataset:
library(dplyr) library(tidypredict) library(aorsf) model <- orsf(mtcars, mpg ~ ., n_tree = 5)
Unlike axis-aligned forests, each split in an oblique random forest is a linear combination of (standardized) predictors compared against a cutpoint. tidypredict reads the trees stored in the fitted forest, folds the forest's centering and scaling into the split coefficients, and turns each tree into a nested dplyr::case_when() statement. The trees are then averaged.
tidypredict_fit(model)
From there, the Tidy Eval formula can be used anywhere it can be evaluated. tidypredict provides three paths:
dplyr, mutate(mtcars, !! tidypredict_fit(model))tidypredict_to_column(model) to add it to a piped command settidypredict_sql(model) to retrieve the SQL statementaorsf uses observed linear-combination values from the training data as split cutpoints. A training row can therefore land exactly on a split boundary, where floating-point differences between aorsf's internal traversal and the generated formula may send it down a different branch. This affects only rows that coincide with a training cutpoint; on new data the formula reproduces predict() exactly.
tidypredict also supports aorsf model objects fitted via the parsnip package (using the bonsai extension).
library(parsnip) library(bonsai) parsnip_model <- rand_forest(mode = "regression", trees = 5) %>% set_engine("aorsf") %>% fit(mpg ~ ., data = mtcars) tidypredict_fit(parsnip_model)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.