Extra Practice: Prediction Case Studies
Each case study below walks through one prediction problem from start to finish. Click a case study to open it. Write your code in the editors and click Run Code. The editors in a case study share their work, so run them in order. If something goes wrong, reload the page to start over.
All libraries and datasets have already been loaded for you.
An ice cream truck owner recorded the number of cones sold and the day’s high temperature (in °F) on 60 summer days. They want to predict how many cones they’ll sell, so they know how much to stock.
Dataframe name: icecream
Target variable: sales, the number of cones sold that day.
The first few rows of icecream:
| temperature | sales |
|---|---|
| 90 | 385 |
| 86 | 406 |
| 66 | 285 |
| 76 | 334 |
| 94 | 387 |
| 94 | 367 |
A model has been fit to predict sales from temperature. Here is its output:
# A tibble: 2 × 2
term estimate
<chr> <dbl>
1 (Intercept) -121.
2 temperature 5.65
Reminder: all datasets and libraries are already loaded for you.
Q1 Is this a regression or classification problem?
Show answer
Regression. sales is a continuous value; there are more than 2 possible outcomes.
Q2 Write the mathematical form of the model implied by this output.
Show answer
\[\widehat{\text{sales}} = -121 + 5.65 \cdot \text{temperature}\]
Q3 What would this model predict for a day with a high of 80°F? Round to 2 decimal places, if needed.
Show answer
Using the coefficients in the output:
-121 + 5.65 * 80which is about 331.
Q4 If classification, convert the target variable column to 1 or 0 (or equivalently, TRUE and FALSE). If regression, leave this blank.
Hint: use mutate(), and possibly ifelse() (there’s more than one way to do this).
Show answer
Leave blank: this is a regression problem.
Q5 Write the code to fit this model to icecream. Store it as model.
Show answer
model <- lm(sales ~ temperature, data = icecream)
model # Just to confirm we got the right results
Call:
lm(formula = sales ~ temperature, data = icecream)
Coefficients:
(Intercept) temperature
-121.330 5.646
Q6 Write the code to use this model to predict sales for each row in icecream. Store the predictions in a new column, sales_hat. (If this is a classification problem, these are predicted probabilities: don’t apply a decision threshold.)
Show answer
icecream <- mutate(icecream, sales_hat = predict(model))
head(icecream)# A tibble: 6 × 3
temperature sales sales_hat
<dbl> <dbl> <dbl>
1 90 385 387.
2 86 406 364.
3 66 285 251.
4 76 334 308.
5 94 387 409.
6 94 367 409.
Q7 Evaluate the model on icecream:
- If this is a regression problem, compute the MSE.
- If this is a classification problem, add a new column
decisionthat is 1 if the prediction is above 0.5 and 0 otherwise. Then calculate MCR, Precision, and Recall.
Show answer
summarize(icecream, MSE = mean((sales - sales_hat)^2))# A tibble: 1 × 1
MSE
<dbl>
1 506.
Q8 The owner looks up the forecast highs for the next three days, stored in new_icecream:
| temperature |
|---|
| 62 |
| 78 |
| 91 |
Write the code to have this model make predictions for new_icecream.
Show answer
predict(model, newdata = new_icecream) 1 2 3
228.7283 319.0659 392.4651
A driving school recorded how many hours 60 of its learners practiced driving before their road test, and whether they passed. The school wants to predict which learners will pass, so it can suggest more practice to the others.
Dataframe name: driving
Target variable: result, whether the learner passed ("pass") or failed ("fail") the road test.
The first few rows of driving:
| practice_hours | result |
|---|---|
| 32 | pass |
| 12 | fail |
| 29 | pass |
| 9 | fail |
| 38 | pass |
| 37 | pass |
A model has been fit to predict result from practice_hours. Here is its output:
# A tibble: 2 × 2
term estimate
<chr> <dbl>
1 (Intercept) -3.17
2 practice_hours 0.146
Reminder: all datasets and libraries are already loaded for you.
Q1 Is this a regression or classification problem?
Show answer
Classification. result has two categories, "pass" and "fail".
Q2 Write the mathematical form of the model implied by this output.
Show answer
\[\hat{P}(\text{result} = 1) = \frac{1}{1 + e^{-(-3.17 + 0.146 \cdot \text{practice\_hours})}}\]
Q3 What would this model predict for a learner who practiced for 20 hours? Round to 2 decimal places, if needed.
Show answer
Using the coefficients in the output:
1 / (1 + exp(-(-3.17 + 0.146 * 20)))which is about 0.44. So the model thinks there is about a 44% chance that this learner will pass.
Q4 If classification, convert the target variable column to 1 or 0 (or equivalently, TRUE and FALSE). If regression, leave this blank.
Hint: use mutate(), and possibly ifelse() (there’s more than one way to do this).
Show answer
driving <- mutate(driving, result = result == "pass")Or, with ifelse():
driving <- mutate(driving, result = ifelse(result == "pass", 1, 0))Q5 Write the code to fit this model to driving. Store it as model.
Show answer
model <- glm(result ~ practice_hours, data = driving, family = "binomial")
model # Just to confirm we got the right results
Call: glm(formula = result ~ practice_hours, family = "binomial", data = driving)
Coefficients:
(Intercept) practice_hours
-3.1743 0.1456
Degrees of Freedom: 59 Total (i.e. Null); 58 Residual
Null Deviance: 82.91
Residual Deviance: 56.91 AIC: 60.91
Q6 Write the code to use this model to predict result for each row in driving. Store the predictions in a new column, result_hat. (If this is a classification problem, these are predicted probabilities: don’t apply a decision threshold.)
Show answer
driving <- mutate(driving, result_hat = predict(model, type = "response"))
head(driving)# A tibble: 6 × 3
practice_hours result result_hat
<dbl> <lgl> <dbl>
1 32 TRUE 0.815
2 12 FALSE 0.194
3 29 TRUE 0.741
4 9 FALSE 0.134
5 38 TRUE 0.914
6 37 TRUE 0.902
Q7 Evaluate the model on driving:
- If this is a regression problem, compute the MSE.
- If this is a classification problem, add a new column
decisionthat is 1 if the prediction is above 0.5 and 0 otherwise. Then calculate MCR, Precision, and Recall.
Show answer
driving <- mutate(driving, decision = ifelse(result_hat > 0.5, 1, 0))
# MCR: the fraction of rows where the decision is wrong
summarize(driving, MCR = mean(result != decision))# A tibble: 1 × 1
MCR
<dbl>
1 0.233
# Precision: of the rows where the decision is 1, the fraction that really are 1
driving |>
filter(decision == 1) |>
summarize(Precision = mean(result == 1))# A tibble: 1 × 1
Precision
<dbl>
1 0.75
# Recall: of the rows that really are 1, the fraction where the decision is 1
driving |>
filter(result == 1) |>
summarize(Recall = mean(decision == 1))# A tibble: 1 × 1
Recall
<dbl>
1 0.844
Or all at once, by counting the four cells of the confusion matrix:
driving |>
summarize(
TP = sum(result == 1 & decision == 1),
FP = sum(result == 0 & decision == 1),
TN = sum(result == 0 & decision == 0),
FN = sum(result == 1 & decision == 0)
) |>
mutate(
MCR = (FP + FN) / (TP + FP + TN + FN),
Precision = TP / (TP + FP),
Recall = TP / (TP + FN)
)# A tibble: 1 × 7
TP FP TN FN MCR Precision Recall
<int> <int> <int> <int> <dbl> <dbl> <dbl>
1 27 9 19 5 0.233 0.75 0.844
Q8 Three learners have their road tests next week. Their practice hours are stored in new_driving:
| practice_hours |
|---|
| 5 |
| 18 |
| 33 |
Write the code to have this model make predictions for new_driving.
Show answer
predict(model, newdata = new_driving, type = "response") 1 2 3
0.07972195 0.36518348 0.83637767
These are the predicted probabilities that result is "pass".
A used car website collected the asking price, age (in years) and mileage (in thousands of miles) of 60 listings for the same model of car. Prices are in thousands of dollars, so a price of 23 means $23,000. The website wants to suggest a fair price to new sellers.
Dataframe name: cars
Target variable: price, the asking price in thousands of dollars.
The first few rows of cars:
| age | mileage | price |
|---|---|---|
| 5 | 71 | 23.0 |
| 8 | 71 | 19.3 |
| 15 | 193 | 7.3 |
| 8 | 85 | 18.1 |
| 13 | 136 | 12.3 |
| 11 | 116 | 14.1 |
A model has been fit to predict price from age and mileage. Here is its output:
# A tibble: 3 × 2
term estimate
<chr> <dbl>
1 (Intercept) 29.4
2 age -0.968
3 mileage -0.0459
Reminder: all datasets and libraries are already loaded for you.
Q1 Is this a regression or classification problem?
Show answer
Regression. price is a continuous value; there are more than 2 possible outcomes.
Q2 Write the mathematical form of the model implied by this output.
Show answer
\[\widehat{\text{price}} = 29.4 - 0.968 \cdot \text{age} - 0.0459 \cdot \text{mileage}\]
Q3 What would this model predict for a car that is 4 years old with 50 (thousand) miles? Round to 2 decimal places, if needed.
Show answer
Using the coefficients in the output:
29.4 - 0.968 * 4 - 0.0459 * 50which is about 23.23.
Q4 If classification, convert the target variable column to 1 or 0 (or equivalently, TRUE and FALSE). If regression, leave this blank.
Hint: use mutate(), and possibly ifelse() (there’s more than one way to do this).
Show answer
Leave blank: this is a regression problem.
Q5 Write the code to fit this model to cars. Store it as model.
Show answer
model <- lm(price ~ age + mileage, data = cars)
model # Just to confirm we got the right results
Call:
lm(formula = price ~ age + mileage, data = cars)
Coefficients:
(Intercept) age mileage
29.39027 -0.96763 -0.04592
Q6 Write the code to use this model to predict price for each row in cars. Store the predictions in a new column, price_hat. (If this is a classification problem, these are predicted probabilities: don’t apply a decision threshold.)
Show answer
cars <- mutate(cars, price_hat = predict(model))
head(cars)# A tibble: 6 × 4
age mileage price price_hat
<dbl> <dbl> <dbl> <dbl>
1 5 71 23 21.3
2 8 71 19.3 18.4
3 15 193 7.3 6.01
4 8 85 18.1 17.7
5 13 136 12.3 10.6
6 11 116 14.1 13.4
Q7 Evaluate the model on cars:
- If this is a regression problem, compute the MSE.
- If this is a classification problem, add a new column
decisionthat is 1 if the prediction is above 0.5 and 0 otherwise. Then calculate MCR, Precision, and Recall.
Show answer
summarize(cars, MSE = mean((price - price_hat)^2))# A tibble: 1 × 1
MSE
<dbl>
1 3.66
Q8 Three new sellers have just listed their cars, without a price. Their cars are stored in new_cars:
| age | mileage |
|---|---|
| 2 | 25 |
| 7 | 90 |
| 12 | 160 |
Write the code to have this model make predictions for new_cars.
Show answer
predict(model, newdata = new_cars) 1 2 3
26.30691 18.48371 10.43088
A gym recorded, for 60 of its members, how many times a month they visit on average, how many months they’ve been a member, and whether they canceled their membership the following month. The gym wants to predict who is about to cancel, so it can offer them a discount.
Dataframe name: gym
Target variable: canceled, whether the member canceled the following month ("yes" or "no").
The first few rows of gym:
| visits_per_month | months_member | canceled |
|---|---|---|
| 9 | 11 | no |
| 4 | 15 | no |
| 5 | 20 | yes |
| 11 | 14 | no |
| 13 | 5 | no |
| 7 | 15 | no |
A model has been fit to predict canceled from visits_per_month and months_member. Here is its output:
# A tibble: 3 × 2
term estimate
<chr> <dbl>
1 (Intercept) 3.57
2 visits_per_month -0.504
3 months_member -0.0616
Reminder: all datasets and libraries are already loaded for you.
Q1 Is this a regression or classification problem?
Show answer
Classification. canceled has two categories, "yes" and "no".
Q2 Write the mathematical form of the model implied by this output.
Show answer
\[\hat{P}(\text{canceled} = 1) = \frac{1}{1 + e^{-(3.57 - 0.504 \cdot \text{visits\_per\_month} - 0.0616 \cdot \text{months\_member})}}\]
Q3 What would this model predict for a member who visits 3 times a month and has been a member for 10 months? Round to 2 decimal places, if needed.
Show answer
Using the coefficients in the output:
1 / (1 + exp(-(3.57 - 0.504 * 3 - 0.0616 * 10)))which is about 0.81. So the model thinks there is about an 81% chance that this member will cancel.
Q4 If classification, convert the target variable column to 1 or 0 (or equivalently, TRUE and FALSE). If regression, leave this blank.
Hint: use mutate(), and possibly ifelse() (there’s more than one way to do this).
Show answer
gym <- mutate(gym, canceled = canceled == "yes")Or, with ifelse():
gym <- mutate(gym, canceled = ifelse(canceled == "yes", 1, 0))Q5 Write the code to fit this model to gym. Store it as model.
Show answer
model <- glm(canceled ~ visits_per_month + months_member, data = gym, family = "binomial")
model # Just to confirm we got the right results
Call: glm(formula = canceled ~ visits_per_month + months_member, family = "binomial",
data = gym)
Coefficients:
(Intercept) visits_per_month months_member
3.57249 -0.50440 -0.06157
Degrees of Freedom: 59 Total (i.e. Null); 57 Residual
Null Deviance: 67.48
Residual Deviance: 38.67 AIC: 44.67
Q6 Write the code to use this model to predict canceled for each row in gym. Store the predictions in a new column, canceled_hat. (If this is a classification problem, these are predicted probabilities: don’t apply a decision threshold.)
Show answer
gym <- mutate(gym, canceled_hat = predict(model, type = "response"))
head(gym)# A tibble: 6 × 4
visits_per_month months_member canceled canceled_hat
<dbl> <dbl> <lgl> <dbl>
1 9 11 FALSE 0.162
2 4 15 FALSE 0.653
3 5 20 TRUE 0.455
4 11 14 FALSE 0.0553
5 13 5 FALSE 0.0358
6 7 15 FALSE 0.293
Q7 Evaluate the model on gym:
- If this is a regression problem, compute the MSE.
- If this is a classification problem, add a new column
decisionthat is 1 if the prediction is above 0.5 and 0 otherwise. Then calculate MCR, Precision, and Recall.
Show answer
gym <- mutate(gym, decision = ifelse(canceled_hat > 0.5, 1, 0))
# MCR: the fraction of rows where the decision is wrong
summarize(gym, MCR = mean(canceled != decision))# A tibble: 1 × 1
MCR
<dbl>
1 0.133
# Precision: of the rows where the decision is 1, the fraction that really are 1
gym |>
filter(decision == 1) |>
summarize(Precision = mean(canceled == 1))# A tibble: 1 × 1
Precision
<dbl>
1 0.769
# Recall: of the rows that really are 1, the fraction where the decision is 1
gym |>
filter(canceled == 1) |>
summarize(Recall = mean(decision == 1))# A tibble: 1 × 1
Recall
<dbl>
1 0.667
Or all at once, by counting the four cells of the confusion matrix:
gym |>
summarize(
TP = sum(canceled == 1 & decision == 1),
FP = sum(canceled == 0 & decision == 1),
TN = sum(canceled == 0 & decision == 0),
FN = sum(canceled == 1 & decision == 0)
) |>
mutate(
MCR = (FP + FN) / (TP + FP + TN + FN),
Precision = TP / (TP + FP),
Recall = TP / (TP + FN)
)# A tibble: 1 × 7
TP FP TN FN MCR Precision Recall
<int> <int> <int> <int> <dbl> <dbl> <dbl>
1 10 3 42 5 0.133 0.769 0.667
Q8 Here are three current members, stored in new_gym:
| visits_per_month | months_member |
|---|---|
| 1 | 3 |
| 6 | 20 |
| 12 | 8 |
Write the code to have this model make predictions for new_gym.
Show answer
predict(model, newdata = new_gym, type = "response") 1 2 3
0.94701888 0.33508335 0.04866587
These are the predicted probabilities that canceled is "yes".
A student housing office collected the monthly rent (in dollars), size (in square feet), number of bedrooms, and distance to campus (in miles) of 60 apartments. It wants to help students tell whether a listing is priced fairly.
Dataframe name: apartments
Target variable: rent, the monthly rent in dollars.
The first few rows of apartments:
| sqft | bedrooms | miles_to_campus | rent |
|---|---|---|---|
| 600 | 1 | 2.2 | 1810 |
| 540 | 1 | 2.0 | 1620 |
| 950 | 3 | 0.6 | 2640 |
| 790 | 3 | 5.0 | 1870 |
| 910 | 3 | 1.2 | 2600 |
| 1130 | 2 | 1.3 | 2810 |
A model has been fit to predict rent from sqft, bedrooms, and miles_to_campus. Here is its output:
# A tibble: 4 × 2
term estimate
<chr> <dbl>
1 (Intercept) 962.
2 sqft 1.37
3 bedrooms 162.
4 miles_to_campus -119.
Reminder: all datasets and libraries are already loaded for you.
Q1 Is this a regression or classification problem?
Show answer
Regression. rent is a continuous value; there are more than 2 possible outcomes.
Q2 Write the mathematical form of the model implied by this output.
Show answer
\[\widehat{\text{rent}} = 962 + 1.37 \cdot \text{sqft} + 162 \cdot \text{bedrooms} - 119 \cdot \text{miles\_to\_campus}\]
Q3 What would this model predict for a 700 square foot, 2-bedroom apartment 1.5 miles from campus? Round to 2 decimal places, if needed.
Show answer
Using the coefficients in the output:
962 + 1.37 * 700 + 162 * 2 - 119 * 1.5which is about 2066.5.
Q4 If classification, convert the target variable column to 1 or 0 (or equivalently, TRUE and FALSE). If regression, leave this blank.
Hint: use mutate(), and possibly ifelse() (there’s more than one way to do this).
Show answer
Leave blank: this is a regression problem.
Q5 Write the code to fit this model to apartments. Store it as model.
Show answer
model <- lm(rent ~ sqft + bedrooms + miles_to_campus, data = apartments)
model # Just to confirm we got the right results
Call:
lm(formula = rent ~ sqft + bedrooms + miles_to_campus, data = apartments)
Coefficients:
(Intercept) sqft bedrooms miles_to_campus
961.609 1.368 162.353 -118.592
Q6 Write the code to use this model to predict rent for each row in apartments. Store the predictions in a new column, rent_hat. (If this is a classification problem, these are predicted probabilities: don’t apply a decision threshold.)
Show answer
apartments <- mutate(apartments, rent_hat = predict(model))
head(apartments)# A tibble: 6 × 5
sqft bedrooms miles_to_campus rent rent_hat
<dbl> <dbl> <dbl> <dbl> <dbl>
1 600 1 2.2 1810 1684.
2 540 1 2 1620 1626.
3 950 3 0.6 2640 2677.
4 790 3 5 1870 1937.
5 910 3 1.2 2600 2551.
6 1130 2 1.3 2810 2678.
Q7 Evaluate the model on apartments:
- If this is a regression problem, compute the MSE.
- If this is a classification problem, add a new column
decisionthat is 1 if the prediction is above 0.5 and 0 otherwise. Then calculate MCR, Precision, and Recall.
Show answer
summarize(apartments, MSE = mean((rent - rent_hat)^2))# A tibble: 1 × 1
MSE
<dbl>
1 10931.
Q8 Three new listings just went up, without a rent posted. They are stored in new_apartments:
| sqft | bedrooms | miles_to_campus |
|---|---|---|
| 500 | 1 | 0.5 |
| 800 | 2 | 2.0 |
| 1100 | 3 | 4.0 |
Write the code to have this model make predictions for new_apartments.
Show answer
predict(model, newdata = new_apartments) 1 2 3
1748.795 2143.738 2479.385