Goal: Predict the average movie rating

Import data

horror_movies <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-11-01/horror_movies.csv')
## Rows: 32540 Columns: 20
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (10): original_title, title, original_language, overview, tagline, post...
## dbl   (8): id, popularity, vote_count, vote_average, budget, revenue, runtim...
## lgl   (1): adult
## date  (1): release_date
## 
## ℹ 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(horror_movies)
Data summary
Name horror_movies
Number of rows 32540
Number of columns 20
_______________________
Column type frequency:
character 10
Date 1
logical 1
numeric 8
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
original_title 0 1.00 1 191 0 30296 0
title 0 1.00 1 191 0 29563 0
original_language 0 1.00 2 2 0 97 0
overview 1286 0.96 1 1000 0 31020 0
tagline 19835 0.39 1 237 0 12513 0
poster_path 4474 0.86 30 32 0 28048 0
status 0 1.00 7 15 0 4 0
backdrop_path 18995 0.42 29 32 0 13536 0
genre_names 0 1.00 6 144 0 772 0
collection_name 30234 0.07 4 56 0 815 0

Variable type: Date

skim_variable n_missing complete_rate min max median n_unique
release_date 0 1 1950-01-01 2022-12-31 2012-12-09 10999

Variable type: logical

skim_variable n_missing complete_rate mean count
adult 0 1 0 FAL: 32540

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
id 0 1.00 445910.83 305744.67 17 146494.8 426521.00 707534.00 1033095.00 ▇▆▆▅▅
popularity 0 1.00 4.01 37.51 0 0.6 0.84 2.24 5088.58 ▇▁▁▁▁
vote_count 0 1.00 62.69 420.89 0 0.0 2.00 11.00 16900.00 ▇▁▁▁▁
vote_average 0 1.00 3.34 2.88 0 0.0 4.00 5.70 10.00 ▇▂▆▃▁
budget 0 1.00 543126.59 4542667.81 0 0.0 0.00 0.00 200000000.00 ▇▁▁▁▁
revenue 0 1.00 1349746.73 14430479.15 0 0.0 0.00 0.00 701842551.00 ▇▁▁▁▁
runtime 0 1.00 62.14 41.00 0 14.0 80.00 91.00 683.00 ▇▁▁▁▁
collection 30234 0.07 481534.88 324498.16 656 155421.0 471259.00 759067.25 1033032.00 ▇▅▅▅▅
data <- horror_movies %>%
    
    # Treat missing values
    select(-tagline, -backdrop_path, -collection_name, -collection, -release_date) %>% 
    na.omit() %>% 
    
    # Log transform variables with pos-skewed distribution
    mutate(popularity = log(popularity))

Explore data

Identify good predictors

Popularity

data %>% 
    ggplot(aes(vote_average, popularity)) +
    scale_x_log10() +
    geom_point()
## Warning in scale_x_log10(): log-10 transformation introduced infinite values.

Revenue

data %>% 
    ggplot(aes(vote_average, revenue)) +
    geom_point()

Runtime

data %>% 
    ggplot(aes(vote_average, runtime)) +
    geom_point()

Title

data %>%
    
    # Tokenize title
    unnest_tokens(output = word, input = title) %>%
    
    # Calculate avg rating per word
    group_by(word) %>%
    summarise(vote_average = mean(vote_average),
              n = n()) %>%
    ungroup() %>%
    
    filter(n > 5, !str_detect(word, "\\d")) %>%
    slice_max(order_by = vote_average, n = 25) %>%
    
    # Plot
    ggplot(aes(vote_average, fct_reorder(word, vote_average))) +
    geom_point() +
    
    labs(y = "Words in Title")

Build models

Split Data

data <- sample_n(data, 100)

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

