nyt_titles <- readr::read_tsv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2022/2022-05-10/nyt_titles.tsv')
## Rows: 7431 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr  (2): title, author
## dbl  (5): id, year, total_weeks, debut_rank, best_rank
## date (1): first_week
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
skimr::skim(nyt_titles)
Data summary
Name nyt_titles
Number of rows 7431
Number of columns 8
_______________________
Column type frequency:
character 2
Date 1
numeric 5
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
title 0 1 1 74 0 7172 0
author 4 1 4 73 0 2205 0

Variable type: Date

skim_variable n_missing complete_rate min max median n_unique
first_week 0 1 1931-10-12 2020-12-06 2000-06-25 3348

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
id 0 1 3715.00 2145.29 0 1857.5 3715 5572.5 7430 ▇▇▇▇▇
year 0 1 1989.61 26.23 1931 1968.0 2000 2011.0 2020 ▂▂▂▃▇
total_weeks 0 1 8.13 11.21 1 2.0 4 10.0 178 ▇▁▁▁▁
debut_rank 0 1 7.90 4.57 1 4.0 8 12.0 17 ▇▆▅▅▅
best_rank 0 1 6.91 4.57 1 3.0 6 10.0 17 ▇▅▃▃▂
data1 <- nyt_titles %>% 
  
  #treat missing values
  select(-id, -year, -first_week) %>%
  na.omit() %>%

  #log transform variables with pos-skewed distribution
  mutate(total_weeks = log(total_weeks))
data_binarized_tbl1 <- data1 %>%
  select(-author) %>%
  binarize()

#step 2 correlate
data_corr_tbl1 <- data_binarized_tbl1 %>%
  correlate(total_weeks__0.693147180559945_1.38629436111989)

data_corr_tbl1 %>%
  plot_correlation_funnel()
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## ℹ The deprecated feature was likely used in the correlationfunnel package.
##   Please report the issue at
##   <https://github.com/business-science/correlationfunnel/issues>.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## ℹ The deprecated feature was likely used in the correlationfunnel package.
##   Please report the issue at
##   <https://github.com/business-science/correlationfunnel/issues>.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Split data

#data <- sample_n(data, 100)

#split into train and test dataset
set.seed(1234)
data_split <- rsample::initial_split(data1)
data_train <- training(data_split)
data_test <- testing(data_split)

#further split training dataset for cross-validation
set.seed(2345)
data_cv <- rsample::vfold_cv(data_train)
data_cv
## #  10-fold cross-validation 
## # A tibble: 10 × 2
##    splits             id    
##    <list>             <chr> 
##  1 <split [5013/557]> Fold01
##  2 <split [5013/557]> Fold02
##  3 <split [5013/557]> Fold03
##  4 <split [5013/557]> Fold04
##  5 <split [5013/557]> Fold05
##  6 <split [5013/557]> Fold06
##  7 <split [5013/557]> Fold07
##  8 <split [5013/557]> Fold08
##  9 <split [5013/557]> Fold09
## 10 <split [5013/557]> Fold10
library(usemodels)
usemodels::use_xgboost(total_weeks ~ ., data = data_train)
## xgboost_recipe <- 
##   recipe(formula = total_weeks ~ ., data = data_train) %>% 
##   step_zv(all_predictors()) 
## 
## xgboost_spec <- 
##   boost_tree(trees = tune(), min_n = tune(), tree_depth = tune(), learn_rate = tune(), 
##     loss_reduction = tune(), sample_size = tune()) %>% 
##   set_mode("classification") %>% 
##   set_engine("xgboost") 
## 
## xgboost_workflow <- 
##   workflow() %>% 
##   add_recipe(xgboost_recipe) %>% 
##   add_model(xgboost_spec) 
## 
## set.seed(41811)
## xgboost_tune <-
##   tune_grid(xgboost_workflow, resamples = stop("add your rsample object"), grid = stop("add number of candidate points"))
xgboost_recipe <- 
  recipe(formula = total_weeks ~ ., data = data_train) %>% 
  step_tokenize(title) %>%
  step_tokenfilter(title, max_tokens = 100) %>%
  step_tfidf(title) %>%
  step_other(author, threshold = 0.003) %>%
  step_dummy(author) %>%
  step_zv(all_predictors()) 

