Chapter 10 Applications: Model
Section 10.1 Case study: Houses for sale
Take a walk around your neighborhood and youβll probably see a few houses for sale, and you might be able to look up its price online. Youβll note that house prices are somewhat arbitrary β the homeowners get to decide the listing price, and many criteria factor into this decision, e.g., what do comparable houses ("comps" in real estate speak) sell for, how quickly they need to sell the house, etc.
In this case study weβll formalize the process of determining the listing price of a house by using data on current home sales. In November of 2020, information on 98 houses in the Duke Forest neighborhood of Durham, NC were scraped from Zillow. The homes were all recently sold at the time of data collection, and the goal of the project was to build a model for predicting the sale price based on a particular homeβs characteristics. The first four homes are shown in TableΒ 10.2, and descriptions of each variable are shown in TableΒ 10.3.
Note 10.1. Data.
duke_forest.| price | bed | bath | area | year_built | cooling | lot |
|---|---|---|---|---|---|---|
| 1,520,000 | 3 | 4 | 6,040 | 1972 | central | 0.46 |
| 1,030,000 | 5 | 4.5 | 4,475 | 1969 | central | 1.14 |
| 420,000 | 2 | 2.5 | 1,745 | 1959 | central | 0.51 |
| 680,000 | 4 | 3 | 2,091 | 1961 | other | 0.84 |
duke_forest dataset.| Variable | Description |
|---|---|
price |
Sale price, in USD |
bed |
Number of bedrooms |
bath |
Number of bathrooms |
area |
Area of home, in square feet |
year_built |
Year the home was built |
cooling |
Cooling system: central or other (other is baseline) |
lot |
Area of the entire property, in acres |
Subsection 10.1.1 Correlating with price
As mentioned, the goal of the data collection was to build a model for the sale price of homes. While using multiple predictor variables is likely preferable to using only one variable, we start by learning about the variables themselves and their relationship to price. FigureΒ 10.5 shows scatterplots describing price as a function of each of the predictor variables. All of the variables seem to be positively associated with price (higher values of the variable are matched with higher price values).
pr_bed <- ggplot(duke_forest, aes(x = bed, y = price)) +
geom_point(alpha = 0.8) +
labs(
x = "Number of bedrooms",
y = "Sale price (USD)"
) +
stat_cor(aes(label = paste("r", ..r.., sep = "~`=`~"))) +
scale_y_continuous(labels = label_dollar(scale = 1/1000, suffix = "K"))
pr_bath <- ggplot(duke_forest, aes(x = bath, y = price)) +
geom_point(alpha = 0.8) +
labs(
x = "Number of bathrooms",
y = "Sale price (USD)"
) +
stat_cor(aes(label = paste("r", ..r.., sep = "~`=`~"))) +
scale_y_continuous(labels = label_dollar(scale = 1/1000, suffix = "K"))
pr_area <- ggplot(duke_forest, aes(x = area, y = price)) +
geom_point(alpha = 0.8) +
labs(
x = "Area of home (in square feet)",
y = "Sale price (USD)"
) +
stat_cor(aes(label = paste("r", ..r.., sep = "~`=`~"))) +
scale_y_continuous(labels = label_dollar(scale = 1/1000, suffix = "K"))
pr_year <- ggplot(duke_forest, aes(x = year_built, y = price)) +
geom_point(alpha = 0.8) +
labs(
x = "Year built",
y = "Sale price (USD)"
) +
stat_cor(aes(label = paste("r", ..r.., sep = "~`=`~"))) +
scale_y_continuous(labels = label_dollar(scale = 1/1000, suffix = "K"))
pr_cool <- ggplot(duke_forest, aes(x = cooling, y = price)) +
geom_point(alpha = 0.8) +
labs(
x = "Cooling type",
y = "Sale price (USD)"
) +
stat_cor(aes(label = paste("r", ..r.., sep = "~`=`~"))) +
scale_y_continuous(labels = label_dollar(scale = 1/1000, suffix = "K"))
pr_lot <- ggplot(duke_forest, aes(x = lot, y = price)) +
geom_point(alpha = 0.8) +
labs(
x = "Area of property (in acres)",
y = "Sale price (USD)"
) +
stat_cor(aes(label = paste("r", ..r.., sep = "~`=`~"))) +
scale_y_continuous(labels = label_dollar(scale = 1/1000, suffix = "K"))
pr_bed + pr_bath + pr_area + pr_year + pr_cool + pr_lot +
plot_layout(ncol = 2)