# Further split training data-set for cross-validation
set.seed(4321)
data_cv <- rsample::vfold_cv(data_train)
data_cv
## #  10-fold cross-validation 
## # A tibble: 10 × 2
##    splits         id    
##    <list>         <chr> 
##  1 <split [67/8]> Fold01
##  2 <split [67/8]> Fold02
##  3 <split [67/8]> Fold03
##  4 <split [67/8]> Fold04
##  5 <split [67/8]> Fold05
##  6 <split [68/7]> Fold06
##  7 <split [68/7]> Fold07
##  8 <split [68/7]> Fold08
##  9 <split [68/7]> Fold09
## 10 <split [68/7]> Fold10
library(usemodels)
usemodels::use_xgboost(vote_average ~ ., data = data_train)
## xgboost_recipe <- 
##   recipe(formula = vote_average ~ ., 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(4796)
## xgboost_tune <-
##   tune_grid(xgboost_workflow, resamples = stop("add your rsample object"), grid = stop("add number of candidate points"))
xgboost_recipe <- 
  recipe(formula = vote_average ~ ., data = data_train) %>% 
  step_zv(all_predictors()) %>%
    update_role(id, new_role = "id variable") %>%
    step_tokenize(overview) %>% 
    step_tokenfilter(overview, max_tokens = 100) %>%
    step_tfidf(overview) %>% 
    step_other(original_title, title, poster_path, original_language, genre_names) %>%
    step_dummy(original_title, title, poster_path, original_language, genre_names, one_hot = TRUE) %>%
    step_YeoJohnson(popularity, vote_count, budget, revenue, runtime)

