Elements of Data Science
SDS 322E

H. Sherry Zhang
Department of Statistics and Data Sciences
University of Texas at Austin

Get the repository for today:
library(usethis)
create_from_github("SDS322E-26FALL/1101-ml1", fork = FALSE)

Today

So far, we’ve learned two types of machine learning tasks:

  • Regression: predict a numeric response
  • Classification: predict a categorical (e.g. binary) response

Today we start from real data and ask: what question could machine learning answer here?

  • Four datasets from TidyTuesday: your group picks one and investigates it
  • Turn it into a question that a regression or classification model can answer

No modeling today: you will use data wrangling and visualization to explore the data, shape the question, and check that it is doable.

In parts II and III, we will build models for one of these datasets.

TidyTuesday

A weekly community data project, started in 2018 and run by the Data Science Learning Community: every Tuesday, a new real-world dataset.
github.com/rfordatascience/tidytuesday

Load the dataset:

# or with the tidytuesdayR package, using the date of the week
tuesdata <- tidytuesdayR::tt_load("2020-02-11")
hotels <- tuesdata$hotels

From a question to an ML problem

For the dataset you pick, think about:

  1. The question: what is one row, and what would be useful to predict for it?
  2. The response variable: is it numeric (regression) or categorical (classification)?
    • What if it has more than two categories?
    • What does the response variable look like? Plot it!
  3. The model structure:
    • Which predictors would be useful to predict the response variable?
    • what preprocessing is needed: missing values, scaling, categorical variables?
    • which algorithms would we compare, and with which metric?
  4. Is it doable? Is there enough data? Are some classes rare? Do you think the models will be able to beat a simple baseline?

Dataset 1: Hotel bookings

TidyTuesday 2020-02-11: bookings at two hotels in Portugal, a city hotel and a resort hotel, with arrival dates from 2015 to 2017.

tuesdata <- tidytuesdayR::tt_load("2020-02-11")
hotels <- tuesdata$hotels
dim(hotels)
[1] 119390     32
hotels |>
  select(hotel, is_canceled, lead_time, deposit_type, market_segment,
         reservation_status)
# A tibble: 119,390 × 6
  hotel        is_canceled lead_time deposit_type market_segment reservation_status
  <chr>              <dbl>     <dbl> <chr>        <chr>          <chr>             
1 Resort Hotel           0       342 No Deposit   Direct         Check-Out         
2 Resort Hotel           0       737 No Deposit   Direct         Check-Out         
3 Resort Hotel           0         7 No Deposit   Direct         Check-Out         
4 Resort Hotel           0        13 No Deposit   Corporate      Check-Out         
5 Resort Hotel           0        14 No Deposit   Online TA      Check-Out         
# ℹ 119,385 more rows

Dataset 2: Coffee ratings

TidyTuesday 2020-07-07: coffee lots graded by trained tasters for the Coffee Quality Institute, with where and how each coffee was grown.

tuesdata <- tidytuesdayR::tt_load("2020-07-07")
coffee <- tuesdata$coffee_ratings
dim(coffee)
[1] 1339   43
coffee |>
  select(total_cup_points, species, country_of_origin, altitude_mean_meters,
         processing_method, aroma, flavor, acidity)
# A tibble: 1,339 × 8
  total_cup_points species country_of_origin altitude_mean_meters processing_method aroma flavor
             <dbl> <chr>   <chr>                            <dbl> <chr>             <dbl>  <dbl>
1             90.6 Arabica Ethiopia                          2075 Washed / Wet       8.67   8.83
2             89.9 Arabica Ethiopia                          2075 Washed / Wet       8.75   8.67
3             89.8 Arabica Guatemala                         1700 <NA>               8.42   8.5 
4             89   Arabica Ethiopia                          2000 Natural / Dry      8.17   8.58
5             88.8 Arabica Ethiopia                          2075 Washed / Wet       8.25   8.5 
# ℹ 1,334 more rows
# ℹ 1 more variable: acidity <dbl>

Dataset 3: Spotify songs

TidyTuesday 2020-01-21: songs from Spotify playlists in six genres, with Spotify’s audio features for each song.

tuesdata <- tidytuesdayR::tt_load("2020-01-21")
spotify <- tuesdata$spotify_songs
dim(spotify)
[1] 32833    23
spotify |>
  select(track_name, playlist_genre, track_popularity, danceability, energy,
         tempo, duration_ms)
# A tibble: 32,833 × 7
  track_name                   playlist_genre track_popularity danceability energy tempo duration_ms
  <chr>                        <chr>                     <dbl>        <dbl>  <dbl> <dbl>       <dbl>
1 I Don't Care (with Justin B… pop                          66        0.748  0.916 122.       194754
2 Memories - Dillon Francis R… pop                          67        0.726  0.815 100.0      162600
3 All the Time - Don Diablo R… pop                          70        0.675  0.931 124.       176616
4 Call You Mine - Keanu Silva… pop                          60        0.718  0.93  122.       169093
5 Someone You Loved - Future … pop                          69        0.65   0.833 124.       189052
# ℹ 32,828 more rows

Dataset 4: Himalayan expeditions

TidyTuesday 2025-01-21: expeditions to peaks in the Nepal Himalaya from 2020 to 2024, from the Himalayan Database. Information about each peak is in a second table.

tuesdata <- tidytuesdayR::tt_load("2025-01-21")
exped <- tuesdata$exped_tidy
peaks <- tuesdata$peaks_tidy
dim(exped)
[1] 882  69
dim(peaks)
[1] 480  29
exped |>
  left_join(peaks |> select(PEAKID, PKNAME, HEIGHTM), by = "PEAKID") |>
  select(PKNAME, HEIGHTM, YEAR, SEASON_FACTOR, TOTMEMBERS, TOTHIRED, O2USED,
         SUCCESS1)
# A tibble: 882 × 8
  PKNAME     HEIGHTM  YEAR SEASON_FACTOR TOTMEMBERS TOTHIRED O2USED SUCCESS1
  <chr>        <dbl> <dbl> <chr>              <dbl>    <dbl> <lgl>  <lgl>   
1 Everest       8849  2020 Spring                 0        6 TRUE   TRUE    
2 Everest       8849  2020 Spring                12        0 TRUE   TRUE    
3 Everest       8849  2020 Spring                20       22 TRUE   TRUE    
4 Ama Dablam    6814  2020 Autumn                14       19 FALSE  TRUE    
5 Ama Dablam    6814  2020 Autumn                 6        8 FALSE  TRUE    
# ℹ 877 more rows

Your turn: pick one and investigate

In your group, pick one of the four datasets. Load it, read its data dictionary, and work through the checklist:

  1. The question: what is one row? Write the question you want to answer in one sentence
  2. The response variable: which variable is it? Is it numeric or binary? Plot it: a histogram if numeric, a bar plot if binary
  3. The model structure:
    • pick three predictors you would use, and plot how each relates to the response
    • name one variable you would leave out because it is only known after the outcome
    • check for missing values and messy variables: what would you need to clean?
  4. Is it doable? How many rows are left after cleaning? Is one class rare?