Checkpoint 10.6.
In FigureΒ 10.5 there does not appear to be a correlation value calculated for the predictor variable,
cooling. Why not? Can the variable still be used in the linear model?
Solution.
The correlation coefficient can only be calculated to describe the relationship between two numerical variables. The predictor variable
cooling is categorical, not numerical. It can, however, be used in the linear model as a binary indicator variable coded, for example, with a \(1\) for central and \(0\) for other.
Example 10.7.
In FigureΒ 10.5 which variable seems to be most informative for predicting house price? Provide two reasons for your answer.
Solution.
The
area of the home is the variable which is most highly correlated with price. Additionally, the scatterplot for price vs. area seems to show a strong linear relationship between the two variables. Note that the correlation coefficient and the scatterplot linearity will often give the same conclusion. However, recall that the correlation coefficient is very sensitive to outliers, so it is always wise to look at the scatterplot even when the variables are highly correlated.
Subsection 10.1.2 Modeling price with area
A linear model was fit to predict
price from area. The resulting model information is given in TableΒ 10.8.
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
(Intercept) |
116,652 | 53,302 | 2.19 | 0.0316 |
area |
159 | 18 | 8.78 | <0.0001 |
| Adjusted R-sq = 0.4399 | ||||
| df = 96 | ||||
Checkpoint 10.9.
Interpret the value of \(b_1 = 159\) in the context of the problem.
Checkpoint 10.10.
The residuals from the linear model can be used to assess whether a linear model is appropriate. FigureΒ 10.12 plots the residuals \(e_i = y_i - \hat{y}_i\) on the \(y\)-axis and the fitted (or predicted) values \(\hat{y}_i\) on the \(x\)-axis.
duke_forest |>
lm(price ~ area, data = _) |>
augment() |>
ggplot(aes(x = .fitted, y = .resid)) +
geom_point(size = 2, alpha = 0.8) +
labs(
x = "Predicted values of sale price (in USD)",
y = "Residuals"
) +
geom_hline(yintercept = 0, linetype = "dashed") +
scale_x_continuous(labels = label_dollar(scale = 1/1000, suffix = "K")) +
scale_y_continuous(labels = label_dollar(scale = 1/1000, suffix = "K"))

