Using package specific functions with parsnip

Viewed 64

I'm trying to learn the R's modeling framework tidymodels. After creating the model and specifying the package (engine) I want to use for my model, I now try to use some specific functions that are inside of the engine I chose. In this case it is the randomForest package and the varImpPlot() function that I'm trying to use. However, this error shows up when I try to execute it where it says that in order to use the function the object has to be a randomForest object. Well, this is obvious, but my question would be, is there some way to translate the parsnip object to the object of the engine I chose, or some way to use these functions inside of the package I have chosen? Thanks for help!

model_rand_forest <- rand_forest() %>% 
  set_engine("randomForest") %>%
  set_mode("regression") %>%
  translate()

training_workflow <- workflow() %>%
  add_recipe(recipe) %>%
  add_model(model_rand_forest) 

training_workflow_fit <- training_workflow %>% fit(data = train)
training_workflow_fit %>% varImpPlot()

training_workflow_fit %>% varImpPlot()
Error in varImpPlot(.) : 
  This function only works for objects of class `randomForest'
1 Answers

You can extract the randomForest object from the workflow by using $fit$fit$fit. In your example, this should work

training_workflow_fit$fit$fit$fit %>% varImpPlot()

Or you could use the below syntax, which may be more neat

training_workflow_fit %>% 
  chuck("fit") %>% 
  chuck("fit") %>% 
  chuck("fit") %>% 
  varImpPlot()
Related