Predicting children in hotel bookings

Suggested answers

Application exercise
Answers

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.

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_curve(truth = children, .pred_children) |>
  autoplot()

Acknowledgments

sessioninfo::session_info()
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.1 (2023-06-16)
 os       macOS Ventura 13.5.2
 system   aarch64, darwin20
 ui       X11
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/New_York
 date     2023-11-06
 pandoc   3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)

─ Packages ───────────────────────────────────────────────────────────────────
 package      * version    date (UTC) lib source
 backports      1.4.1      2021-12-13 [1] CRAN (R 4.3.0)
 bit            4.0.5      2022-11-15 [1] CRAN (R 4.3.0)
 bit64          4.0.5      2020-08-30 [1] CRAN (R 4.3.0)
 broom        * 1.0.5      2023-06-09 [1] CRAN (R 4.3.0)
 C50          * 0.1.8      2023-02-08 [1] CRAN (R 4.3.0)
 class          7.3-22     2023-05-03 [1] CRAN (R 4.3.0)
 cli            3.6.1      2023-03-23 [1] CRAN (R 4.3.0)
 codetools      0.2-19     2023-02-01 [1] CRAN (R 4.3.0)
 colorspace     2.1-0      2023-01-23 [1] CRAN (R 4.3.0)
 crayon         1.5.2      2022-09-29 [1] CRAN (R 4.3.0)
 Cubist         0.4.2.1    2023-03-09 [1] CRAN (R 4.3.0)
 data.table     1.14.8     2023-02-17 [1] CRAN (R 4.3.0)
 dials        * 1.2.0      2023-04-03 [1] CRAN (R 4.3.0)
 DiceDesign     1.9        2021-02-13 [1] CRAN (R 4.3.0)
 digest         0.6.33     2023-07-07 [1] CRAN (R 4.3.0)
 dplyr        * 1.1.3      2023-09-03 [1] CRAN (R 4.3.0)
 evaluate       0.22       2023-09-29 [1] CRAN (R 4.3.1)
 fansi          1.0.5      2023-10-08 [1] CRAN (R 4.3.1)
 farver         2.1.1      2022-07-06 [1] CRAN (R 4.3.0)
 fastmap        1.1.1      2023-02-24 [1] CRAN (R 4.3.0)
 forcats      * 1.0.0      2023-01-29 [1] CRAN (R 4.3.0)
 foreach        1.5.2      2022-02-02 [1] CRAN (R 4.3.0)
 Formula        1.2-5      2023-02-24 [1] CRAN (R 4.3.0)
 furrr          0.3.1      2022-08-15 [1] CRAN (R 4.3.0)
 future         1.32.0     2023-03-07 [1] CRAN (R 4.3.0)
 future.apply   1.11.0     2023-05-21 [1] CRAN (R 4.3.0)
 generics       0.1.3      2022-07-05 [1] CRAN (R 4.3.0)
 ggplot2      * 3.4.2      2023-04-03 [1] CRAN (R 4.3.0)
 globals        0.16.2     2022-11-21 [1] CRAN (R 4.3.0)
 glue           1.6.2      2022-02-24 [1] CRAN (R 4.3.0)
 gower          1.0.1      2022-12-22 [1] CRAN (R 4.3.0)
 GPfit          1.0-8      2019-02-08 [1] CRAN (R 4.3.0)
 gtable         0.3.3      2023-03-21 [1] CRAN (R 4.3.0)
 hardhat        1.3.0      2023-03-30 [1] CRAN (R 4.3.0)
 here           1.0.1      2020-12-13 [1] CRAN (R 4.3.0)
 hms            1.1.3      2023-03-21 [1] CRAN (R 4.3.0)
 htmltools      0.5.6.1    2023-10-06 [1] CRAN (R 4.3.1)
 htmlwidgets    1.6.2      2023-03-17 [1] CRAN (R 4.3.0)
 infer        * 1.0.4      2022-12-02 [1] CRAN (R 4.3.0)
 inum           1.0-5      2023-03-09 [1] CRAN (R 4.3.0)
 ipred          0.9-14     2023-03-09 [1] CRAN (R 4.3.0)
 iterators      1.0.14     2022-02-05 [1] CRAN (R 4.3.0)
 jsonlite       1.8.7      2023-06-29 [1] CRAN (R 4.3.0)
 knitr          1.44       2023-09-11 [1] CRAN (R 4.3.0)
 labeling       0.4.2      2020-10-20 [1] CRAN (R 4.3.0)
 lattice        0.21-8     2023-04-05 [1] CRAN (R 4.3.0)
 lava           1.7.2.1    2023-02-27 [1] CRAN (R 4.3.0)
 lhs            1.1.6      2022-12-17 [1] CRAN (R 4.3.0)
 libcoin        1.0-9      2021-09-27 [1] CRAN (R 4.3.0)
 lifecycle      1.0.3      2022-10-07 [1] CRAN (R 4.3.0)
 listenv        0.9.0      2022-12-16 [1] CRAN (R 4.3.0)
 lubridate    * 1.9.2      2023-02-10 [1] CRAN (R 4.3.0)
 magrittr       2.0.3      2022-03-30 [1] CRAN (R 4.3.0)
 MASS           7.3-60     2023-05-04 [1] CRAN (R 4.3.0)
 Matrix         1.5-4.1    2023-05-18 [1] CRAN (R 4.3.0)
 modeldata    * 1.1.0      2023-01-25 [1] CRAN (R 4.3.0)
 modelenv       0.1.1      2023-03-08 [1] CRAN (R 4.3.0)
 munsell        0.5.0      2018-06-12 [1] CRAN (R 4.3.0)
 mvtnorm        1.2-2      2023-06-08 [1] CRAN (R 4.3.0)
 nnet           7.3-19     2023-05-03 [1] CRAN (R 4.3.0)
 parallelly     1.36.0     2023-05-26 [1] CRAN (R 4.3.0)
 parsnip      * 1.1.0      2023-04-12 [1] CRAN (R 4.3.0)
 partykit       1.2-20     2023-04-14 [1] CRAN (R 4.3.0)
 pillar         1.9.0      2023-03-22 [1] CRAN (R 4.3.0)
 pkgconfig      2.0.3      2019-09-22 [1] CRAN (R 4.3.0)
 plyr           1.8.8      2022-11-11 [1] CRAN (R 4.3.0)
 prodlim        2023.03.31 2023-04-02 [1] CRAN (R 4.3.0)
 purrr        * 1.0.2      2023-08-10 [1] CRAN (R 4.3.0)
 R6             2.5.1      2021-08-19 [1] CRAN (R 4.3.0)
 Rcpp           1.0.10     2023-01-22 [1] CRAN (R 4.3.0)
 readr        * 2.1.4      2023-02-10 [1] CRAN (R 4.3.0)
 recipes      * 1.0.6      2023-04-25 [1] CRAN (R 4.3.0)
 reshape2       1.4.4      2020-04-09 [1] CRAN (R 4.3.0)
 rlang          1.1.1      2023-04-28 [1] CRAN (R 4.3.0)
 rmarkdown      2.25       2023-09-18 [1] CRAN (R 4.3.1)
 rpart          4.1.19     2022-10-21 [1] CRAN (R 4.3.0)
 rprojroot      2.0.3      2022-04-02 [1] CRAN (R 4.3.0)
 rsample      * 1.1.1      2022-12-07 [1] CRAN (R 4.3.0)
 rstudioapi     0.14       2022-08-22 [1] CRAN (R 4.3.0)
 scales       * 1.2.1      2022-08-20 [1] CRAN (R 4.3.0)
 sessioninfo    1.2.2      2021-12-06 [1] CRAN (R 4.3.0)
 stringi        1.7.12     2023-01-11 [1] CRAN (R 4.3.0)
 stringr      * 1.5.0      2022-12-02 [1] CRAN (R 4.3.0)
 survival       3.5-5      2023-03-12 [1] CRAN (R 4.3.0)
 tibble       * 3.2.1      2023-03-20 [1] CRAN (R 4.3.0)
 tidymodels   * 1.1.0      2023-05-01 [1] CRAN (R 4.3.0)
 tidyr        * 1.3.0      2023-01-24 [1] CRAN (R 4.3.0)
 tidyselect     1.2.0      2022-10-10 [1] CRAN (R 4.3.0)
 tidyverse    * 2.0.0      2023-02-22 [1] CRAN (R 4.3.0)
 timechange     0.2.0      2023-01-11 [1] CRAN (R 4.3.0)
 timeDate       4022.108   2023-01-07 [1] CRAN (R 4.3.0)
 tune         * 1.1.1      2023-04-11 [1] CRAN (R 4.3.0)
 tzdb           0.4.0      2023-05-12 [1] CRAN (R 4.3.0)
 utf8           1.2.3      2023-01-31 [1] CRAN (R 4.3.0)
 vctrs          0.6.4      2023-10-12 [1] CRAN (R 4.3.1)
 vroom          1.6.3      2023-04-28 [1] CRAN (R 4.3.0)
 withr          2.5.1      2023-09-26 [1] CRAN (R 4.3.1)
 workflows    * 1.1.3      2023-02-22 [1] CRAN (R 4.3.0)
 workflowsets * 1.0.1      2023-04-06 [1] CRAN (R 4.3.0)
 xfun           0.40       2023-08-09 [1] CRAN (R 4.3.0)
 yaml           2.3.7      2023-01-23 [1] CRAN (R 4.3.0)
 yardstick    * 1.2.0      2023-04-21 [1] CRAN (R 4.3.0)

 [1] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library

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