I am using library(partykit) to sample from nodes from an rpart model created with library(tidymodels). I need to use repair_call() to use as.party() with output from parsnip::fit(). This breaks when I move the code inside of a custom function:
library(tidymodels)
library(partykit)
cart_model <- parsnip::decision_tree() %>%
parsnip::set_engine("rpart") %>%
parsnip::set_mode("regression")
parsnip_model <- fit(cart_model, mpg ~ ., data = mtcars)
predict_sample_rpart <- function(object, old_data, new_data) {
repaired_model <- repair_call(object, data = old_data)
node_ecdf <- predict(as.party(repaired_model$fit), newdata = new_data, type = "prob")
sample(environment(node_ecdf[["1"]])[["x"]], 1)
}
predict_sample_rpart(parsnip_model, old_data = mtcars, new_data = mtcars)
#> Error in is.data.frame(data) : object 'old_data' not found
repair_call() assigns the data in repaired_model as old_data instead of mtcars and then predict() does not work.
- Any help fixing this would be greatly appreciated.
- Any suggestions for better ways to sample from conditional distributions created by
lm(), rpart(), ranger() would be doubly appreciated.