library(tidyverse)
library(tidymodels)
library(DAAG)
Weights of books
Today we’ll explore the question “How do volume and weights books relate?” and “How, if at all, does that change when we take whether the book is hardback or paperback into consideration?”
Goals
Build, fit, and interpret linear models with more than one predictor
Use categorical variables as a predictor in a model
Compute \(R^2\) and use it to select between models
Packages
Data
The data for this application exercise comes from the allbacks
dataset in the DAAG package. The dataset has 15 observations and 4 columns. Each observation represents a book. Let’s take a peek at the data:
allbacks
volume area weight cover
1 885 382 800 hb
2 1016 468 950 hb
3 1125 387 1050 hb
4 239 371 350 hb
5 701 371 750 hb
6 641 367 600 hb
7 1228 396 1075 hb
8 412 0 250 pb
9 953 0 700 pb
10 929 0 650 pb
11 1492 0 975 pb
12 419 0 350 pb
13 1010 0 950 pb
14 595 0 425 pb
15 1034 0 725 pb
Note that volume
is measured in cubic centimeters and weight
is measured in grams. More information on the dataset can be found in the documentation for allbacks
, with ?allbacks
.
Single predictor
Exercise 1
Visualize the relationship between volume (on the x-axis) and weight (on the y-axis). Overlay the line of best fit. Describe the relationship between these variables.
# add code here
Add response here.
Exercise 2
Fit a model predicting weight from volume for these books and save it as weight_fit
. Display a tidy output of the model.
# add code here
Exercise 3
Interpret the slope and the intercept in context of the data.
Add response here.
Exercise 4
Calculate the \(R^2\) of this model and interpret it in context of the data.
# add code here
Add response here.
Multiple predictors
Exercise 5
Visualize the relationship between volume (on the x-axis) and weight (on the y-axis), taking into consideration the cover type of the book. Use different colors and shapes for hardback and paperback books. Also use different colors for lines of best fit for the two types of books. In addition, add the overall line of best fit (from Exercise 1) as a gray dashed line so that you can see the difference between the lines when considering and not considering cover type.
# add code here
Exercise 6
Fit a model predicting weight from volume for these books and save it as weight_cover_fit
. Display a tidy output of the model.
# add code here
Exercise 7
In the model output we have a variable coverpb
. Why is only the pb
(paperback) level of the cover
variable shown? What happened to the hb
(hardback) level?
Add response here.
Exercise 8
Interpret the slopes and the intercept in context of the data.
Add response here.
Exercise 9
First, guess whether the \(R^2\) of this model will be greater than, less than, or the same as the previous model, or whether we can’t tell. Then, calculate the \(R^2\) of this model to confirm your guess, and then interpret it in context of the data.
# add code here
Add response here.
Exercise 10
Which model is preferred for predicting book weights and why?
Add response here.
Exercise 11
Using the model you chose, predict the weight of a hardcover book that is 1000 cubic centimeters (that is, roughly 25 centimeters in length, 20 centimeters in width, and 2 centimeters in height/thickness).
# add code here