Chapter page 16 / 3810 Applications: Model
English

10  Applications: Model

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.1, and descriptions of each variable are shown in Table 10.2.

The duke_forest data can be found in the openintro R package.

Table 10.1: Top four rows of duke_forest.
price bed bath area year_built cooling lot
1,520,000 3 4 6,040 1,972 central 0.97
1,030,000 5 4 4,475 1,969 central 1.38
420,000 2 3 1,745 1,959 central 0.51
680,000 4 3 2,091 1,961 central 0.84
Table 10.2: Variables and their descriptions for the 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

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.1 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).

Six scatterplots where the observations are different homes. All plots have sale price on the y axis.  The x axes are number of bedrooms, number of bathrooms, square feet, year built, colling type and area of property. All variables are positively correlated with price of the house.  Square foot of the home is the most highly correlated.
Figure 10.1: Scatterplots describing six different predictor variables’ relationship with the price of a home.

In Figure 10.1 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?1

In Figure 10.1 which variable seems to be most informative for predicting house price? Provide two reasons for your answer.


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.

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

Table 10.3: Summary of least squares fit for price on area.
term estimate std.error statistic p.value
(Intercept) 116,652 53,302 2.19 0.0311
area 159 18 8.78 <0.0001
Adjusted R-sq = 0.4394
df = 96

Interpret the value of \(b_1\) = 159 in the context of the problem.2

Using the output in Table 10.3, write out the model for predicting price from area.3

The residuals from the linear model can be used to assess whether a linear model is appropriate. Figure 10.2 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.

Residual scatterplot showing predicted sale price on the y axis and residual on the y axis from the model on square feet only. There is a fan shape showing a possible deviation from the equal variance condition.
Figure 10.2: Residuals versus predicted values for the model predicting sale price from area of home.

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?4

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.4 displays the output from a linear model of price regressed on area, bed, bath, year_built, cooling, and lot.

Table 10.4: Summary of least squares fit for price on multiple predictor variables.
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

Using Table 10.4, write out the linear model of price on the six predictor variables.


\[ \begin{aligned} \widehat{\texttt{price}} = -2,910,715 &+ 102 \times \texttt{area} \\ &- 13,692 \times \texttt{bed} \\ &+ 41,076 \times \texttt{bath} \\ &+ 1,459 \times \texttt{year\_built} \\ &+ 84,065 \times \texttt{cooling}_{\texttt{central}} \\ &+ 356,141 \times \texttt{lot} \end{aligned} \]

The value of the estimated coefficient on \(\texttt{cooling}_{\texttt{central}}\) is \(b_5 = 84,065.\) Interpret the value of \(b_5\) in the context of the problem.5

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.

Results corresponding to the full model for the housing data are shown in Table 10.4. How should we proceed under the backward elimination strategy?


Our baseline adjusted \(R^2\) from the full model is 0.59, and we need to determine whether dropping a predictor will improve the adjusted \(R^2\). To check, we fit models that each drop a different predictor, and we record the adjusted \(R^2\):

  • Excluding area: 0.506
  • Excluding bed: 0.593
  • Excluding bath: 0.582
  • Excluding year_built: 0.583
  • Excluding cooling: 0.559
  • Excluding lot: 0.489

The model without bed has the highest adjusted \(R^2\) of 0.593, 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.1. 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.593. We fit another set of new models, which consider eliminating each of the remaining predictors in addition to bed:

  • Excluding bed and area: 0.51
  • Excluding bed and bath: 0.586
  • Excluding bed and year_built: 0.586
  • Excluding bed and cooling: 0.563
  • Excluding bed and lot: 0.493

None of these models lead to an improvement in adjusted \(R^2\), 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.5.

Table 10.5: Summary of least squares fit for price on multiple predictor variables, excluding number of bedrooms.
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{aligned} \widehat{\texttt{price}} = &-2,952,641 + 99 \times \texttt{area} + 36,228 \times \texttt{bath} + 1,466 \times \texttt{year\_built} \\ &+ 83,856 \times \texttt{cooling}_{\texttt{central}} + 357,119 \times \texttt{lot} \end{aligned} \]

The residual plot for the model with all of the predictor variables except bed is given in Figure 10.3. How do the residuals in Figure 10.3 compare to the residuals in Figure 10.2?


