if (!rlang::is_installed("partykit")) { knitr::opts_chunk$set( eval = FALSE ) }
knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) library(dplyr) library(tidypredict) library(partykit) library(parsnip) 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 are supported. Classification cforest() models rely on
a voting mechanism that cannot be expressed as a single formula.
Here is a simple cforest() model using the mtcars dataset:
library(dplyr) library(tidypredict) library(partykit) model <- cforest(mpg ~ wt + cyl, data = mtcars, ntree = 5)
Each tree in the forest is a partykit party tree. tidypredict turns every
tree into a nested dplyr::case_when() statement using each terminal node's
in-bag weighted mean, then averages the trees. This matches the default
predict() behavior, which scales the per-tree weights before aggregating.
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, con) to retrieve the SQL statementtidypredict also supports cforest model objects fitted via the parsnip
package with the "partykit" engine, which is provided by the bonsai package.
library(bonsai) library(parsnip) parsnip_model <- rand_forest(mode = "regression", trees = 5) %>% set_engine("partykit") %>% fit(mpg ~ wt + cyl, 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.