Tips

Application exercise

Today we’ll explore the question “What best predicts what percent of the bill amount people tip at restaurants?”

Goals

  • Build, fit, and interpret linear models with more than one predictor

  • Compute \(R^2\) and adjusted \(R^2\)

  • Use adjusted \(R^2\) for stepwise model selection

  • Evaluate predictive performance of models

Packages

library(tidyverse)
library(tidymodels)
library(scales)

Data

The data for this application exercise was collected in 2011 by a student at St. Olaf who worked at a local restaurant.1

tips <- read_csv("data/tips.csv")
Rows: 129 Columns: 13
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (8): day, meal, payment, age, gift_card, comps, alcohol, bday
dbl (5): party, bill, w_tip, tip, tip_percentage

ℹ 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.

The dataset has 129 observations and 13 columns. Each observation represents a meal by a party at the restaurant, and for each meal we know the bill amount, the tip amount, and a variety of other features like whether the payment was made on credit card or by cash, whether any alcohol was ordered, etc.

To keep the scope manageable for this application exercise, we’ll only consider the following predictors:

  • meal: Meal of the day (Lunch, Dinner, or Late Night)

  • party: Number of people in the party

  • alcohol: Whether the party ordered any alcohol (No or Yes)

  • bill: Bill amount, in USD

We will aim to predict tip_percentage from these variables.

Exploratory data analysis

Exercise 1

Visualize the relationship between these variables.

# add code here

Exercise 2

In a couple of sentences, describe any apparent patterns.

Add response here.

Strength of fit

Exercise 3

Fit a model predicting tip percentage from bill amount. Display and interpret the model coefficients. Additionally, calculate and interpret the \(R^2\) of this model.

# add code here

Add response here.

# add code here

Add response here.

Exercise 4

Suppose we next add meal as a predictor and interpret the model coefficients again.

# add code here

Add response here.

Exercise 5

Would you expect the \(R^2\) of the second model (with bill and meal as predictors) to be higher, lower, or the same as the \(R^2\) for the first model (with only bill as the predictor)? Explain your reasoning.

Add response here.

Exercise 6

Fit a model predicting tip percentage from bill amount and meal, calculate its \(R^2\), and comment on your guess from the previous exercise.

# add code here

Add response here.

Adjusted \(R^2\)

Exercise 7

Calculate adjusted \(R^2\) for the two models. Is adding meal to a model predicting tip_percentage from bill useful?

# add code here

Add response here.

Stepwise model selection

Backward elimination

Backward elimination starts with the full model (the model that includes all potential predictor variables). Variables are eliminated one-at-a-time from the model until we cannot improve the model any further.

Procedure:

  1. Start with a model that has all predictors we consider and compute the adjusted \(R^2\).

  2. Next fit every possible model with 1 fewer predictor.

  3. Compare adjusted \(R^2\)s to select the best model (highest adjusted \(R^2\)) with 1 fewer predictor.

  4. Repeat steps 2 and 3 until adjusted \(R^2\) no longer increases.

Forward selection

Forward selection is the reverse of the backward elimination technique. Instead, of eliminating variables one-at-a-time, we add variables one-at-a-time until we cannot find any variables that improve the model any further.

Procedure:

  1. Start with a model that has no predictors.

  2. Next fit every possible model with 1 additional predictor and calculate adjusted \(R^2\) of each model.

  3. Compare adjusted \(R^2\) values to select the best model (highest adjusted \(R^2\)) with 1 additional predictor.

  4. Repeat steps 2 and 3 until adjusted \(R^2\) no longer increases.

Exercise 8

Perform backward elimination to find the best model for predicting tip_percentage from meal, party, alcohol, bill.

# add code here

Add response here.

Exercise 9

Perform forward selection to find the best model for predicting tip_percentage from meal, party, alcohol, bill.

# add code here

Add response here.

Exercise 10

Fit the “best” model and interpret it.

# add code here

Add response here.

Predictive performance

A common way of evaluating the predictive performance of a model is to test it against new data that was not used to build the model in the first place. In machine learning, this new dataset is commonly referred to as testing data, and the dataset that was used to build and select the model is commonly referred to as training data.

Let’s first load the new data.

tips_test <- read_csv("data/tips-test.csv")
Rows: 40 Columns: 13
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (8): day, meal, payment, age, gift_card, comps, alcohol, bday
dbl (5): party, bill, w_tip, tip, tip_percentage

ℹ 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.

There are 40 observations in this new dataset, and it has all the same columns as our existing dataset.

glimpse(tips_test)
Rows: 40
Columns: 13
$ day            <chr> "Tuesday", "Tuesday", "Sunday", "Friday", "Friday", "Sa…
$ meal           <chr> "Late Night", "Dinner", "Lunch", "Late Night", "Late Ni…
$ payment        <chr> "Credit", "Cash", "Cash", "Cash", "Credit", "Credit", "…
$ party          <dbl> 1, 2, 2, 1, 3, 5, 4, 3, 3, 2, 1, 6, 4, 4, 6, 1, 2, 2, 3…
$ age            <chr> "Yadult", "Middle", "SenCit", "Yadult", "Middle", "Midd…
$ gift_card      <chr> "No", "No", "No", "No", "No", "No", "No", "No", "No", "…
$ comps          <chr> "No", "No", "No", "No", "No", "No", "No", "No", "No", "…
$ alcohol        <chr> "No", "No", "No", "No", "Yes", "No", "Yes", "No", "No",…
$ bday           <chr> "No", "No", "No", "No", "No", "No", "No", "No", "No", "…
$ bill           <dbl> 3.74, 26.54, 25.68, 4.98, 25.40, 64.28, 57.86, 30.38, 3…
$ w_tip          <dbl> 4.74, 31.00, 30.00, 5.00, 29.40, 74.28, 67.86, 35.38, 3…
$ tip            <dbl> 1.00, 4.46, 4.32, 0.02, 4.00, 10.00, 10.00, 5.00, 3.48,…
$ tip_percentage <dbl> 0.267379679, 0.168048229, 0.168224299, 0.004016064, 0.1…

Let’s use our model to make predictions for tip_percentage for these new data:

# add code here

We can plot the predicted values of tip_percentage against the observed values to see how well we’re doing.

# add code here

We can also quantify the average error in our predictions with a measure called root mean square error, RMSE.

# add code here

What this value tells us is that our predictions are off by, on average, approximately ___.

Let’s take a look back at the tip percentage variables in this dataset.

# add code here

The tips range anywhere from ___ to ___ and our predictions are off, on average, by ___. It’s not great, and it’s not terrible either. This, of course, is not a very technical assessment of the predictive performance of our model. There are much more formal ways of evaluating the predictive performance of a model using RMSE, but this is a good starting point for making sense of how well you’re doing with your predictions.