The residuals, for the most part, are randomly scattered around 0. However, there is one extreme outlier with a residual of -$750,000, a house whose actual sale price is a lot lower than its predicted price. Also, we observe again that the residuals are quite large for expensive homes.

Residual scatterplot showing predicted sale price on the y axis and residual on the y axis from the model on all variables except number of bedrooms. There is a fan shape showing a possible deviation from the equal variance condition.
Figure 10.3: Residuals versus predicted values for the model predicting sale price from all predictors except for number of bedrooms.

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?6

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?7

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

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


  1. 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.↩︎

  2. For each additional square foot of house, we would expect such houses to cost, on average, $159 more.↩︎

  3. \(\widehat{\texttt{price}} = 116,652 + 159 \times \texttt{area}\)↩︎

  4. 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.↩︎

  5. The coefficient indicates that if all the other variables are kept constant, homes with central air conditioning cost $84,065 more, on average.↩︎

  6. \(\widehat{\texttt{price}} = -2,952,641 + 99 \times 1803 + 36,228 \times 2.5 + 1,466 \times 1941 + 83,856 \times 1 + 357,119 \times 0.145 = \$297,570.\)↩︎

  7. A residual of $506,563 is reasonably big. Note that the large residuals (except a few homes) in Figure 10.3 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.↩︎

中文

10  应用:模型

10.1 案例研究:在售房屋

在你的社区周围走走,你可能会看到几套在售的房屋,而且你也许可以在网上查到它的价格。你会注意到房价在某种程度上是随意的——房主自行决定挂牌价格,而许多因素会影响这一决定,例如,类似的房屋(房地产术语中的“comps”)卖多少钱、他们需要多快卖掉房子,等等。

在本案例研究中,我们将利用当前房屋销售数据,把确定房屋挂牌价格的过程形式化。2020年11月,我们从 Zillow上抓取了北卡罗来纳州达勒姆市杜克森林社区的98套房屋的信息。在数据收集时,这些房屋都是最近售出的,该项目的目标是建立一个模型,根据特定房屋的特征来预测其售价。前四套房屋显示在 表 10.1中,每个变量的描述显示在 表 10.2.

duke_forest 数据可以在 openintro R 包中找到。

表 10.1: duke_forest.
的前四行数据 price bed 浴室 area 建造年份 year_built 地块
1,520,000 3 4 6,040 1,972 中央 0.97
1,030,000 5 4 4,475 1,969 中央 1.38
420,000 2 3 1,745 1,959 中央 0.51
680,000 4 3 2,091 1,961 中央 0.84
表 10.2:变量及其描述,用于 duke_forest 数据集的最后五行。
变量 描述
的前四行数据 price 售价,单位为美元
bed 卧室数量
浴室 浴室数量
area 房屋面积,单位为平方英尺
建造年份 房屋建造年份
year_built 制冷系统:中央或其他(其他为基准)
地块 整个房产的面积,单位为英亩

10.1.1 与之相关的是 price

如前所述,数据收集的目标是建立一个房屋售价的模型。虽然使用多个预测变量可能比只使用一个变量更可取,但我们首先来了解这些变量本身以及它们与价格的关系。 图 10.1 展示了描述价格作为每个预测变量函数的散点图。所有变量似乎都与价格呈正相关(变量的值越高,对应的价格值也越高)。

Six scatterplots where the observations are different homes. All plots have sale price on the y axis.  The x axes are number of bedrooms, number of bathrooms, square feet, year built, colling type and area of property. All variables are positively correlated with price of the house.  Square foot of the home is the most highly correlated.
图 10.1:描述六个不同预测变量与房屋价格关系的散点图。

图 10.1 中似乎没有为预测变量 cooling计算相关值。为什么?该变量还能用于线性模型吗?1

图 10.1 哪个变量似乎对预测房价最有参考价值?请给出两个理由。


area 的变量是与 price相关性最高的变量。此外, price 对比 area 的散点图似乎显示了两个变量之间很强的线性关系。请注意,相关系数和散点图的线性程度通常会得出相同的结论。然而,请记住相关系数对异常值非常敏感,因此即使变量高度相关,查看散点图也总是明智的做法。

