+ - 0:00:00
Notes for current slide
Notes for next slide

Random Forests

Dr. D’Agostino McGowan

1 / 9

The heart disease example

Recall that we are predicting whether a patient has heart disease from 13 predictors

2 / 9

1. Randomly divide the data in half, 149 training observations, 148 testing

set.seed(77)
heart_split <- initial_split(heart, prop = 0.5)
heart_train <- training(heart_split)
3 / 9

2. Create model specification

model_spec <- rand_forest(
mode = "classification",
mtry = ---
) %>%
set_engine("ranger")

mtry here is m. If we are doing bagging what do you think we set this to?

4 / 9

2. Create bagging specification

bagging_spec <- rand_forest(
mode = "classification",
mtry = 13
) %>%
set_engine("ranger")
5 / 9

2. Create bagging specification

bagging_spec <- rand_forest(
mode = "classification",
mtry = 13
) %>%
set_engine("ranger")

What would we change mtry to if we are doing a random forest?

5 / 9

2. Create Random Forest specification

rf_spec <- rand_forest(
mode = "classification",
mtry = 3
) %>%
set_engine("ranger")
6 / 9

2. Create Random Forest specification

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)
6 / 9

3. Fit the model

model <- fit(rf_spec,
HD ~ Age + Sex + ChestPain + RestBP + Chol + Fbs +
RestECG + MaxHR + ExAng + Oldpeak + Slope + Ca + Thal,
data = heart_train)
7 / 9

4. Examine how it looks in the test data

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")

8 / 9
10:00

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
9 / 9

The heart disease example

Recall that we are predicting whether a patient has heart disease from 13 predictors

2 / 9
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow