AE 14: Predicting children in hotel bookings

Suggested answers

Application exercise
Answers
Modified

October 22, 2025

Your Turn 1

Run the chunk below and look at the output. Then, copy/paste the code and edit to create:

  • a decision tree model for classification

  • that uses the {C5.0} engine.

Save it as tree_mod and look at the object. What is different about the output?

Hint: you’ll need https://www.tidymodels.org/find/parsnip/

lr_mod <- logistic_reg() |>
  set_engine(engine = "glm") |>
  set_mode("classification")
lr_mod
Logistic Regression Model Specification (classification)

Computational engine: glm 
tree_mod <- decision_tree() |>
  set_engine(engine = "C5.0") |>
  set_mode("classification")
tree_mod
Decision Tree Model Specification (classification)

Computational engine: C5.0 

Your Turn 2

Fill in the blanks.

Use initial_split(), training(), and testing() to:

  1. Split hotels into training and test sets. Save the rsplit!

  2. Extract the training data and fit your classification tree model.

  3. Check the proportions of the test variable in each set.

Keep set.seed(100) at the start of your code.

Hint: Be sure to remove every _ before running the code!

set.seed(100) # Important!

hotels_split <- initial_split(data = hotels, prop = 3 / 4)
hotels_train <- training(hotels_split)
hotels_test <- testing(hotels_split)

# check distribution
count(x = hotels_train, children) |>
  mutate(prop = n / sum(n))
# A tibble: 2 × 3
  children     n  prop
  <fct>    <int> <dbl>
1 children  1503 0.501
2 none      1497 0.499
count(x = hotels_test, children) |>
  mutate(prop = n / sum(n))
# A tibble: 2 × 3
  children     n  prop
  <fct>    <int> <dbl>
1 children   497 0.497
2 none       503 0.503

Your Turn 3

Run the code below. What does it return?

set.seed(100)
hotels_folds <- vfold_cv(data = hotels_train, v = 10)
hotels_folds
#  10-fold cross-validation 
# A tibble: 10 × 2
   splits             id    
   <list>             <chr> 
 1 <split [2700/300]> Fold01
 2 <split [2700/300]> Fold02
 3 <split [2700/300]> Fold03
 4 <split [2700/300]> Fold04
 5 <split [2700/300]> Fold05
 6 <split [2700/300]> Fold06
 7 <split [2700/300]> Fold07
 8 <split [2700/300]> Fold08
 9 <split [2700/300]> Fold09
10 <split [2700/300]> Fold10

Your Turn 4

Add a autoplot() to visualize the ROC AUC. How well does the model perform?

tree_preds <- tree_mod |>
  fit_resamples(
    children ~ average_daily_rate + stays_in_weekend_nights,
    resamples = hotels_folds,
    control = control_resamples(save_pred = TRUE)
  )

tree_preds |>
  collect_predictions() |>
  roc_auc(truth = children, .pred_children)
# A tibble: 1 × 3
  .metric .estimator .estimate
  <chr>   <chr>          <dbl>
1 roc_auc binary         0.670
tree_preds |>
  collect_predictions() |>
  roc_curve(truth = children, .pred_children) |>
  autoplot()

It’s moderately successful. Better than \(0.5\), but still has a lot of room for improvement.

Acknowledgments

sessioninfo::session_info()
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.5.1 (2025-06-13)
 os       macOS Tahoe 26.0.1
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/New_York
 date     2025-10-27
 pandoc   3.4 @ /usr/local/bin/ (via rmarkdown)
 quarto   1.8.24 @ /usr/local/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────
 ! package      * version    date (UTC) lib source
 P backports      1.5.0      2024-05-23 [?] RSPM (R 4.5.0)
 P bit            4.6.0      2025-03-06 [?] RSPM (R 4.5.0)
 P bit64          4.6.0-1    2025-01-16 [?] RSPM (R 4.5.0)
 P broom        * 1.0.9      2025-07-28 [?] RSPM (R 4.5.0)
 P C50            0.2.0      2025-04-03 [?] RSPM
 P class          7.3-23     2025-01-01 [?] RSPM (R 4.5.0)
 P cli            3.6.5      2025-04-23 [?] RSPM (R 4.5.0)
 P codetools      0.2-20     2024-03-31 [?] RSPM (R 4.5.0)
 P crayon         1.5.3      2024-06-20 [?] RSPM (R 4.5.0)
 P Cubist         0.5.0      2025-04-03 [?] RSPM (R 4.5.0)
 P data.table     1.17.8     2025-07-10 [?] RSPM (R 4.5.0)
 P dials        * 1.4.2      2025-09-04 [?] RSPM
 P DiceDesign     1.10       2023-12-07 [?] RSPM (R 4.5.0)
 P digest         0.6.37     2024-08-19 [?] RSPM (R 4.5.0)
 P dplyr        * 1.1.4      2023-11-17 [?] RSPM (R 4.5.0)
 P evaluate       1.0.4      2025-06-18 [?] RSPM (R 4.5.1)
 P farver         2.1.2      2024-05-13 [?] RSPM (R 4.5.0)
 P fastmap        1.2.0      2024-05-15 [?] RSPM (R 4.5.0)
 P forcats      * 1.0.0      2023-01-29 [?] RSPM (R 4.5.0)
 P Formula        1.2-5      2023-02-24 [?] RSPM (R 4.5.0)
 P furrr          0.3.1      2022-08-15 [?] RSPM
 P future         1.67.0     2025-07-29 [?] RSPM
 P future.apply   1.20.0     2025-06-06 [?] RSPM
 P generics       0.1.4      2025-05-09 [?] RSPM (R 4.5.0)
 P ggplot2      * 3.5.2      2025-04-09 [?] RSPM (R 4.5.0)
 P globals        0.18.0     2025-05-08 [?] RSPM
 P glue           1.8.0      2024-09-30 [?] RSPM (R 4.5.0)
 P gower          1.0.2      2024-12-17 [?] RSPM
 P GPfit          1.0-9      2025-04-12 [?] RSPM (R 4.5.0)
 P gtable         0.3.6      2024-10-25 [?] RSPM (R 4.5.0)
 P hardhat        1.4.2      2025-08-20 [?] RSPM
 P here           1.0.1      2020-12-13 [?] RSPM (R 4.5.0)
 P hms            1.1.3      2023-03-21 [?] RSPM (R 4.5.0)
 P htmltools      0.5.8.1    2024-04-04 [?] RSPM (R 4.5.0)
 P htmlwidgets    1.6.4      2023-12-06 [?] RSPM (R 4.5.0)
 P infer        * 1.0.9      2025-06-26 [?] RSPM
 P inum           1.0-5      2023-03-09 [?] RSPM
 P ipred          0.9-15     2024-07-18 [?] RSPM
 P jsonlite       2.0.0      2025-03-27 [?] RSPM (R 4.5.0)
 P knitr          1.50       2025-03-16 [?] RSPM (R 4.5.0)
 P labeling       0.4.3      2023-08-29 [?] RSPM (R 4.5.0)
 P lattice        0.22-7     2025-04-02 [?] RSPM (R 4.5.0)
 P lava           1.8.1      2025-01-12 [?] RSPM
 P lhs            1.2.0      2024-06-30 [?] RSPM (R 4.5.0)
 P libcoin        1.0-10     2023-09-27 [?] RSPM
 P lifecycle      1.0.4      2023-11-07 [?] RSPM (R 4.5.0)
 P listenv        0.9.1      2024-01-29 [?] RSPM
 P lubridate    * 1.9.4      2024-12-08 [?] RSPM (R 4.5.0)
 P magrittr       2.0.3      2022-03-30 [?] RSPM (R 4.5.1)
 P MASS           7.3-65     2025-02-28 [?] RSPM (R 4.5.0)
 P Matrix         1.7-3      2025-03-11 [?] RSPM (R 4.5.0)
 P modeldata    * 1.5.1      2025-08-22 [?] RSPM
 P modelenv       0.2.0      2024-10-14 [?] RSPM
 P mvtnorm        1.3-3      2025-01-10 [?] RSPM
 P nnet           7.3-20     2025-01-01 [?] RSPM (R 4.5.0)
 P parallelly     1.45.1     2025-07-24 [?] RSPM
 P parsnip      * 1.3.3      2025-08-31 [?] RSPM
 P partykit       1.2-24     2025-05-02 [?] RSPM
 P pillar         1.11.0     2025-07-04 [?] RSPM (R 4.5.1)
 P pkgconfig      2.0.3      2019-09-22 [?] RSPM (R 4.5.0)
 P plyr           1.8.9      2023-10-02 [?] RSPM (R 4.5.0)
 P prodlim        2025.04.28 2025-04-28 [?] RSPM
 P purrr        * 1.1.0      2025-07-10 [?] RSPM (R 4.5.0)
 P R6             2.6.1      2025-02-15 [?] RSPM (R 4.5.0)
 P RColorBrewer   1.1-3      2022-04-03 [?] RSPM (R 4.5.0)
 P Rcpp           1.1.0      2025-07-02 [?] RSPM (R 4.5.0)
 P readr        * 2.1.5      2024-01-10 [?] RSPM (R 4.5.0)
 P recipes      * 1.3.1      2025-05-21 [?] RSPM
 P renv           1.1.5      2025-07-24 [?] RSPM
 P reshape2       1.4.4      2020-04-09 [?] RSPM (R 4.5.0)
 P rlang          1.1.6      2025-04-11 [?] RSPM (R 4.5.0)
 P rmarkdown      2.29       2024-11-04 [?] RSPM
 P rpart          4.1.24     2025-01-07 [?] RSPM (R 4.5.0)
 P rprojroot      2.1.0      2025-07-12 [?] RSPM (R 4.5.0)
 P rsample      * 1.3.1      2025-07-29 [?] RSPM
 P rstudioapi     0.17.1     2024-10-22 [?] RSPM (R 4.5.0)
 P scales       * 1.4.0      2025-04-24 [?] RSPM (R 4.5.0)
 P sessioninfo    1.2.3      2025-02-05 [?] RSPM (R 4.5.0)
 P sparsevctrs    0.3.4      2025-05-25 [?] RSPM
 P stringi        1.8.7      2025-03-27 [?] RSPM (R 4.5.0)
 P stringr      * 1.5.1      2023-11-14 [?] RSPM (R 4.5.1)
 P survival       3.8-3      2024-12-17 [?] RSPM (R 4.5.0)
 P tailor       * 0.1.0      2025-08-25 [?] RSPM
 P tibble       * 3.3.0      2025-06-08 [?] RSPM (R 4.5.0)
 P tidymodels   * 1.4.1      2025-09-08 [?] RSPM
 P tidyr        * 1.3.1      2024-01-24 [?] RSPM (R 4.5.0)
 P tidyselect     1.2.1      2024-03-11 [?] RSPM (R 4.5.0)
 P tidyverse    * 2.0.0      2023-02-22 [?] RSPM (R 4.5.0)
 P timechange     0.3.0      2024-01-18 [?] RSPM (R 4.5.0)
 P timeDate       4041.110   2024-09-22 [?] RSPM
 P tune         * 2.0.0      2025-09-01 [?] RSPM
 P tzdb           0.5.0      2025-03-15 [?] RSPM (R 4.5.0)
 P utf8           1.2.6      2025-06-08 [?] RSPM (R 4.5.0)
 P vctrs          0.6.5      2023-12-01 [?] RSPM (R 4.5.0)
 P vroom          1.6.5      2023-12-05 [?] RSPM (R 4.5.1)
 P withr          3.0.2      2024-10-28 [?] RSPM (R 4.5.0)
 P workflows    * 1.3.0      2025-08-27 [?] RSPM
 P workflowsets * 1.1.1      2025-05-27 [?] RSPM
 P xfun           0.52       2025-04-02 [?] RSPM (R 4.5.1)
 P yaml           2.3.10     2024-07-26 [?] RSPM (R 4.5.0)
 P yardstick    * 1.3.2      2025-01-22 [?] RSPM

 [1] /Users/bcs88/Projects/info-5001/course-site/renv/library/macos/R-4.5/aarch64-apple-darwin20
 [2] /Users/bcs88/Library/Caches/org.R-project.R/R/renv/sandbox/macos/R-4.5/aarch64-apple-darwin20/4cd76b74

 * ── Packages attached to the search path.
 P ── Loaded and on-disk path mismatch.

──────────────────────────────────────────────────────────────────────────────