Get startedGet started for free

Create full random forest model

Random forest models naturally perform feature selection as they build many subtrees from random subsets of the features. One way to understand feature importances is to build a model and then extract the feature importances. So, in this exercise, you will use the Healthcare Job Attrition data to train a rand_forest() classification model from which you can extract feature importances. To make feature importances available, be sure to create the model with importance = "impurity". The train and test sets are available to you.

The tidyverse, tidymodels, and vip packages have been loaded for you.

This exercise is part of the course

Dimensionality Reduction in R

View Course

Exercise instructions

  • Define a random forest classification model with 200 trees that you can use to extract feature importances.
  • Fit the random forest model with all predictors.
  • Bind the predictions to the test set.
  • Calculate the F1 metric.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Specify the random forest model
rf_spec <- ___(mode = "___", ___ = ___) %>% 
  set_engine("___", ___ = "___") 

# Fit the random forest model with all predictors
rf_fit <- ___ %>% 
  ___(___, data = ___)

# Create the test set prediction data frame
predict_df <- ___ %>% 
  bind_cols(predict = ___(___, ___))

# Calculate F1 performance
f_meas(predict_df, ___, .pred_class)
Edit and Run Code