10.1.2 残差图的哪些方面表明线性模型是合适的?残差图的哪些方面在拟合线性模型时似乎令人担忧? pricearea

建模 price 表 25.6:用 area拟合了一个线性模型来预测 表 10.3.

。所得的模型信息见
term 估计 的线性模型摘要 统计量 std.error
p.value 116,652 53,302 2.19 0.0311
area 159 18 8.78 <0.0001
调整后 R 方 = 0.4394
df = 96

解释 \(b_1\) = 159 在问题背景下的含义。2

利用 表 10.3中的输出,写出预测 price 表 25.6:用 area.3

的模型。 图 10.2 线性模型的残差可用于评估线性模型是否合适。 \(e_i = y_i - \hat{y}_i\) 轴上,以及拟合(或预测)值 \(y\)\(\hat{y}_i\) 轴上,以及拟合(或预测)值 \(x\)

Residual scatterplot showing predicted sale price on the y axis and residual on the y axis from the model on square feet only. There is a fan shape showing a possible deviation from the equal variance condition.
轴上。

图 10.2:用房屋面积预测售价的模型的残差与预测值图。4

10.1.3 残差图的哪些方面表明线性模型是合适的?残差图的哪些方面在拟合线性模型时似乎令人担忧? price 用多个变量建模

如果在线性模型中使用多个预测变量,房价的预测似乎会更准确。 表 10.4 展示了以 price 为响应变量, area, bed, bath, year_built, coolinglot.

表 10.4:price 对多个预测变量的最小二乘拟合摘要。
term 估计 的线性模型摘要 统计量 std.error
p.value -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
浴室 41,076 24,662 1.67 0.0993
建造年份 1,459 914 1.60 0.1139
中央空调制冷 84,065 30,338 2.77 0.0068
地块 356,141 75,940 4.69 <0.0001
调整后 R 方 = 0.5896
df = 90

使用 表 10.4,写出 price 对这六个预测变量的线性模型。


\[ \begin{aligned} \widehat{\texttt{price}} = -2,910,715 &+ 102 \times \texttt{area} \\ &- 13,692 \times \texttt{bed} \\ &+ 41,076 \times \texttt{bath} \\ &+ 1,459 \times \texttt{year\_built} \\ &+ 84,065 \times \texttt{cooling}_{\texttt{central}} \\ &+ 356,141 \times \texttt{lot} \end{aligned} \]

估计系数 \(\texttt{cooling}_{\texttt{central}}\) 在学生每周外出超过两个晚上时为 \(b_5 = 84,065.\) 的值 \(b_5\) 在问题背景下解释5

的值。 price一位朋友建议,也许你不需要全部六个变量就能为 建立一个良好的模型。你考虑去掉一个变量,但不确定应该删除哪一个。

住房数据完整模型对应的结果显示在 表 10.4。在向后剔除策略下我们应该如何进行?


我们从完整模型得到的基线调整 \(R^2\) 为 0.59,需要确定去掉一个预测变量是否会提高调整 \(R^2\)。为了检验这一点,我们拟合了各自去掉一个不同预测变量的模型,并记录调整 \(R^2\):

  • 剔除 area: 0.506
  • 剔除 bed: 0.593
  • 剔除 bath: 0.582
  • 剔除 year_built: 0.583
  • 剔除 cooling: 0.559
  • 剔除 lot: 0.489

不含 bed 的模型具有最高的调整 \(R^2\) ,为 0.593,高于完整模型的调整 \(R^2\) 。因为剔除 bed 后得到的模型具有更高的调整 \(R^2\) 比完整模型更好,我们从模型中剔除 bed 。从模型中排除卧室数量似乎有违直觉。毕竟,我们通常预期卧室更多的房子价格更高,而且我们可以在 图 10.1中清楚地看到卧室数量与售价之间的关系。然而,请注意 area 仍然在模型中,而且房屋面积与卧室数量很可能高度相关。因此,通过纳入 area.

,模型已经包含了“房屋内可用空间有多大”的信息。由于我们在第一步中从模型中剔除了一个预测变量,接下来我们要判断是否还应剔除其他预测变量。我们目前的基线调整 \(R^2\) 为 0.593。我们拟合另一组新模型,这些模型考虑在剔除 bed:

  • 剔除 bedarea: 0.51
  • 剔除 bedbath: 0.586
  • 剔除 bedyear_built: 0.586
  • 剔除 bedcooling: 0.563
  • 剔除 bedlot: 0.493

