1

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'
Ata
  • 27
  • 6

1 Answers1

0

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()
hnagaty
  • 796
  • 5
  • 13
  • 2
    You can also use the [function `pull_workflow_fit()` to get the parsnip fit](https://workflows.tidymodels.org/reference/workflow-extractors.html) out of a workflow object. Then it is just one more level down (i.e. one more `$fit`) to get the `randomForest` object inside of it. – Julia Silge Jan 05 '21 at 21:18