xgboost_recipe %>% prep() %>% juice() %>% glimpse()
## Rows: 75
## Columns: 120
## $ id                                            <dbl> 256781, 580627, 388705, …
## $ popularity                                    <dbl> -0.51076049, -0.67663608…
## $ vote_count                                    <dbl> 0.9210510, 0.0000000, 0.…
## $ budget                                        <dbl> 0.000000, 0.000000, 0.00…
## $ revenue                                       <dbl> 0.000000, 0.000000, 0.00…
## $ runtime                                       <dbl> 96.528151, 90.432636, 12…
## $ vote_average                                  <dbl> 3.5, 0.0, 5.5, 0.0, 2.8,…
## $ tfidf_overview_a                              <dbl> 0.07732266, 0.09389180, …
## $ tfidf_overview_about                          <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_accident                       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ tfidf_overview_after                          <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_all                            <dbl> 0.03440293, 0.00000000, …
## $ tfidf_overview_an                             <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_and                            <dbl> 0.05461537, 0.00000000, …
## $ tfidf_overview_are                            <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_as                             <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_at                             <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_be                             <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_been                           <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_being                          <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_boy                            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ tfidf_overview_but                            <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_by                             <dbl> 0.04582778, 0.00000000, …
## $ tfidf_overview_college                        <dbl> 0.04791318, 0.00000000, …
## $ tfidf_overview_crew                           <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_dark                           <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_day                            <dbl> 0.09582637, 0.00000000, …
## $ tfidf_overview_death                          <dbl> 0.0438699, 0.0000000, 0.…
## $ tfidf_overview_during                         <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_each                           <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_evil                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ tfidf_overview_family                         <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_film                           <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_finds                          <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_for                            <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_forced                         <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_found                          <dbl> 0.04077336, 0.00000000, …
## $ tfidf_overview_four                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ tfidf_overview_friends                        <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_from                           <dbl> 0.00000000, 0.13375414, …
## $ tfidf_overview_get                            <dbl> 0.0438699, 0.0000000, 0.…
## $ tfidf_overview_ghost                          <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_go                             <dbl> 0.04386990, 0.00000000, …
## $ tfidf_overview_goes                           <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_government                     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ tfidf_overview_grandfather                    <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ tfidf_overview_group                          <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_has                            <dbl> 0.02913237, 0.12381259, …
## $ tfidf_overview_he                             <dbl> 0.00000000, 0.10864192, …
## $ tfidf_overview_her                            <dbl> 0.12232009, 0.00000000, …
## $ tfidf_overview_him                            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ tfidf_overview_himself                        <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_his                            <dbl> 0.00000000, 0.09498911, …
## $ tfidf_overview_home                           <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_horror                         <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_house                          <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_however                        <dbl> 0.0000000, 0.1864471, 0.…
## $ tfidf_overview_in                             <dbl> 0.04389414, 0.00000000, …
## $ tfidf_overview_into                           <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_is                             <dbl> 0.03368036, 0.00000000, …
## $ tfidf_overview_island                         <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_it                             <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_john                           <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ tfidf_overview_known                          <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_life                           <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_looking                        <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_lost                           <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_love                           <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_making                         <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_man                            <dbl> 0.0000000, 0.1626681, 0.…
## $ tfidf_overview_may                            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ tfidf_overview_mother                         <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_new                            <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_night                          <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_not                            <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_now                            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ tfidf_overview_of                             <dbl> 0.02767598, 0.00000000, …
## $ tfidf_overview_off                            <dbl> 0.04077336, 0.00000000, …
## $ tfidf_overview_old                            <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_on                             <dbl> 0.02719969, 0.00000000, …
## $ tfidf_overview_one                            <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_out                            <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_over                           <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_own                            <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_school                         <dbl> 0.09582637, 0.00000000, …
## $ tfidf_overview_she                            <dbl> 0.05624669, 0.11952422, …
## $ tfidf_overview_story                          <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_strange                        <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_that                           <dbl> 0.04363731, 0.18545857, …
## $ tfidf_overview_the                            <dbl> 0.09124598, 0.04847443, …
## $ tfidf_overview_their                          <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_them                           <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_they                           <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_this                           <dbl> 0.03440293, 0.00000000, …
## $ tfidf_overview_those                          <dbl> 0.04077336, 0.00000000, …
## $ tfidf_overview_to                             <dbl> 0.06650414, 0.05652852, …
## $ tfidf_overview_two                            <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_was                            <dbl> 0.03827485, 0.00000000, …
## $ tfidf_overview_when                           <dbl> 0.03284694, 0.00000000, …
## $ tfidf_overview_which                          <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_while                          <dbl> 0.00000000, 0.00000000, …
## $ tfidf_overview_who                            <dbl> 0.02415041, 0.00000000, …
## $ tfidf_overview_will                           <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_with                           <dbl> 0.04702517, 0.00000000, …
## $ tfidf_overview_woman                          <dbl> 0.16423472, 0.27919903, …
## $ tfidf_overview_years                          <dbl> 0.0000000, 0.0000000, 0.…
## $ tfidf_overview_young                          <dbl> 0.0000000, 0.1538006, 0.…
## $ original_title_Alien.Space.Avenger            <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ original_title_other                          <dbl> 1, 1, 1, 1, 1, 1, 1, 1, …
## $ title_Alien.Space.Avenger                     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ title_other                                   <dbl> 1, 1, 1, 1, 1, 1, 1, 1, …
## $ poster_path_X.1chy8R4wkggtW6NTMDY3lu7aibf.jpg <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ poster_path_other                             <dbl> 1, 1, 1, 1, 1, 1, 1, 1, …
## $ original_language_en                          <dbl> 0, 0, 1, 1, 1, 1, 0, 1, …
## $ original_language_es                          <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ original_language_other                       <dbl> 1, 1, 0, 0, 0, 0, 1, 0, …
## $ genre_names_Comedy..Horror                    <dbl> 0, 0, 0, 0, 0, 0, 0, 0, …
## $ genre_names_Horror                            <dbl> 1, 1, 0, 0, 0, 0, 1, 1, …
## $ genre_names_Horror..Thriller                  <dbl> 0, 0, 1, 0, 0, 0, 0, 0, …
## $ genre_names_other                             <dbl> 0, 0, 0, 1, 1, 1, 0, 0, …
xgboost_spec <- 
  boost_tree(trees = tune(), min_n = tune(), mtry = tune(), learn_rate = tune()) %>% 
  set_mode("regression") %>% 
  set_engine("xgboost") 

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

set.seed(4796)
xgboost_tune <-
  tune_grid(xgboost_workflow,
            resamples = data_cv,
            grid = 5)
## i Creating pre-processing data to finalize unknown parameter: mtry
## Warning: package 'xgboost' was built under R version 4.3.3
## → A | warning: A correlation computation is required, but `estimate` is constant and has 0
##                standard deviation, resulting in a divide by 0 error. `NA` will be returned.
## 
There were issues with some computations   A: x1

There were issues with some computations   A: x2

There were issues with some computations   A: x3

There were issues with some computations   A: x4

There were issues with some computations   A: x5

There were issues with some computations   A: x6

There were issues with some computations   A: x7

There were issues with some computations   A: x8

There were issues with some computations   A: x9

There were issues with some computations   A: x10

There were issues with some computations   A: x10