class: center, middle, inverse, title-slide # Random Forests ### Dr. D’Agostino McGowan --- layout: true <div class="my-footer"> <span> Dr. Lucy D'Agostino McGowan <i>adapted from slides by Hastie & Tibshirani</i> </span> </div> --- class: center, middle ## The heart disease example _Recall that we are predicting whether a patient has heart disease from 13 predictors_ --- ## 1. Randomly divide the data in half, 149 training observations, 148 testing ```r set.seed(77) heart_split <- initial_split(heart, prop = 0.5) heart_train <- training(heart_split) ``` --- ## 2. Create model specification ```r model_spec <- rand_forest( mode = "classification", mtry = --- ) %>% set_engine("ranger") ``` .question[ `mtry` here is `m`. If we are doing _bagging_ what do you think we set this to? ] --- ## 2. Create bagging specification ```r bagging_spec <- rand_forest( mode = "classification", * mtry = 13 ) %>% set_engine("ranger") ``` -- .question[ What would we change `mtry` to if we are doing a random forest? ] --- ## 2. Create Random Forest specification ```r rf_spec <- rand_forest( mode = "classification", * mtry = 3 ) %>% set_engine("ranger") ``` -- * The default for `rand_forest` is `floor(sqrt(# predictors))` (so 3 in this case) --- ## 3. Fit the model ```r model <- fit(rf_spec, HD ~ Age + Sex + ChestPain + RestBP + Chol + Fbs + RestECG + MaxHR + ExAng + Oldpeak + Slope + Ca + Thal, data = heart_train) ``` --- ## 4. Examine how it looks in the test data ```r heart_test <- testing(heart_split) model %>% predict(new_data = heart_test) %>% bind_cols(heart_test) %>% conf_mat(truth = HD, estimate = .pred_class) %>% autoplot(type = "heatmap") ``` ![](18-random-forest-in-r_files/figure-html/unnamed-chunk-9-1.png)<!-- --> --- class: inverse
10
:
00
## <i class="fas fa-laptop"></i> `AE 05 - Random Forests` 1. Open your application exercise (05) from last class 2. Refit your model as a _random forest_ 3. Knit, Commit, Push. Be sure to have the final results pushed to GitHub by **April 10 at noon**