xgboost_recipe %>% prep() %>% juice() %>% glimpse
## Rows: 5,570
## Columns: 144
## $ debut_rank                               <dbl> 8, 4, 14, 4, 12, 12, 10, 6, 7…
## $ best_rank                                <dbl> 10, 6, 13, 1, 6, 4, 7, 5, 8, …
## $ total_weeks                              <dbl> 2.1972246, 0.6931472, 0.00000…
## $ tfidf_title_a                            <dbl> 1.033044, 0.000000, 0.000000,…
## $ tfidf_title_after                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_all                          <dbl> 0.000000, 0.000000, 0.000000,…
## $ tfidf_title_an                           <dbl> 0.000000, 0.000000, 0.000000,…
## $ tfidf_title_and                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_are                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_as                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_at                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_big                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_black                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_blood                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_blue                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_bones                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_book                         <dbl> 0.000000, 0.000000, 0.000000,…
## $ tfidf_title_cat                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_christmas                    <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_city                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_cold                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_dark                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_day                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_days                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_dead                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_death                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_die                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_down                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_edge                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_fall                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_fire                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_first                        <dbl> 0.000000, 0.000000, 0.000000,…
## $ tfidf_title_for                          <dbl> 1.415046, 0.000000, 0.000000,…
## $ tfidf_title_from                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_game                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_girl                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_girls                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_god                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_golden                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_gone                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_good                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_heart                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_heaven                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_high                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_home                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_honor                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_house                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_i                            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_in                           <dbl> 0.000000, 0.000000, 0.000000,…
## $ tfidf_title_is                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_island                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_lady                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_last                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_life                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_light                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_little                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_long                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_lost                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_love                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_man                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_me                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_men                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_midnight                     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_moon                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_mr                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_murder                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_my                           <dbl> 0.000000, 0.000000, 0.000000,…
## $ tfidf_title_new                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_night                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_no                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_not                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_of                           <dbl> 0.0000000, 0.0000000, 0.00000…
## $ tfidf_title_on                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_one                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_prey                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_red                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_river                        <dbl> 0.000000, 0.000000, 0.000000,…
## $ tfidf_title_road                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_sea                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_second                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_secret                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_shadow                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_son                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_star                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_storm                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_street                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_summer                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_the                          <dbl> 0.4312176, 0.0000000, 0.00000…
## $ tfidf_title_this                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_three                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_time                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_to                           <dbl> 0.000000, 0.000000, 0.000000,…
## $ tfidf_title_tree                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_two                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_we                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_white                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_who                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_wife                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_wind                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_with                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_woman                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_world                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ tfidf_title_you                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Anne.Rice                         <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Barbara.Taylor.Bradford           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Catherine.Coulter                 <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Danielle.Steel                    <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_David.Baldacci                    <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Dean.Koontz                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Debbie.Macomber                   <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Dick.Francis                      <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Douglas.Preston.and.Lincoln.Child <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Harlan.Coben                      <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Iris.Johansen                     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_J..A..Jance                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_J..D..Robb                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Jack.Higgins                      <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_James.Lee.Burke                   <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_James.Patterson                   <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_James.Patterson.and.Maxine.Paetro <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Janet.Evanovich                   <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Jeffrey.Archer                    <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_John.Grisham                      <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_John.le.Carré                     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_John.Sandford                     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Jonathan.Kellerman                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Ken.Follett                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Lisa.Scottoline                   <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Mary.Higgins.Clark                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Michael.Connelly                  <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Nora.Roberts                      <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Patricia.Cornwell                 <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Richard.Paul.Evans                <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Robert.B..Parker                  <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Robert.Ludlum                     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Robin.Cook                        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Sandra.Brown                      <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Stephen.King                      <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Stuart.Woods                      <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Sue.Grafton                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Taylor.Caldwell                   <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_Terry.Brooks                      <dbl> 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,…
## $ author_W..E..B..Griffin                  <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
## $ author_other                             <dbl> 1, 1, 1, 1, 1, 1, 1, 0, 1, 1,…
xgboost_spec <- 
  boost_tree(trees = tune(), min_n = tune(), tree_depth = tune(), learn_rate = tune())%>% 
  set_mode("regression") %>% 
  set_engine("xgboost") 

xgboost_workflow <- 
  workflow() %>% 
  add_recipe(xgboost_recipe) %>% 
  add_model(xgboost_spec) 

set.seed(12193) 
xgboost_tune <-
  tune_grid(xgboost_workflow, 
            resamples = data_cv,
            grid = 5)

#evaluate models

tune::show_best(xgboost_tune, metric = "rmse")
## # A tibble: 5 × 10
##   trees min_n tree_depth learn_rate .metric .estimator  mean     n std_err
##   <int> <int>      <int>      <dbl> <chr>   <chr>      <dbl> <int>   <dbl>
## 1  1000     2          1    0.0750  rmse    standard   0.830    10 0.00613
## 2  2000    40          8    0.0178  rmse    standard   0.833    10 0.00711
## 3  1500    11         11    0.001   rmse    standard   0.842    10 0.00511
## 4   500    21         15    0.316   rmse    standard   0.905    10 0.00839
## 5     1    30          4    0.00422 rmse    standard   1.11     10 0.00741
## # ℹ 1 more variable: .config <chr>
#update the model by selecting best hyperparameters.
xgboost_fw <- tune::finalize_workflow(xgboost_workflow,
                        tune::select_best(xgboost_tune, metric = "rmse"))

#fit the model on the entire training data and test it on the test data.
data_fit <- tune::last_fit(xgboost_fw, data_split)
tune::collect_metrics(data_fit)
## # A tibble: 2 × 4
##   .metric .estimator .estimate .config        
##   <chr>   <chr>          <dbl> <chr>          
## 1 rmse    standard       0.817 pre0_mod0_post0
## 2 rsq     standard       0.451 pre0_mod0_post0
tune::collect_predictions(data_fit) %>%
  ggplot(aes(total_weeks, .pred)) +
  geom_point(alpha = 0.3, fill = "midnightblue") +
  geom_abline(lty = 2, color = "gray50") +
  coord_fixed()