Random Forest, using Ranger

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

library(dplyr)
library(tidypredict)
library(parsnip)
library(ranger)
set.seed(100)

| Function |Works| |---------------------------------------------------------------|-----| |tidypredict_fit(), tidypredict_sql(), parse_model() | ✔ | |tidypredict_to_column() | ✗ | |tidypredict_test() | ✗ | |tidypredict_interval(), tidypredict_sql_interval() | ✗ | |parsnip | ✔ |

How it works

Here is a simple ranger() model using the iris dataset:

library(dplyr)
library(tidypredict)
library(ranger)

model <- ranger(Species ~ .,data = iris, num.trees =  100)

Under the hood

The parser is based on the output from the ranger::treeInfo() function. It will return as many decision paths as there are non-NA rows in the prediction field.

treeInfo(model) %>%
  head()

The output from parse_model() is transformed into a dplyr, a.k.a Tidy Eval, formula. The entire decision tree becomes one dplyr::case_when() statement

tidypredict_fit(model)[1]

From there, the Tidy Eval formula can be used anywhere where it can be operated. tidypredict provides three paths:

parsnip

tidypredict also supports ranger model objects fitted via the parsnip package.

library(parsnip)

parsnip_model <- rand_forest(mode = "classification") %>%
  set_engine("ranger") %>%
  fit(Species ~ ., data = iris)

tidypredict_fit(parsnip_model)[[1]]


Try the tidypredict package in your browser

Any scripts or data that you put into this service are public.

tidypredict documentation built on Oct. 28, 2020, 9:07 a.m.