这些模型都没有使调整 \(R^2\)得到改善,因此我们不再剔除其余的任何预测变量。也就是说,经过向后剔除法之后,我们得到的模型保留了除

之外的所有预测变量,我们可以使用 bed中的系数对其进行总结。 表 10.5:以多个预测变量(不含卧室数量)对 price 进行最小二乘拟合的汇总。 表 10.5.

比完整模型更好,我们从模型中剔除
term 估计 的线性模型摘要 统计量 std.error
p.value -2,952,641 1,779,079 -1.66 0.1004
area 99 22 4.44 <0.0001
浴室 36,228 22,799 1.59 0.1155
建造年份 1,466 910 1.61 0.1107
中央空调制冷 83,856 30,215 2.78 0.0067
地块 357,119 75,617 4.72 <0.0001
调整后 R 方 = 0.5929
自由度 = 91

那么,基于该模型预测售价的线性模型如下:

\[ \begin{aligned} \widehat{\texttt{price}} = &-2,952,641 + 99 \times \texttt{area} + 36,228 \times \texttt{bath} + 1,466 \times \texttt{year\_built} \\ &+ 83,856 \times \texttt{cooling}_{\texttt{central}} + 357,119 \times \texttt{lot} \end{aligned} \]

包含除 bed 之外所有预测变量的模型的残差图见 图 10.3图 10.3 中的残差与 图 10.2?


中的残差相比如何?

Residual scatterplot showing predicted sale price on the y axis and residual on the y axis from the model on all variables except number of bedrooms. There is a fan shape showing a possible deviation from the equal variance condition.
残差大体上随机分布在 0 附近。然而,存在一个极端异常值,其残差为 -$750,000,这是一套实际售价远低于预测价格的房屋。此外,我们再次观察到,昂贵房屋的残差相当大。

图 10.3:用除卧室数量之外的所有预测变量预测售价的模型中,残差与预测值的关系图。6

考虑一套面积为 1,803 平方英尺、有 2.5 个浴室、占地 0.145 英亩、建于 1941 年且配有中央空调的房屋。该房屋的预测价格是多少?7

10.2 交互式 R 教程

使用以下自学教程在 R 中探索你在本部分所学的概念。你只需要一个浏览器即可开始!

您还可以访问支持本书的完整教程列表 这里.

10.3 R 实验课

通过引导您逐步完成数据分析案例研究的计算实验,在 R 中进一步应用您在本部分学到的概念。

您还可以访问支持本书的完整实验列表 这里.


  1. 相关系数只能用于描述两个数值变量之间的关系。预测变量 cooling 是分类变量,不是数值变量。不过它 可以作为二元指示变量用于线性模型中,例如用 1 表示市中心,用 0 对其他情况。↩︎

  2. 房屋每增加一平方英尺,我们预计此类房屋的价格平均高出159美元。↩︎

  3. \(\widehat{\texttt{price}} = 116,652 + 159 \times \texttt{area}\)↩︎

  4. 残差图表明, areaprice 之间的关系确实是线性的。然而,昂贵房屋的残差相当大。较大的残差表明可能存在离群值或变异性增大,这两种情况都可能需要比本章所介绍的更复杂的建模技术。↩︎

  5. 该系数表明,如果所有其他变量保持不变,有中央空调的房屋平均贵84,065美元。↩︎

  6. \(\widehat{\texttt{price}} = -2,952,641 + 99 \times 1803 + 36,228 \times 2.5 + 1,466 \times 1941 + 83,856 \times 1 + 357,119 \times 0.145 = \$297,570.\)↩︎

  7. 506,563美元的残差相当大。请注意, 图 10.3 中较大的残差(除少数房屋外)更接近250,000美元(约为前者的一半)。当我们得知该房屋位于加利福尼亚州后,我们意识到根本不应该将该模型应用于这套新房屋!原始数据来自北卡罗来纳州达勒姆,基于北卡罗来纳州达勒姆数据的模型应仅用于探索北卡罗来纳州达勒姆地区房屋价格的规律。↩︎