Checkpoint 10.13.
What aspect(s) of the residual plot indicate that a linear model is appropriate? What aspect(s) of the residual plot seem concerning when fitting a linear model?
Solution.
The residual plot shows that the relationship between
area and price of a home is indeed linear. However, the residuals are quite large for expensive homes. The large residuals indicate potential outliers or increasing variability, either of which could warrant more involved modeling techniques than are presented in this chapter.
Subsection 10.1.3 Modeling price with multiple variables
It seems as though the predictions of home price might be more accurate if more than one predictor variable was used in the linear model. TableΒ 10.14 displays the output from a linear model of
price regressed on area, bed, bath, year_built, cooling, and lot.
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
(Intercept) |
-2,910,715 | 1,787,934 | -1.63 | 0.107 |
area |
102 | 23 | 4.42 | <0.0001 |
bed |
-13,692 | 25,928 | -0.53 | 0.5987 |
bath |
41,076 | 24,662 | 1.67 | 0.0993 |
year_built |
1,459 | 914 | 1.60 | 0.1139 |
coolingcentral |
84,065 | 30,338 | 2.77 | 0.0068 |
lot |
356,141 | 75,940 | 4.69 | <0.0001 |
| Adjusted R-sq = 0.5896 | ||||
| df = 90 | ||||
Example 10.15.
Using TableΒ 10.14, write out the linear model of price on the six predictor variables.
Solution.
\begin{align*}
\widehat{\text{price}} = -2,910,715 \amp+ 102 \times \text{area}\\
\amp- 13,692 \times \text{bed}\\
\amp+ 41,076 \times \text{bath}\\
\amp+ 1,459 \times \text{year_built}\\
\amp+ 84,065 \times \text{cooling}_{\text{central}}\\
\amp+ 356,141 \times \text{lot}
\end{align*}
Checkpoint 10.16.
The value of the estimated coefficient on \(\text{cooling}_{\text{central}}\) is \(b_5 = 84,065\text{.}\) Interpret the value of \(b_5\) in the context of the problem.
A friend suggests that maybe you do not need all six variables to have a good model for
price. You consider taking a variable out, but you arenβt sure which one to remove.
Example 10.17.
Results corresponding to the full model for the housing data are shown in TableΒ 10.14. How should we proceed under the backward elimination strategy?
Solution.
Our baseline adjusted \(R^2\) from the full model is 0.5584, and we need to determine whether dropping a predictor will improve the adjusted \(R^2\text{.}\) To check, we fit models that each drop a different predictor, and we record the adjusted \(R^2\text{:}\)
-
Excluding
area: 0.4846 -
Excluding
bed: 0.5609 -
Excluding
bath: 0.5488 -
Excluding
year_built: 0.4951 -
Excluding
cooling: 0.5423 -
Excluding
lot: 0.5051
The model without
bed has the highest adjusted \(R^2\) of 0.5609, higher than the adjusted \(R^2\) for the full model. Because eliminating bed leads to a model with a higher adjusted \(R^2\) than the full model, we drop bed from the model. It might seem counter-intuitive to exclude number of bedrooms from the model. After all, we would expect homes with more bedrooms to cost more, and we can see a clear relationship between number of bedrooms and sale price in FigureΒ 10.5. However, note that area is still in the model, and itβs quite likely that the area of the home and the number of bedrooms are highly associated. Therefore, the model already has information on "how much space is available in the house" with the inclusion of area.
Since we eliminated a predictor from the model in the first step, we see whether we should eliminate any additional predictors. Our baseline adjusted \(R^2\) is now 0.5609. We fit another set of new models, which consider eliminating each of the remaining predictors in addition to
bed:
-
Excluding
bedandarea: 0.4888 -
Excluding
bedandbath: 0.5526 -
Excluding
bedandyear_built: 0.4972 -
Excluding
bedandcooling: 0.5440 -
Excluding
bedandlot: 0.5073
None of these models lead to an improvement in adjusted \(R^2\text{,}\) so we do not eliminate any of the remaining predictors.
That is, after backward elimination, we are left with the model that keeps all predictors except
bed, which we can summarize using the coefficients from TableΒ 10.18.
| term | estimate | std.error | statistic | p.value |
|---|---|---|---|---|
(Intercept) |
-2,952,641 | 1,779,079 | -1.66 | 0.1004 |
area |
99 | 22 | 4.44 | <0.0001 |
bath |
36,228 | 22,799 | 1.59 | 0.1155 |
year_built |
1,466 | 910 | 1.61 | 0.1107 |
coolingcentral |
83,856 | 30,215 | 2.78 | 0.0067 |
lot |
357,119 | 75,617 | 4.72 | <0.0001 |
| Adjusted R-sq = 0.5929 | ||||
| df = 91 | ||||
Then, the linear model for predicting sale price based on this model is as follows:
\begin{align*}
\widehat{\text{price}} = \amp-2,952,641 + 99 \times \text{area} + 36,228 \times \text{bath} + 1,466 \times \text{year_built}\\
\amp+ 83,856 \times \text{cooling}_{\text{central}} + 357,119 \times \text{lot}
\end{align*}
Example 10.19.
The residual plot for the model with all of the predictor variables except
bed is given in FigureΒ 10.21. How do the residuals in FigureΒ 10.21 compare to the residuals in FigureΒ 10.12?
Solution.
m_full_no_bed |>
augment() |>
ggplot(aes(x = .fitted, y = .resid)) +
geom_point(size = 2, alpha = 0.8) +
labs(
x = "Predicted values of house price (in USD)",
y = "Residuals"
) +
geom_hline(yintercept = 0, linetype = "dashed") +
scale_x_continuous(labels = label_dollar(scale = 1/1000, suffix = "K")) +
scale_y_continuous(labels = label_dollar(scale = 1/1000, suffix = "K"))

Checkpoint 10.22.
Consider a house with 1,803 square feet, 2.5 bathrooms, 0.145 acres, built in 1941, that has central air conditioning. What is the predicted price of the home?
Checkpoint 10.23.
If you later learned that the house (with a predicted price of $297,570) had recently sold for $804,133, would you think the model was terrible? What if you learned that the house was in California?
Solution.
A residual of $506,563 is reasonably big. Note that the large residuals (except a few homes) in FigureΒ 10.21 are closer to $250,000 (about half as big). After we learn that the house is in California, we realize that the model shouldnβt be applied to the new home at all! The original data are from Durham, NC, and models based on the Durham, NC data should be used only to explore patterns in prices for homes in Durham, NC.
Section 10.2 Interactive R tutorials
Navigate the concepts youβve learned in this part in R using the following self-paced tutorials. All you need is your browser to get started!
You can also access the full list of tutorials supporting this book at https://openintrostat.github.io/ims-tutorials.
Section 10.3 R labs
Further apply the concepts youβve learned in this part in R with computational labs that walk you through a data analysis case study.
You can also access the full list of labs supporting this book at https://www.openintro.org/go?id=ims-r-labs.
Section 10.4 Exercises
This applications chapter presents a case study with guided practice exercises embedded throughout. There are no additional end-of-chapter exercises for applications chapters.
