Chapter page 14 / 388 Linear regression with multiple predictors
English

8  Linear regression with multiple predictors

Building on the ideas of one predictor variable in a linear regression model (from Chapter 7), a multiple linear regression model is now fit to two or more predictor variables. By considering how different explanatory variables interact, we can uncover complicated relationships between the predictor variables and the response variable. One challenge to working with multiple variables is that it is sometimes difficult to know which variables are most important to include in the model. Model building is an extensive topic, and we scratch the surface here by defining and utilizing the adjusted \(R^2\) value.

Multiple regression extends single predictor variable regression to the case that still has one response but many predictors (denoted \(x_1\), \(x_2\), \(x_3\), …). The method is motivated by scenarios where many variables may be simultaneously connected to an output.

We will consider data about loans from the peer-to-peer lender, Lending Club, which is a dataset we first encountered in Chapter 1. The loan data includes terms of the loan as well as information about the borrower. The outcome variable we would like to better understand is the interest rate assigned to the loan. For instance, all other characteristics held constant, does it matter how much debt someone already has? Does it matter if their income has been verified? Multiple regression will help us answer these and other questions.

The dataset includes information on 10000 loans, and we’ll be looking at a subset of the available variables, some of which will be new from those we saw in earlier chapters. The first six observations in the dataset are shown in Table 8.1, and descriptions for each variable are shown in Table 8.2. Notice that the past bankruptcy variable (bankruptcy) is an indicator variable, where it takes the value 1 if the borrower had a past bankruptcy in their record and 0 if not. Using an indicator variable in place of a category name allows for these variables to be directly used in regression. Two of the other variables are categorical (verified_income and issue_month), each of which can take one of a few different non-numerical values; we’ll discuss how these are handled in the model in Section 8.1.

The loans_full_schema data can be found in the openintro R package. Based on the data in this dataset we have created two new variables: credit_util which is calculated as the total credit utilized divided by the total credit limit and bankruptcy which turns the number of bankruptcies to an indicator variable (0 for no bankruptcies and 1 for at least 1 bankruptcy). We will refer to this modified dataset as loans.

Table 8.1: First six rows of the loans dataset.
interest_rate verified_income debt_to_income credit_util bankruptcy term credit_checks issue_month
14.07 Verified 18.01 0.548 0 60 6 Mar-2018
12.61 Not Verified 5.04 0.150 1 36 1 Feb-2018
17.09 Source Verified 21.15 0.661 0 36 4 Feb-2018
6.72 Not Verified 10.16 0.197 0 36 0 Jan-2018
14.07 Verified 57.96 0.755 0 36 7 Mar-2018
6.72 Not Verified 6.46 0.093 0 36 6 Jan-2018
Table 8.2: Variables and their descriptions for the loans dataset.
Variable Description
interest_rate Interest rate on the loan, in an annual percentage.
verified_income Categorical variable describing whether the borrower's income source and amount have been verified, with levels Verified (source and amount verified), Source Verified (source only verified), and Not Verified.
debt_to_income Debt-to-income ratio, which is the percentage of total debt of the borrower divided by their total income.
credit_util Of all the credit available to the borrower, what fraction are they utilizing. For example, the credit utilization on a credit card would be the card's balance divided by the card's credit limit.
bankruptcy An indicator variable for whether the borrower has a past bankruptcy in their record. This variable takes a value of `1` if the answer is *yes* and `0` if the answer is *no*.
term The length of the loan, in months.
issue_month The month and year the loan was issued, which for these loans is always during the first quarter of 2018.
credit_checks Number of credit checks in the last 12 months. For example, when filing an application for a credit card, it is common for the company receiving the application to run a credit check.

8.1 Indicator and categorical predictors

Let’s start by fitting a linear regression model for interest rate with a single predictor indicating whether a person has a bankruptcy in their record:

\[ \widehat{\texttt{interest\_rate}} = 12.34 + 0.74 \times \texttt{bankruptcy} \]

Results of this model are shown in Table 8.3.

Table 8.3: Summary of a linear model for predicting interest_rate based on whether the borrower has a bankruptcy in their record. Degrees of freedom for this model is 9998.
term estimate std.error statistic p.value
(Intercept) 12.34 0.05 231.49 <0.0001
bankruptcy1 0.74 0.15 4.82 <0.0001

Interpret the coefficient for the past bankruptcy variable in the model.


The variable takes one of two values: 1 when the borrower has a bankruptcy in their history and 0 otherwise. A slope of 0.74 means that the model predicts a 0.74% higher interest rate for those borrowers with a bankruptcy in their record. (See Section 7.2.6 for a review of the interpretation for two-level categorical predictor variables.)

Suppose we had fit a model using a 3-level categorical variable, such as verified_income. The output from software is shown in Table 8.4. This regression output provides multiple rows for the variable. Each row represents the relative difference for each level of verified_income. However, we are missing one of the levels: Not Verified. The missing level is called the reference level and it represents the default level that other levels are measured against.

Table 8.4: Summary of a linear model for predicting interest_rate from the borrower’s income source and amount verification. This predictor has three levels, which results in 2 rows in the regression output.
term estimate std.error statistic p.value
(Intercept) 11.10 0.08 137.2 <0.0001
verified_incomeSource Verified 1.42 0.11 12.8 <0.0001
verified_incomeVerified 3.25 0.13 25.1 <0.0001

How would we write an equation for this regression model?


The equation for the regression model may be written as a model with two predictors:

\[ \begin{aligned} \widehat{\texttt{interest\_rate}} &= 11.10 \\ &+ 1.42 \times \texttt{verified\_income}_{\texttt{Source Verified}} \\ &+ 3.25 \times \texttt{verified\_income}_{\texttt{Verified}} \end{aligned} \]

We use the notation \(\texttt{variable}_{\texttt{level}}\) to represent indicator variables for when the categorical variable takes a particular value. For example, \(\texttt{verified\_income}_{\texttt{Source Verified}}\) would take a value of 1 if it was for a borrower that was source verified, and it would take a value of 0 otherwise. Likewise, \(\texttt{verified\_income}_{\texttt{Verified}}\) would take a value of 1 if it was for a borrower that was verified, and 0 if it took any other value.

The notation \(\texttt{variable}_{\texttt{level}}\) may feel a bit confusing. Let’s figure out how to use the equation for each level of the verified_income variable.

Using the model for predicting interest rate from income verification type, compute the average interest rate for borrowers whose income source and amount are both unverified.


When verified_income takes a value of Not Verified, then both indicator functions in the equation for the linear model are set to 0:

\[ \widehat{\texttt{interest\_rate}} = 11.10 + 1.42 \times 0 + 3.25 \times 0 = 11.10 \]

The average interest rate for these borrowers is 11.1%. Because the level does not have its own coefficient and it is the reference value, the indicators for the other levels for this variable all drop out.

Using the model for predicting interest rate from income verification type, compute the average interest rate for borrowers whose income source is source verified.


When verified_income takes a value of Source Verified, then the corresponding variable takes a value of 1 while the other is 0:

\[ \widehat{\texttt{interest\_rate}} = 11.10 + 1.42 \times 1 + 3.25 \times 0 = 12.52 \]

The average interest rate for these borrowers is 12.52%.

Compute the average interest rate for borrowers whose income source and amount are both verified.1

Predictors with several categories.

When fitting a regression model with a categorical variable that has \(k\) levels where \(k > 2\), software will provide a coefficient for \(k - 1\) of those levels. For the last level that does not receive a coefficient, this is the reference level, and the coefficients listed for the other levels are all considered relative to this reference level.

Interpret the coefficients from the model above.2

The higher interest rate for borrowers who have verified their income source or amount is surprising. Intuitively, we would think that a loan would look less risky if the borrower’s income has been verified. However, note that the situation may be more complex, and there may be confounding variables that we didn’t account for. For example, perhaps lenders require borrowers with poor credit to verify their income. That is, verifying income in our dataset might be a signal of some concerns about the borrower rather than a reassurance that the borrower will pay back the loan. For this reason, the borrower could be deemed higher risk, resulting in a higher interest rate. (What other confounding variables might explain this counter-intuitive relationship suggested by the model?)

How much larger of an interest rate would we expect for a borrower who has verified their income source and amount vs a borrower whose income source has only been verified?3

8.2 Many predictors in a model

The world is complex, and it can be helpful to consider many factors at once in statistical modeling. For example, we might like to use the full context of borrowers to predict the interest rate they receive rather than using a single variable. This is the strategy used in multiple regression. While we remain cautious about making any causal interpretations using multiple regression on observational data, such models are a common first step in gaining insights or providing some evidence of a causal connection.

We want to construct a model that accounts not only for any past bankruptcy or whether the borrower had their income source or amount verified, but simultaneously accounts for all the variables in the loans dataset: verified_income, debt_to_income, credit_util, bankruptcy, term, issue_month, and credit_checks.

\[ \begin{aligned} \widehat{\texttt{interest\_rate}} &= b_0 \\ &+ b_1 \times \texttt{verified\_income}_{\texttt{Source Verified}} + b_2 \times \texttt{verified\_income}_{\texttt{Verified}} \\ &+ b_3 \times \texttt{debt\_to\_income} + b_4 \times \texttt{credit\_util} \\ &+ b_5 \times \texttt{bankruptcy} + b_6 \times \texttt{term} \\ &+ b_7 \times \texttt{credit\_checks} + b_8 \times \texttt{issue\_month}_{\texttt{Jan-2018}} \\ &+ b_9 \times \texttt{issue\_month}_{\texttt{Mar-2018}} \end{aligned} \]

This equation represents a holistic approach for modeling all of the variables simultaneously. Notice that there are two coefficients for verified_income and two coefficients for issue_month, since both are 3-level categorical variables.

We calculate \(b_0\), \(b_1\), \(b_2\), \(\cdots\), \(b_9\) the same way as we did in the case of a model with a single predictor – we select values that minimize the sum of the squared residuals:

\[ SSE = e_1^2 + e_2^2 + \dots + e_{10000}^2 = \sum_{i=1}^{10000} e_i^2 = \sum_{i=1}^{10000} \left(y_i - \hat{y}_i\right)^2 \]

where \(y_i\) and \(\hat{y}_i\) represent the observed interest rates and their estimated values according to the model, respectively. 10,000 residuals are calculated, one for each observation. Note that these values are sample statistics and in the case where the observed data is a random sample from a target population that we are interested in making inferences about, they are estimates of the population parameters \(\beta_0\), \(\beta_1\), \(\beta_2\), \(\cdots\), \(\beta_9\). We will discuss inference based on linear models in Chapter 25, for now we will focus on calculating sample statistics \(b_i\).

We typically use a computer to minimize the sum of squares and compute point estimates, as shown in the sample output in Table 8.5. Using this output, we identify \(b_i,\) just as we did in the one-predictor case.

Table 8.5: Output for the regression model, where interest rate is the outcome and the variables listed are the predictors. Degrees of freedom for this model is 9990.
term estimate std.error statistic p.value
(Intercept) 1.89 0.21 9.01 <0.0001
verified_incomeSource Verified 1.00 0.10 10.06 <0.0001
verified_incomeVerified 2.56 0.12 21.87 <0.0001
debt_to_income 0.02 0.00 7.43 <0.0001
credit_util 4.90 0.16 30.25 <0.0001
bankruptcy1 0.39 0.13 2.96 0.0031
term 0.15 0.00 38.89 <0.0001
credit_checks 0.23 0.02 12.52 <0.0001
issue_monthJan-2018 0.05 0.11 0.42 0.6736
issue_monthMar-2018 -0.04 0.11 -0.39 0.696

Multiple regression model.

A multiple regression model is a linear model with many predictors. In general, we write the model as

\[ \hat{y} = b_0 + b_1 x_1 + b_2 x_2 + \cdots + b_k x_k \]

when there are \(k\) predictors. We always calculate \(b_i\) using statistical software.

Write out the regression model using the regression output from Table 8.5. How many predictors are there in this model?


The fitted model for the interest rate is given by:

\[ \begin{aligned} \widehat{\texttt{interest\_rate}} &= 1.89 \\ &+ 1.00 \times \texttt{verified\_income}_{\texttt{Source Verified}} + 2.56 \times \texttt{verified\_income}_{\texttt{Verified}} \\ &+ 0.02 \times \texttt{debt\_to\_income} + 4.90 \times \texttt{credit\_util} \\ &+ 0.39 \times \texttt{bankruptcy} + 0.15 \times \texttt{term} \\ &+ 0.23 \times \texttt{credit\_checks} + 0.05 \times \texttt{issue\_month}_{\texttt{Jan-2018}} \\ &- 0.04 \times \texttt{issue\_month}_{\texttt{Mar-2018}} \end{aligned} \]

If we count up the number of predictor coefficients, we get the effective number of predictors in the model; there are nine of those. Notice that the categorical predictor counts as two, once for each of the two levels shown in the model. In general, a categorical predictor with \(p\) different levels will be represented by \(p - 1\) terms in a multiple regression model. A total of seven variables were used as predictors to fit this model: verified_income, debt_to_income, credit_util, bankruptcy, term, credit_checks, issue_month.

Interpret the coefficient of the variable credit_checks.4

Compute the residual of the first observation in Table 8.1 using the full model.5

We calculated a slope coefficient of 0.74 for bankruptcy in Section 8.1 while the coefficient is 0.39 here. Why is there a difference between the coefficient values between the models with single and multiple predictors?


If we examined the data carefully, we would see that some predictors are correlated. For instance, when we modeled the relationship of the outcome interest_rate and predictor bankruptcy using linear regression, we were unable to control for other variables like whether the borrower had their income verified, the borrower’s debt-to-income ratio, and other variables. That original model was constructed in a vacuum and did not consider the full context of everything that is considered when an interest rate is decided. When we include all of the variables, underlying and unintentional bias that was missed by not including these other variables is reduced or eliminated. Of course, bias can still exist from other confounding variables.

The previous example describes a common issue in multiple regression: correlation among predictor variables. We say the two predictor variables are collinear (pronounced as co-linear) when they are correlated, and this multicollinearity complicates model estimation. While it is impossible to prevent multicollinearity from arising in observational data, experiments are usually designed to prevent predictors from being multicollinear.

The estimated value of the intercept is 1.89, and one might be tempted to make some interpretation of this coefficient, such as, it is the model’s predicted interest rate when each of the variables take value zero: income source is not verified, the borrower has no debt (debt-to-income and credit utilization are zero), and so on. Is this reasonable? Is there any value gained by making this interpretation?6

8.3 Adjusted R-squared

We first used \(R^2\) in Section 7.2.5 to determine the amount of variability in the response that was explained by the model:

\[ R^2 = 1 - \frac{\text{variability in residuals}}{\text{variability in the outcome}} = 1 - \frac{Var(e_i)}{Var(y_i)} \]

where \(e_i\) represents the residuals of the model and \(y_i\) the outcomes. This equation remains valid in the multiple regression framework, but a small enhancement can make it even more informative when comparing models.

The variance of the residuals for the model given in the earlier Guided Practice is 18.53, and the variance of the total price in all the auctions is 25.01. Calculate \(R^2\) for this model.7

This strategy for estimating \(R^2\) works when there is a single predictor. However, it becomes less helpful when there are many variables. The regular \(R^2\) is a biased estimate of the amount of variability explained by the model when applied to model with more than one predictor. To get a better estimate, we use the adjusted \(R^2\).

Adjusted R-squared as a tool for model assessment.

The adjusted R-squared is computed as

\[ \begin{aligned} R_{adj}^{2} &= 1 - \frac{s_{\text{residuals}}^2 / (n-k-1)} {s_{\text{outcome}}^2 / (n-1)} = 1 - \frac{s_{\text{residuals}}^2}{s_{\text{outcome}}^2} \times \frac{n-1}{n-k-1} \end{aligned} \]

where \(n\) is the number of observations used to fit the model and \(k\) is the number of predictor variables in the model. Remember that a categorical predictor with \(p\) levels will contribute \(p - 1\) to the number of variables in the model.

Because \(k\) is never negative, the adjusted \(R^2\) will be smaller – often times just a little smaller – than the unadjusted \(R^2\). The reasoning behind the adjusted \(R^2\) lies in the degrees of freedom associated with each variance, which is equal to \(n - k - 1\) in the multiple regression context. If we were to make predictions for new data using our current model, we would find that the unadjusted \(R^2\) would tend to be slightly overly optimistic, while the adjusted \(R^2\) formula helps correct this bias.

There were n = 10,000 auctions in the dataset and \(k=9\) predictor variables in the model. Use \(n\), \(k\), and the variances from the earlier Guided Practice to calculate adjusted \(R^2\) for the interest rate model.8

Suppose you added another predictor to the model, but the variance of the errors \(Var(e_i)\) didn’t go down. What would happen to the \(R^2\)? What would happen to the adjusted \(R^2\)?9

Adjusted \(R^2\) could also have been used in Chapter 7 where we introduced regression models with a single predictor. However, when there is only \(k = 1\) predictors, adjusted \(R^2\) is very close to regular \(R^2\), so this nuance isn’t typically important when the model has only one predictor.

8.4 Model selection

The best model is not always the most complicated. Sometimes including predictors that are not evidently important can actually reduce the accuracy of predictions. In this section, we discuss model selection strategies, which will help us eliminate predictors from the model that are found to be less important. It’s common (and hip, at least in the statistical world) to refer to models that have undergone such predictor pruning as parsimonious.

In practice, the model that includes all available predictors is often referred to as the full model. The full model may not be the best model, and if it isn’t, we want to identify a smaller model that is preferable.

8.4.1 Stepwise selection

Two common strategies for adding or removing predictors in a multiple regression model are called backward elimination and forward selection. These techniques are often referred to as stepwise selection strategies, because they add or delete one variable at a time as they “step” through the candidate predictors.

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

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

An important consideration in implementing either of these stepwise selection strategies is the criterion used to decide whether to eliminate or add a predictors. One commonly used decision criterion is adjusted \(R^2\). When using adjusted \(R^2\) as the decision criterion, we seek to eliminate or add predictors depending on whether they lead to the largest improvement in adjusted \(R^2\) and we stop when adding or elimination of another predictor does not lead to further improvement in adjusted \(R^2\).

Adjusted \(R^2\) describes the strength of a model fit, and it is a useful tool for evaluating which predictors are adding value to the model, where adding value means they are (likely) improving the accuracy in predicting future outcomes.

Let’s consider two models, which are shown in Table 8.6 and Table 8.7. The first table summarizes the full model since it includes all predictors, while the second does not include the issue_month variable.

Table 8.6: The fit for the full regression model, including the adjusted \(R^2\).
term estimate std.error statistic p.value
(Intercept) 1.89 0.21 9.01 <0.0001
verified_incomeSource Verified 1.00 0.10 10.06 <0.0001
verified_incomeVerified 2.56 0.12 21.87 <0.0001
debt_to_income 0.02 0.00 7.43 <0.0001
credit_util 4.90 0.16 30.25 <0.0001
bankruptcy1 0.39 0.13 2.96 0.0031
term 0.15 0.00 38.89 <0.0001
credit_checks 0.23 0.02 12.52 <0.0001
issue_monthJan-2018 0.05 0.11 0.42 0.6736
issue_monthMar-2018 -0.04 0.11 -0.39 0.696
Adjusted R-sq = 0.2597
df = 9964
Table 8.7: The fit for the regression model after dropping issue month, including the adjusted \(R^2\).
term estimate std.error statistic p.value
(Intercept) 1.90 0.20 9.56 <0.0001
verified_incomeSource Verified 1.00 0.10 10.05 <0.0001
verified_incomeVerified 2.56 0.12 21.86 <0.0001
debt_to_income 0.02 0.00 7.44 <0.0001
credit_util 4.90 0.16 30.25 <0.0001
bankruptcy1 0.39 0.13 2.96 0.0031
term 0.15 0.00 38.89 <0.0001
credit_checks 0.23 0.02 12.52 <0.0001
Adjusted R-sq = 0.2598
df = 9966

Which of the two models is better?


We compare the adjusted \(R^2\) of each model to determine which to choose. Since the second model has a higher adjusted \(R^2\) compared to the first model, we prefer the second model to the first. We cannot know for sure, but based on the adjusted \(R^2\), this is our best assessment.

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


Our baseline adjusted \(R^2\) from the full model is 0.2597, 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 verified_income: 0.2238
  • Excluding debt_to_income: 0.2557
  • Excluding credit_util: 0.1916
  • Excluding bankruptcy: 0.2589
  • Excluding term: 0.1468
  • Excluding credit_checks: 0.2484
  • Excluding issue_month: 0.2598

The model without issue_month has the highest adjusted \(R^2\) of 0.2598, higher than the adjusted \(R^2\) for the full model; therefore, we drop issue_month from the model.

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 \(R^2_{adj} = 0.2598\). We now fit new models, which consider eliminating each of the remaining predictors in addition to issue_month:

  • Excluding issue_month and verified_income: 0.22395
  • Excluding issue_month and debt_to_income: 0.25579
  • Excluding issue_month and credit_util: 0.19174
  • Excluding issue_month and bankruptcy: 0.25898
  • Excluding issue_month and term: 0.14692
  • Excluding issue_month and credit_checks: 0.24801

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 issue_month, which we can summarize using the coefficients from Table 8.7.

\[ \begin{aligned} \widehat{\texttt{interest\_rate}} &= 1.90 \\ &+ 1.00 \times \texttt{verified\_income}_\texttt{Source only} + 2.56 \times \texttt{verified\_income}_\texttt{Verified} \\ &+ 0.02 \times \texttt{debt\_to\_income} + 4.90 \times \texttt{credit\_util} \\ &+ 0.39 \times \texttt{bankruptcy} + 0.15 \times \texttt{term} \\ &+ 0.23 \times \texttt{credit\_checks} \end{aligned} \]

Construct a model for predicting interest_rate from the loans data using forward selection.


We start with the model that includes no predictors. Then we fit each of the possible models with just one predictor. Then we examine the adjusted \(R^2\) for each of these models:

  • Including verified_income: 0.05926
  • Including debt_to_income: 0.01946
  • Including credit_util: 0.06452
  • Including bankruptcy: 0.00222
  • Including term: 0.12855
  • Including credit_checks: -0.0001
  • Including issue_month: 0.01711

In this first step, we compare the adjusted \(R^2\) against a baseline model that has no predictors, which always has \(R_{adj}^2 = 0\). The model with one predictor that has the largest adjusted \(R^2\) is the model with the term predictor, so we will add this variable to our model.

We repeat the process again, this time considering 2-predictor models where one of the predictors is term and with a new baseline of \(R^2_{adj} = 0.12855:\)

  • Including term and verified_income: 0.16851
  • Including term and debt_to_income: 0.14368
  • Including term and credit_util: 0.20046
  • Including term and bankruptcy: 0.13070
  • Including term and credit_checks: 0.12840
  • Including term and issue_month: 0.14294

The model including credit_util has the largest increase in adjusted \(R^2\) (0.20046) from the baseline (0.12855), Thus, we will also add credit_util to the model as a predictor.

Now we have a new baseline adjusted \(R^2\) of 0.20046. We can continue on and see whether it would be beneficial to add a third predictor:

  • Including term, credit_util, and verified_income: 0.24183
  • Including term, credit_util, and debt_to_income: 0.20810
  • Including term, credit_util, and bankruptcy: 0.20169
  • Including term, credit_util, and credit_checks: 0.20031
  • Including term, credit_util, and issue_month: 0.21629

The model including verified_income has the largest increase in adjusted \(R^2\) (0.24183) from the baseline (0.20046), so we add verified_income to the model as a predictor as well.

We continue in this way, next adding debt_to_income, then credit_checks, and bankruptcy. At this point, we come again to the issue_month variable: adding this as a predictor leads to \(R_{adj}^2 = 0.25843\), while keeping all the other predictors but excluding issue_month has a higher \(R_{adj}^2 = 0.25854\). This means we do not add issue_month to the model as a predictor. In this example, we have arrived at the same model that we identified with backward elimination.

Stepwise selection strategies.

Backward elimination begins with the model having the largest number of predictors and eliminates predictors one-by-one until we are satisfied that all remaining predictors are important to the model. Forward selection starts with no predictors included in the model, then it adds in predictors according to their importance until no other important predictors are found. Notice that, for both methods, we have always chosen to retain the model with the largest adjusted \(R^2\) value, even if the difference is less than half a percent (e.g., 0.2597 versus 0.2598). One could argue that the difference between these two models is negligible, as they both explain nearly the same amount of variability in the interest_rate. These negligible differences are an important aspect to model selection. It is highly advised that before you begin the model selection process, you decide what a “meaningful” difference in adjusted \(R^2\) is for the context of your data. Maybe this difference is 1% or maybe it is 5%. This “threshold” is what you will then use to decide if one model is “better” than another model. Using meaningful thresholds in model selection requires more critical thinking about what the adjusted \(R^2\) values mean.

Additionally, backward elimination and forward selection can arrive at different final models, because the decision for whether to include a given predictor or not depends on the other predictors that are already in the model. With forward selection, you start with a model that includes no predictors, and add predictors one at a time. In backward elimination, you start with a model that includes all of the potential predictors, and remove predictors one at a time. How much a given predictor changes the percentage of the variability in the outcome that is explained by the model depends on the other predictors in the model, particularly if the predictor variables are correlated with each other.

There is no “one size fits all” model selection strategy, which is why there are so many different model selection methods. We hope you walk away from this exploration understanding how stepwise selection is carried out and the considerations that should be made when using stepwise selection with regression models.

8.4.2 Other model selection strategies

Stepwise selection using adjusted \(R^2\) as the decision criteria is one of many commonly used model selection strategies. Stepwise selection can also be carried out with decision criteria other than adjusted \(R^2\), such as p-values, which you’ll learn about in Chapter 24 onward, or AIC (Akaike information criterion) or BIC (Bayesian information criterion), which you might learn about in more advanced courses.

Alternatively, one could choose to include or exclude predictors from a model based on expert opinion or due to research focus. In fact, many statisticians discourage the use of stepwise regression alone for model selection and advocate, instead, for a more thoughtful approach that carefully considers the research focus and features of the data.

8.5 Chapter review

8.5.1 Summary

With real data, there is often a need to describe how multiple variables can be modeled together. In this chapter, we have presented one approach using multiple linear regression. Each coefficient represents how the model predicts the outcome might change with one unit increase of that predictor given the rest of the predictor variables in the model. Working with and interpreting multivariable models can be tricky, especially when the predictor variables show multicollinearity. There is often no perfect or “right” final model, however, using the adjusted \(R^2\) value is one way to identify important predictor variables for a final regression model. In later chapters we will generalize multiple linear regression models to a larger population of interest from which the dataset was sampled.

8.5.2 Terms

The terms introduced in this chapter are presented in Table 8.8. If you’re not sure what some of these terms mean, we recommend you go back in the text and review their definitions. You should be able to easily spot them as bolded text.

Table 8.8: Terms introduced in this chapter.
adjusted R-squared forward selection parsimonious
backward elimination full model reference level
collinear multicollinearity stepwise selection
degrees of freedom multiple regression

8.6 Exercises

Answers to odd-numbered exercises can be found in Appendix A.8.

  1. High correlation, good or bad? Two friends, Frances and Annika, are in disagreement about whether high correlation values are always good in the context of regression. Frances claims that it’s desirable for all variables in the dataset to be highly correlated to each other when building linear models. Annika claims that while it’s desirable for each of the predictors to be highly correlated with the outcome, it is not desirable for the predictors to be highly correlated with each other. Who is right: Frances, Annika, both, or neither? Explain your reasoning using appropriate terminology.

  2. Dealing with categorical predictors. Two friends, Elliott and Adrian, want to build a model predicting typing speed (average number of words typed per minute) from whether the person wears glasses or not. Before building the model they want to conduct some exploratory analysis to evaluate the strength of the association between these two variables, but they’re in disagreement about how to evaluate how strongly a categorical predictor is associated with a numerical outcome. Elliott claims that it is not possible to calculate a correlation coefficient to summarize the relationship between a categorical predictor and a numerical outcome, however they’re not sure what a better alternative is. Adrian claims that you can recode a binary predictor as a 0/1 variable (assign one level to be 0 and the other to be 1), thus converting it to a numerical variable. According to Adrian, you can then calculate the correlation coefficient between the predictor and the outcome. Who is right: Elliott or Adrian? If you pick Elliott, can you suggest a better alternative for evaluating the association between the categorical predictor and the numerical outcome?

  3. Meat consumption and life expectancy. In data collected for You et al. (2022), total meat intake is associated with life expectancy (at birth) in 175 countries. Meat intake is measured in kg per capita per year (averaged over 2011 to 2013). The scatterplot on the top left displays the relationship between life expectancy at birth vs. per capita meat consumption. The scatterplot on the top right displays the same relationship colored by income status of the country. The set of scatterplots across the bottom display the same relationship by income status of the country.

    1. Describe the relationship between meat consumption and life expectancy.

    2. Why do you think the variables are positively associated?

    3. Is the relationship between meat consumption and life expectancy stronger, similar, or weaker when broken down by income bracket in the separate plots along the bottom (as compared with the relationship when combined in the top left figure)?

  1. Arrival delays. Consider all of the flights out of New York City in 2013 that flew into Puerto Rico (BQN) or San Francisco (SFO) on the following two airlines: JetBlue (B6) or United Airlines (UA). We consider the relationship between day of the year and arrival delay (in minutes). Note that a negative arrival delay means that the flight arrived early. The figures display a least squares regression line for arrival delay versus time (day of the year).10

    1. Does it seem like there are differences in arrival delays across time when looking at BQN and SFO airports combined (left figure)? Explain.

    2. Does it seem like there are difference in arrival delays across time when looking at BQN and SFO airports separately (right figure)? Explain.

    3. Would it be more appropriate to display the combined plot or the plots which display the airports separately? Explain.

  2. Training for a 5K. Nico signs up for a 5K (a 5,000 metre running race) 30 days prior to the race. They decide to run a 5K every day to train for it, and each day they record the following information: days_since_start (number of days since starting training), days_till_race (number of days left until the race), mood (poor, good, awesome), tiredness (1-not tired to 10-very tired), and time (time it takes to run 5K, recorded as mm:ss). Top few rows of the data they collect is shown below. Using these data Nico wants to build a model predicting time from the other variables. Should they include all variables shown above in their model? Why or why not?

    days_since_start days_till_race mood tiredness time
    1 29 good 3 25:45
    2 28 poor 5 27:13
    3 27 awesome 4 24:13
    ... ... ... ... ...
  3. Multiple regression fact checking. Determine which of the following statements are true and false. For each statement that is false, explain why it is false.

    1. If predictors are collinear, then removing one variable will have no influence on the point estimate of another variable’s coefficient.

    2. Suppose a numerical variable \(x\) has a coefficient of \(b_1 = 2.5\) in the multiple regression model. Suppose also that the first observation has \(x_1 = 7.2\), the second observation has a value of \(x_1 = 8.2\), and these two observations have the same values for all other predictors. Then the predicted value of the second observation will be 2.5 higher than the prediction of the first observation based on the multiple regression model.

    3. If a regression model’s first variable has a coefficient of \(b_1 = 5.7\), then if we are able to influence the data so that an observation will have its \(x_1\) be 1 larger than it would otherwise, the value \(y_1\) for this observation would increase by 5.7.

  1. Baby weights and smoking. US Department of Health and Human Services, Centers for Disease Control and Prevention collect information on births recorded in the country. The data used here are a random sample of 1,000 births from 2014. Here, we study the relationship between smoking and weight of the baby. The variable smoke is coded 1 if the mother is a smoker, and 0 if not. The summary table below shows the results of a linear regression model for predicting the average birth weight of babies, measured in pounds, based on the smoking status of the mother.11 (ICPSR 2014)

    term estimate std.error statistic p.value
    (Intercept) 7.270 0.0435 167.22 <0.0001
    habitsmoker -0.593 0.1275 -4.65 <0.0001
    1. Write the equation of the regression model.

    2. Interpret the slope in this context, and calculate the predicted birth weight of babies born to smoker and non-smoker mothers.

  1. Baby weights and mature moms. The following is a model for predicting baby weight from whether the mom is classified as a mature mom (35 years or older at the time of pregnancy). (ICPSR 2014)

    term estimate std.error statistic p.value
    (Intercept) 7.354 0.103 71.02 <0.0001
    matureyounger mom -0.185 0.113 -1.64 0.102
    1. Write the equation of the regression model.

    2. Interpret the slope in this context, and calculate the predicted birth weight of babies born to mature and younger mothers.

  1. Movie returns, prediction. A model was fit to predict return-on-investment (ROI) on movies based on release year and genre (Adventure, Action, Drama, Horror, and Comedy). The model output is shown below. (FiveThirtyEight 2015)

    term estimate std.error statistic p.value
    (Intercept) -156.04 169.15 -0.92 0.3565
    release_year 0.08 0.08 0.94 0.348
    genreAdventure 0.30 0.74 0.40 0.6914
    genreComedy 0.57 0.69 0.83 0.4091
    genreDrama 0.37 0.62 0.61 0.5438
    genreHorror 8.61 0.86 9.97 <0.0001
    1. For a given release year, which genre of movies are predicted, on average, to have the highest predicted return on investment?

    2. The adjusted \(R^2\) of this model is 10.71%. Adding the production budget of the movie to the model increases the adjusted \(R^2\) to 10.84%. Should production budget be added to the model?

  1. Movie returns by genre. A model was fit to predict return-on-investment (ROI) on movies based on release year and genre (Adventure, Action, Drama, Horror, and Comedy). The plots below show the predicted ROI vs. actual ROI for each of the genres separately. Do these figures support the comment in the FiveThirtyEight.com article that states, “The return-on-investment potential for horror movies is absurd.” Note that the x-axis range varies for each plot. (FiveThirtyEight 2015)

  1. Predicting baby weights. A more realistic approach to modeling baby weights is to consider all possibly related variables at once. Other variables of interest include length of pregnancy in weeks (weeks), mother’s age in years (mage), the sex of the baby (sex), smoking status of the mother (habit), and the number of hospital (visits) visits during pregnancy. Below are three observations from this dataset.

    weight weeks mage sex visits habit
    6.96 37 34 male 14 nonsmoker
    8.86 41 31 female 12 nonsmoker
    7.51 37 36 female 10 nonsmoker

    The summary table below shows the results of a regression model for predicting the average birth weight of babies based on all of the variables presented above.

    term estimate std.error statistic p.value
    (Intercept) -3.82 0.57 -6.73 <0.0001
    weeks 0.26 0.01 18.93 <0.0001
    mage 0.02 0.01 2.53 0.0115
    sexmale 0.37 0.07 5.30 <0.0001
    visits 0.02 0.01 2.09 0.0373
    habitsmoker -0.43 0.13 -3.41 7e-04
    1. Write the equation of the regression model that includes all of the variables.

    2. Interpret the slopes of weeks and habit in this context.

    3. If we fit a model predicting baby weight from only habit (whether the mom smokes), we observe a difference in the slope coefficient for habit in this small model and the slope coefficient for habit in the larger model. Why might there be a difference?

    4. Calculate the residual for the first observation in the dataset.

  1. Palmer penguins, predicting body mass. Researchers studying a community of Antarctic penguins collected body measurement (bill length, bill depth, and flipper length measured in millimeters and body mass, measured in grams), species (Adelie, Chinstrap, or Gentoo), and sex (female or male) data on 344 penguins living on three islands (Torgersen, Biscoe, and Dream) in the Palmer Archipelago, Antarctica.12 The summary table below shows the results of a linear regression model for predicting body mass (which is more difficult to measure) from the other variables in the dataset. (Gorman et al. 2014)

    term estimate std.error statistic p.value
    (Intercept) -1461.0 571.3 -2.6 0.011
    bill_length_mm 18.2 7.1 2.6 0.0109
    bill_depth_mm 67.2 19.7 3.4 7e-04
    flipper_length_mm 16.0 2.9 5.5 <0.0001
    sexmale 389.9 47.8 8.1 <0.0001
    speciesChinstrap -251.5 81.1 -3.1 0.0021
    speciesGentoo 1014.6 129.6 7.8 <0.0001
    1. Write the equation of the regression model.

    2. Interpret each one of the slopes in this context.

    3. Calculate the residual for a male Adelie penguin that weighs 3,750 grams with the following body measurements:

      • bill_length_mm = 39.1
      • bill_depth_mm = 18.7
      • flipper_length_mm = 181

      Does the model overpredict or underpredict this penguin’s weight?

    4. The \(R^2\) of this model is 87.5%. Interpret this value in context of the data and the model.

  1. Baby weights, backwards elimination. Let’s consider a model that predicts weight of newborns using several predictors: whether the mother is considered mature, number of weeks of gestation, number of hospital visits during pregnancy, weight gained by the mother during pregnancy, sex of the baby, and whether the mother smoke cigarettes during pregnancy (habit). (ICPSR 2014)

    The adjusted \(R^2\) of the full model is 0.326. We remove each variable one by one, refit the model, and record the adjusted \(R^2\). Which, if any, variable should be removed from the model?

    • Drop mature: 0.321
    • Drop weeks: 0.061
    • Drop visits: 0.326
    • Drop gained: 0.327
    • Drop sex: 0.301
  1. Palmer penguins, backwards elimination. The following full model is built to predict the weights of three species (Adelie, Chinstrap, or Gentoo) of penguins living in the Palmer Archipelago, Antarctica. (Gorman et al. 2014)

    term estimate std.error statistic p.value
    (Intercept) -1461.0 571.3 -2.6 0.011
    bill_length_mm 18.2 7.1 2.6 0.0109
    bill_depth_mm 67.2 19.7 3.4 7e-04
    flipper_length_mm 16.0 2.9 5.5 <0.0001
    sexmale 389.9 47.8 8.1 <0.0001
    speciesChinstrap -251.5 81.1 -3.1 0.0021
    speciesGentoo 1014.6 129.6 7.8 <0.0001

    The adjusted \(R^2\) of the full model is 0.9. In order to evaluate whether any of the predictors can be dropped from the model without losing predictive performance of the model, the researchers dropped one variable at a time, refit the model, and recorded the adjusted \(R^2\) of the smaller model. These values are given below.

    • Drop bill_length_mm: 0.87
    • Drop bill_depth_mm: 0.869
    • Drop flipper_length_mm: 0.861
    • Drop sex: 0.845
    • Drop species: 0.821

    Which, if any, variable should be removed from the model first?

  1. Baby weights, forward selection. Using information on the mother and the sex of the baby (which can be determined prior to birth), we want to build a model that predicts the birth weight of babies. In order to do so, we will evaluate six candidate predictors: whether the mother is considered mature, number of weeks of gestation, number of hospital visits during pregnancy, weight gained by the mother during pregnancy, sex of the baby, and whether the mother smoke cigarettes during pregnancy (habit). And we will make a decision about including them in the model using forward selection and adjusted \(R^2\). Below are the six models we evaluate and their adjusted \(R^2\) values. (ICPSR 2014)

    • Predict weight from mature: 0.002
    • Predict weight from weeks: 0.3
    • Predict weight from visits: 0.034
    • Predict weight from gained: 0.021
    • Predict weight from sex: 0.018
    • Predict weight from habit: 0.021

    Which variable should be added to the model first?

  1. Palmer penguins, forward selection. Using body measurement and other relevant data on three species (Adelie, Chinstrap, or Gentoo) of penguins living in the Palmer Archipelago, Antarctica, we want to predict their body mass. In order to do so, we will evaluate five candidate predictors and make a decision about including them in the model using forward selection and adjusted \(R^2\). Below are the five models we evaluate and their adjusted \(R^2\) values:

    • Predict body mass from bill_length_mm: 0.352
    • Predict body mass from bill_depth_mm: 0.22
    • Predict body mass from flipper_length_mm: 0.758
    • Predict body mass from sex: 0.178
    • Predict body mass from species: 0.668

    Which variable should be added to the model first?


  1. When verified_income takes a value of Verified, then the corresponding variable takes a value of 1 while the other is 0: \(11.10 + 1.42 \times 0 + 3.25 \times 1 = 14.35.\) The average interest rate for these borrowers is 14.35%.↩︎

  2. Each of the coefficients gives the incremental interest rate for the corresponding level relative to the Not Verified level, which is the reference level. For example, for a borrower whose income source and amount have been verified, the model predicts that they will have a 3.25% higher interest rate than a borrower who has not had their income source or amount verified.↩︎

  3. Relative to the Not Verified category, the Verified category has an interest rate of 3.25% higher, while the Source Verified category is only 1.42% higher. Thus, Verified borrowers will tend to get an interest rate about \(3.25\% - 1.42\% = 1.83\%\) higher than Source Verified borrowers.↩︎

  4. All else held constant, for each additional inquiry into the applicant’s credit during the last 12 months, we would expect the interest rate for the loan to be higher, on average, by 0.23 points.↩︎

  5. To compute the residual, we first need the predicted value, which we compute by plugging values into the equation from earlier. For example, \(\texttt{verified\_income}_{\texttt{Source Verified}}\) takes a value of 0, \(\texttt{verified\_income}_{\texttt{Verified}}\) takes a value of 1 (since the borrower’s income source and amount were verified), \(\texttt{debt\_to\_income}\) was 18.01, and so on. This leads to a prediction of \(\widehat{\texttt{interest\_rate}}_1 = 17.84\). The observed interest rate was 14.07%, which leads to a residual of \(e_1 = 14.07 - 17.84 = -3.77\).↩︎

  6. Many of the variables do take a value 0 for at least one data point, and for those variables, it is reasonable. However, one variable never takes a value of zero: term, which describes the length of the loan, in months. If term is set to zero, then the loan must be paid back immediately; the borrower must give the money back as soon as they receive it, which means it is not a real loan. Ultimately, the interpretation of the intercept in this setting is not insightful.↩︎

  7. \(R^2 = 1 - \frac{18.53}{25.01} = 0.2591\).↩︎

  8. \(R_{adj}^2 = 1 - \frac{18.53}{25.01}\times \frac{10000-1}{10000-9-1} = 0.2584\). While the difference is very small, it will be important when we fine tune the model in the next section.↩︎

  9. The unadjusted \(R^2\) would stay the same and the adjusted \(R^2\) would go down.↩︎

  10. The flights data used in this exercise can be found in the nycflights13 R package.↩︎

  11. The births14 data used in this exercise can be found in the openintro R package.↩︎

  12. The penguins data used in this exercise can be found in the palmerpenguins R package.↩︎

中文

8  含多个预测变量的线性回归

在一元线性回归模型中单个预测变量思想的基础上(见 第 7),我们现在将多元线性回归模型拟合到两个或更多预测变量。通过考虑不同解释变量之间的交互作用,我们可以揭示预测变量与响应变量之间的复杂关系。使用多个变量的一个挑战在于,有时很难知道哪些变量最重要、应该纳入模型。模型构建是一个内容广泛的话题,我们在此仅通过定义并使用调整后的 \(R^2\) 值来略作介绍。

多元回归将单预测变量回归扩展到仍只有一个响应变量但有多个预测变量(记为 \(x_1\), \(x_2\), \(x_3\), …)的情形。该方法的动因来自许多变量可能同时与某个输出相关联的场景。

我们将考虑来自点对点借贷平台 Lending Club 的贷款数据,这是我们在 第 1中首次接触到的数据集。贷款数据包括贷款条款以及借款人的信息。我们希望更深入理解的结局变量是贷款所分配的利率。例如,在所有其他特征保持不变的情况下,某人已有的债务数额是否重要?其收入是否经过验证是否重要?多元回归将帮助我们回答这些以及其他问题。

该数据集包含 10000 笔贷款的信息,我们将考察其中可用变量的一个子集,其中一些变量是前面章节中未曾见过的新变量。数据集的前六个观测显示在 表 8.1中,每个变量的描述显示在 表 8.2中。注意,过去破产变量(bankruptcy)是一个指示变量,如果借款人的记录中有过去破产史,则取值为 1,否则为 0。用指示变量代替类别名称,可以使这些变量直接用于回归。另外两个变量是分类变量(verified_incomeissue_month), 其中每个变量可以取几个不同的非数值之一;我们将在 第 8.1 节.

loans_full_schema 数据可以在 openintro 数据。基于该数据集中的数据,我们创建了两个新变量: credit_util ,其计算方式为已使用的总信用额度除以总信用限额;以及 bankruptcy ,它将破产次数转换为指示变量(0 表示没有破产,1 表示至少破产 1 次)。我们将把这个修改后的数据集称为 loans.

表 8.1: loans 数据集的最后五行。
interest_rate verified_income 债务收入比 credit_util bankruptcy term 信用查询次数 issue_month
14.07 2018年1月 18.01 0.548 0 60 6 2018年3月
12.61 未验证 5.04 0.150 1 36 1 来源已验证
17.09 2018年2月 21.15 0.661 0 36 4 来源已验证
6.72 未验证 10.16 0.197 0 36 0 2018年1月
14.07 2018年1月 57.96 0.755 0 36 7 2018年3月
6.72 未验证 6.46 0.093 0 36 6 2018年1月
表8.2:变量及其描述,用于 loans 数据集的最后五行。
变量 描述
interest_rate 贷款利率,以年百分比表示。
verified_income 分类变量,描述借款人的收入来源和金额是否经过验证,取值包括 Verified(来源和金额均已验证)、Source Verified(仅验证来源)和 Not Verified(未验证)。
债务收入比 债务收入比,即借款人的总债务除以其总收入的百分比。
credit_util 在借款人可用的全部信用额度中,他们使用了多少比例。例如,信用卡的信用使用率就是卡的余额除以卡的信用额度。
bankruptcy 指示变量,表示借款人的记录中是否有过去的破产史。如果答案是*是*,该变量取值为 `1`;如果答案是*否*,则取值为 `0`。
term 贷款期限,以月为单位。
issue_month 贷款发放的月份和年份,对于这些贷款,总是在2018年第一季度。
信用查询次数 过去12个月内的信用查询次数。例如,在提交信用卡申请时,接收申请的公司通常会进行信用查询。

8.1 指示变量与分类预测变量

让我们首先为利率拟合一个仅含单个预测变量的线性回归模型,该预测变量表示一个人的记录中是否有破产:

\[ \widehat{\texttt{interest\_rate}} = 12.34 + 0.74 \times \texttt{bankruptcy} \]

该模型的结果显示在 表 8.3.

表 8.3:基于借款人记录中是否有破产来预测 interest_rate 的线性模型摘要。该模型的自由度为 9998。
term 估计 的线性模型摘要 统计量 std.error
p.value 12.34 0.05 231.49 <0.0001
bankruptcy1 0.74 0.15 4.82 <0.0001

解释模型中过去破产变量的系数。


该变量取两个值之一:当借款人历史中有破产时取 1,否则取 0。斜率 0.74 意味着模型预测记录中有破产的借款人的利率会高出 0.74%。(参见 第 7.2.6 节 以回顾对二水平分类预测变量的解释。)

假设我们使用一个 3 水平的分类变量来拟合模型,例如 verified_income。软件的输出显示在 表 8.4中。该回归输出为该变量提供了多行。每一行代表 verified_income各水平的相对差异。然而,我们缺少其中一个水平: Not Verified。缺失的水平称为 参考水平 ,它代表其他水平与之比较的默认水平。

表 8.4:根据借款人的收入来源和金额验证情况预测 interest_rate 的线性模型汇总。该预测变量有三个水平,因此回归输出中有 2 行。
term 估计 的线性模型摘要 统计量 std.error
p.value 11.10 0.08 137.2 <0.0001
verified_incomeSource Verified 1.42 0.11 12.8 <0.0001
verified_incomeVerified 3.25 0.13 25.1 <0.0001

我们该如何为这个回归模型写出方程?


该回归模型的方程可以写成包含两个预测变量的模型:

\[ \begin{aligned} \widehat{\texttt{interest\_rate}} &= 11.10 \\ &+ 1.42 \times \texttt{verified\_income}_{\texttt{Source Verified}} \\ &+ 3.25 \times \texttt{verified\_income}_{\texttt{Verified}} \end{aligned} \]

我们使用记号 \(\texttt{variable}_{\texttt{level}}\) 来表示当分类变量取某个特定值时的指示变量。例如, \(\texttt{verified\_income}_{\texttt{Source Verified}}\) 在借款人为来源验证(source verified)时取值为 1,否则取值为 0。同样, \(\texttt{verified\_income}_{\texttt{Verified}}\) 在借款人为已验证(verified)时取值为 1,取其他任何值时为 0。

记号 \(\texttt{variable}_{\texttt{level}}\) 可能让人有些困惑。让我们弄清楚如何对 verified_income 变量所示的那个。

的每个水平使用该方程。 使用根据收入验证类型预测利率的模型,计算收入来源和金额均为.


verified_income 根据 Not Verified的借款人的平均利率。当它取值为 0 时,线性模型方程中的两个指示函数都被设为 0:

\[ \widehat{\texttt{interest\_rate}} = 11.10 + 1.42 \times 0 + 3.25 \times 0 = 11.10 \]

这些借款人的平均利率为11.1%。由于该水平没有自己的系数,且它是参考值,因此该变量其他水平的指标全部被剔除。

使用根据收入验证类型预测利率的模型,计算收入来源为……的借款人的平均利率 来源已验证.


verified_income 根据 Source Verified,则对应的变量取值为1,另一个为0:

\[ \widehat{\texttt{interest\_rate}} = 11.10 + 1.42 \times 1 + 3.25 \times 0 = 12.52 \]

这些借款人的平均利率为12.52%。

计算收入来源和金额均为……的借款人的平均利率 已验证.1

具有多个类别的预测变量。

当拟合包含一个具有 \(k\) 个水平(其中 \(k > 2\))的分类变量的回归模型时,软件会为其中 \(k - 1\) 个水平提供系数。对于最后一个没有获得系数的水平,它是参考水平,其他水平所列出的系数都是相对于该参考水平而言的。

解释上述模型的系数。2

已验证收入来源或金额的借款人利率反而更高,这令人意外。直观上,我们会认为贷款看起来 更不 如果借款人的收入已经过验证,则存在风险。但请注意,情况可能更为复杂,可能存在我们未考虑到的混杂变量。例如,也许贷款机构要求信用较差的借款人验证其收入。也就是说,在我们的数据集中,验证收入可能意味着对借款人存在某些担忧,而不是对借款人会偿还贷款的保证。因此,该借款人可能被视为风险更高,从而导致更高的利率。(还有哪些混杂变量可以解释模型所提示的这种反直觉的关系?)

对于已验证收入来源和金额的借款人,与仅验证了收入来源的借款人相比,我们预期其利率会高出多少?3

8.2 模型中的多个预测变量

世界是复杂的,在统计建模中同时考虑多个因素会很有帮助。例如,我们可能希望利用借款人的完整背景信息来预测他们获得的利率,而不是只使用单一变量。这正是 多元回归中采用的策略。尽管我们对在观察性数据上使用多元回归进行因果解释仍保持谨慎,但此类模型是获得洞见或提供因果联系证据的常见第一步。

我们想构建一个模型,不仅考虑过去的破产情况或借款人的收入来源或金额是否经过验证,还同时考虑 loans 数据集中的所有变量: verified_income, debt_to_income, credit_util, bankruptcy, term, issue_monthcredit_checks.

\[ \begin{aligned} \widehat{\texttt{interest\_rate}} &= b_0 \\ &+ b_1 \times \texttt{verified\_income}_{\texttt{Source Verified}} + b_2 \times \texttt{verified\_income}_{\texttt{Verified}} \\ &+ b_3 \times \texttt{debt\_to\_income} + b_4 \times \texttt{credit\_util} \\ &+ b_5 \times \texttt{bankruptcy} + b_6 \times \texttt{term} \\ &+ b_7 \times \texttt{credit\_checks} + b_8 \times \texttt{issue\_month}_{\texttt{Jan-2018}} \\ &+ b_9 \times \texttt{issue\_month}_{\texttt{Mar-2018}} \end{aligned} \]

这个方程代表了一种对所有变量同时建模的整体方法。注意, verified_income 有两个系数, issue_month也有两个系数,因为它们都是具有3个水平的分类变量。

我们按照与单预测变量模型相同的方式计算 \(b_0\), \(b_1\), \(b_2\), \(\cdots\), \(b_9\) ——我们选择使残差平方和最小的值:

\[ SSE = e_1^2 + e_2^2 + \dots + e_{10000}^2 = \sum_{i=1}^{10000} e_i^2 = \sum_{i=1}^{10000} \left(y_i - \hat{y}_i\right)^2 \]

其中 \(y_i\)\(\hat{y}_i\) 分别代表观测到的利率以及根据模型得到的估计值。共计算了10,000个残差,每个观测值对应一个。请注意,这些值是样本统计量,当观测数据是来自我们想要进行推断的目标总体的随机样本时,它们是总体参数 \(\beta_0\), \(\beta_1\), \(\beta_2\), \(\cdots\), \(\beta_9\)的估计值。我们将在 第 25中讨论基于线性模型的推断,现在我们将专注于计算样本统计量 \(b_i\).

我们通常使用计算机来最小化平方和并计算点估计,如 表 8.5中的示例输出所示。利用该输出,我们像在单预测变量情形中那样确定 \(b_i,\)

表 8.5:回归模型的输出,其中利率为结果变量,所列变量为预测变量。该模型的自由度为 9990。
term 估计 的线性模型摘要 统计量 std.error
p.value 1.89 0.21 9.01 <0.0001
verified_incomeSource Verified 1.00 0.10 10.06 <0.0001
verified_incomeVerified 2.56 0.12 21.87 <0.0001
债务收入比 0.02 0.00 7.43 <0.0001
credit_util 4.90 0.16 30.25 <0.0001
bankruptcy1 0.39 0.13 2.96 0.0031
term 0.15 0.00 38.89 <0.0001
信用查询次数 0.23 0.02 12.52 <0.0001
bankruptcy1 0.05 0.11 0.42 0.6736
issue_monthJan-2018 -0.04 0.11 -0.39 0.696

多元回归模型。

多元回归模型是具有多个预测变量的线性模型。一般而言,我们将该模型写为

\[ \hat{y} = b_0 + b_1 x_1 + b_2 x_2 + \cdots + b_k x_k \]

当有 \(k\) 个预测变量时。我们总是使用统计软件计算 \(b_i\)

利用回归输出写出回归模型,来自 表 8.5. 该模型中有多少个预测变量?


利率的拟合模型如下:

\[ \begin{aligned} \widehat{\texttt{interest\_rate}} &= 1.89 \\ &+ 1.00 \times \texttt{verified\_income}_{\texttt{Source Verified}} + 2.56 \times \texttt{verified\_income}_{\texttt{Verified}} \\ &+ 0.02 \times \texttt{debt\_to\_income} + 4.90 \times \texttt{credit\_util} \\ &+ 0.39 \times \texttt{bankruptcy} + 0.15 \times \texttt{term} \\ &+ 0.23 \times \texttt{credit\_checks} + 0.05 \times \texttt{issue\_month}_{\texttt{Jan-2018}} \\ &- 0.04 \times \texttt{issue\_month}_{\texttt{Mar-2018}} \end{aligned} \]

如果我们数一数预测变量系数的个数,就能得到模型中 有效 的预测变量个数;共有九个。注意,分类预测变量计为两个,即模型中所示的两个水平各计一次。一般而言,具有 \(p\) 个不同水平的分类预测变量在多元回归模型中将由 \(p - 1\) 个项来表示。该模型共使用了七个变量作为预测变量: verified_income, debt_to_income, credit_util, bankruptcy, term, credit_checks, issue_month.

使用完整模型解释变量 credit_checks.4

计算 表 8.1 的系数。5

我们在 bankruptcy第 8.1 节 中为


计算出的斜率系数为 0.74,而这里的系数为 0.39。为什么单预测变量模型与多预测变量模型的系数值会有差异? interest_rate 。例如,当我们用简单线性回归估计结果变量 bankruptcy 如果我们仔细检查数据,就会发现一些预测变量之间存在相关。例如,当我们用线性回归对结果

的关系建模时,我们无法控制其他变量,比如借款人的收入是否经过核实、借款人的债务收入比以及其他变量。那个最初的模型是在真空中构建的,没有考虑决定利率时所考虑的全部背景因素。当我们纳入所有这些变量后,由于未纳入这些其他变量而遗漏的潜在的、无意的偏差就会被减少或消除。当然,来自其他混杂变量的偏差仍然可能存在。 共线性 (发音为 共线的)当它们相关时,这种 如果我们仔细检查数据,会发现某些预测变量之间存在 会使模型估计变得复杂。虽然在观察数据中无法防止多重共线性的出现,但实验通常会被设计成防止预测变量之间出现多重共线性。

截距的估计值为 1.89,人们可能会想对这个系数做一些解释,例如,它是当所有变量都取零值时模型的预测利率:收入来源未经核实、借款人没有债务(债务收入比和信用使用率为零),等等。这样解释合理吗?做出这种解释有什么价值吗?6

8.3 调整后的 R 方

我们首先使用 \(R^2\)第 7.2.5 节 来确定模型所解释的响应变量变异量:

\[ R^2 = 1 - \frac{\text{variability in residuals}}{\text{variability in the outcome}} = 1 - \frac{Var(e_i)}{Var(y_i)} \]

其中 \(e_i\) 表示模型的残差, \(y_i\) 表示结果。这个等式在多元回归框架中仍然有效,但稍作改进就能在比较模型时提供更多信息。

前面引导练习中给出的模型的残差方差为 18.53,所有拍卖中总价的方差为 25.01。请计算该模型的 \(R^2\)7

这种估计 \(R^2\) 的策略在只有一个预测变量时有效。然而,当有许多变量时,它的作用就变小了。当应用于具有多个预测变量的模型时,常规的 \(R^2\) 是对模型所解释变异量的有偏估计。为了得到更好的估计,我们使用调整后的 \(R^2\).

调整后R方作为模型评估的工具。

调整后R方 的计算公式为

\[ \begin{aligned} R_{adj}^{2} &= 1 - \frac{s_{\text{residuals}}^2 / (n-k-1)} {s_{\text{outcome}}^2 / (n-1)} = 1 - \frac{s_{\text{residuals}}^2}{s_{\text{outcome}}^2} \times \frac{n-1}{n-k-1} \end{aligned} \]

其中 \(n\) 是用于拟合模型的观测数量, \(k\) 是模型中预测变量的数量。请记住,一个具有 \(p\) 个水平的分类预测变量会为模型中的变量数量贡献 \(p - 1\) 个变量。

因为 \(k\) 永远不会为负,所以调整后的 \(R^2\) 会比未调整的 \(R^2\)小——通常只是略小一点。调整后 \(R^2\) 背后的原理在于每个方差所关联的 自由度 ,在多元回归的背景下它等于 \(n - k - 1\) 。如果我们要使用当前模型对 新数据 进行预测,我们会发现未调整的 \(R^2\) 往往会略微过于乐观,而调整后的 \(R^2\) 公式有助于纠正这一偏差。

数据集中有 n = 10,000 场拍卖,模型中有 \(k=9\) 个预测变量。使用 \(n\), \(k\)以及之前“引导练习”中的方差来计算利率模型的调整后 \(R^2\)8

假设你向模型中添加了另一个预测变量,但误差的方差 \(Var(e_i)\) 并没有下降。那么 \(R^2\)会发生什么变化?调整后的 \(R^2\)?9

不再进一步提升,我们就停止。 \(R^2\) 调整后的 第 7 也可以用于 \(k = 1\) 中,我们在那里介绍了只有一个预测变量的回归模型。然而,当只有 \(R^2\) 个预测变量时,调整后的 \(R^2\)与普通的

8.4 非常接近,因此当模型只有一个预测变量时,这一细节通常并不重要。

最好的模型并不总是最复杂的。有时加入那些并不明显重要的预测变量反而会降低预测的准确性。在本节中,我们将讨论模型选择策略,这将帮助我们剔除模型中被认为不太重要的预测变量。经过这种预测变量删减的模型通常被称为(至少在统计学界很流行这样称呼) 简约.

在实践中,包含所有可用预测变量的模型通常被称为 完整模型。全模型未必是最好的模型,如果不是,我们就希望找出一个更优的更小的模型。

8.4.1 逐步选择

在多元回归模型中添加或删除预测变量的两种常见策略称为向后剔除法和向前选择法。这些技术通常被称为 逐步选择 策略,因为它们在“逐步”遍历候选预测变量时,每次只添加或删除一个变量。

向后剔除法 从全模型开始——即包含所有潜在预测变量的模型。然后从模型中一次剔除一个预测变量,直到无法进一步改进模型为止。

向前选择法 与向后剔除法相反。它不是一次剔除一个预测变量,而是一次添加一个预测变量,直到找不到任何能进一步改进模型的预测变量为止。

在实施这两种逐步选择策略时,一个重要的考虑因素是用于决定是否剔除或添加预测变量的准则。一个常用的决策准则是调整 \(R^2\)。当使用调整 \(R^2\) 作为决策准则时,我们根据预测变量是否能最大程度地提高调整 \(R^2\) 当我们增加或删除另一个预测变量后,若调整后 \(R^2\).

不再进一步提升,我们就停止。 \(R^2\) 调整后 描述了模型拟合的强度,它是评估哪些预测变量为模型增加价值的有效工具,其中 增加价值

意味着它们(很可能)提高了预测未来结果的准确性。 表 8.6表 8.7让我们考虑两个模型,如 issue_month 变量所示的那个。

所示。第一个表格总结了完整模型,因为它包含所有预测变量,而第二个表格则不包含 \(R^2\).
term 估计 的线性模型摘要 统计量 std.error
p.value 1.89 0.21 9.01 <0.0001
verified_incomeSource Verified 1.00 0.10 10.06 <0.0001
verified_incomeVerified 2.56 0.12 21.87 <0.0001
债务收入比 0.02 0.00 7.43 <0.0001
credit_util 4.90 0.16 30.25 <0.0001
bankruptcy1 0.39 0.13 2.96 0.0031
term 0.15 0.00 38.89 <0.0001
信用查询次数 0.23 0.02 12.52 <0.0001
bankruptcy1 0.05 0.11 0.42 0.6736
issue_monthJan-2018 -0.04 0.11 -0.39 0.696
issue_monthMar-2018
Adjusted R-sq = 0.2597 调整后 R 方 = 0.2597
表 8.7:剔除贷款发放月份后的回归模型拟合结果,包括调整后 \(R^2\).
term 估计 的线性模型摘要 统计量 std.error
p.value 1.90 0.20 9.56 <0.0001
verified_incomeSource Verified 1.00 0.10 10.05 <0.0001
verified_incomeVerified 2.56 0.12 21.86 <0.0001
债务收入比 0.02 0.00 7.44 <0.0001
credit_util 4.90 0.16 30.25 <0.0001
bankruptcy1 0.39 0.13 2.96 0.0031
term 0.15 0.00 38.89 <0.0001
信用查询次数 0.23 0.02 12.52 <0.0001
调整后 R-sq = 0.2598
df = 9966

两个模型中哪个更好?


我们比较每个模型的调整后 \(R^2\) 来确定选择哪个模型。由于第二个模型的调整后 \(R^2\) 高于第一个模型,我们更倾向于选择第二个模型而非第一个。我们无法完全确定,但基于调整后 \(R^2\),这是我们最好的判断。

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


我们从完整模型得到的基线调整 \(R^2\) 中。完整模型的调整后 \(R^2\)。为了检验这一点,我们拟合了各自去掉一个不同预测变量的模型,并记录调整 \(R^2\):

  • 剔除 verified_income: 0.2238
  • 剔除 debt_to_income: 0.2557
  • 剔除 credit_util: 0.1916
  • 剔除 bankruptcy: 0.2589
  • 剔除 term: 0.1468
  • 剔除 credit_checks: 0.2484
  • 剔除 issue_month: 0.2598

不含 issue_month 的模型具有最高的调整 \(R^2\) 为 0.2597,我们需要确定剔除一个预测变量是否会提高调整后。完整模型的调整后 R-sq 为 0.2598,高于调整后 \(R^2\) 对于完整模型;因此,我们从模型中剔除 issue_month 从模型中。

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

  • 剔除 issue_monthverified_income: 0.22395
  • 剔除 issue_monthdebt_to_income: 0.25579
  • 剔除 issue_monthcredit_util: 0.19174
  • 剔除 issue_monthbankruptcy: 0.25898
  • 剔除 issue_monthterm: 0.14692
  • 剔除 issue_monthcredit_checks: 0.24801

这些模型都没有使调整 \(R^2\)之外,还考虑剔除其余每个预测变量,因此我们不剔除任何剩余的预测变量。也就是说,经过向后剔除后,我们得到的模型保留了除 issue_month中的系数对其进行总结。 表 10.5:以多个预测变量(不含卧室数量)对 price 进行最小二乘拟合的汇总。 表 8.7.

\[ \begin{aligned} \widehat{\texttt{interest\_rate}} &= 1.90 \\ &+ 1.00 \times \texttt{verified\_income}_\texttt{Source only} + 2.56 \times \texttt{verified\_income}_\texttt{Verified} \\ &+ 0.02 \times \texttt{debt\_to\_income} + 4.90 \times \texttt{credit\_util} \\ &+ 0.39 \times \texttt{bankruptcy} + 0.15 \times \texttt{term} \\ &+ 0.23 \times \texttt{credit\_checks} \end{aligned} \]

之外的所有预测变量。 interest_rate 构建一个用于预测 loans 的模型,使用


数据并通过向前选择法进行。 \(R^2\) 我们从不含任何预测变量的模型开始。然后我们拟合每个仅含一个预测变量的可能模型。接着我们检验这些模型的调整后

  • 包含 verified_income: 0.05926
  • 包含 debt_to_income: 0.01946
  • 包含 credit_util: 0.06452
  • 包含 bankruptcy: 0.00222
  • 包含 term: 0.12855
  • 包含 credit_checks: -0.0001
  • 包含 issue_month: 0.01711

在第一步中,我们将调整后的 \(R^2\) 与一个没有预测变量的基线模型进行比较,该基线模型的 \(R_{adj}^2 = 0\)始终为 \(R^2\) 。在只有一个预测变量的模型中,调整后 term 最大的模型是包含

预测变量的模型,因此我们将把这个变量加入我们的模型。 term 我们再次重复这一过程,这次考虑的是包含两个预测变量的模型,其中一个预测变量是 \(R^2_{adj} = 0.12855:\)

  • 包含 termverified_income: 0.16851
  • 包含 termdebt_to_income: 0.14368
  • 包含 termcredit_util: 0.20046
  • 包含 termbankruptcy: 0.13070
  • 包含 termcredit_checks: 0.12840
  • 包含 termissue_month: 0.14294

包含 credit_util 的模型其调整后 \(R^2\) (0.20046) 相比基线 (0.12855) 有所提升。因此,我们也将 credit_util 作为预测变量加入模型。

现在我们有了一个新的调整后基线 \(R^2\) 为 0.20046。我们可以继续,看看添加第三个预测变量是否有益:

  • 包含 term, credit_utilverified_income: 0.24183
  • 包含 term, credit_utildebt_to_income: 0.20810
  • 包含 term, credit_utilbankruptcy: 0.20169
  • 包含 term, credit_utilcredit_checks: 0.20031
  • 包含 term, credit_utilissue_month: 0.21629

包含 verified_income 的模型其调整后 \(R^2\) (0.24183) 相比基线 (0.20046) 增幅最大,因此我们也将 verified_income 作为预测变量加入模型。

我们以这种方式继续,接下来添加 debt_to_income,则 credit_checksbankruptcy。此时,我们再次遇到 issue_month 变量:将其作为预测变量加入会导致 \(R_{adj}^2 = 0.25843\),同时保留所有其他预测变量,但排除 issue_month\(R_{adj}^2 = 0.25854\)值更高。这意味着我们不将 issue_month 作为预测变量加入模型。在本例中,我们得到了与向后消元法所识别出的相同的模型。

逐步选择策略。

向后消元法从包含最多预测变量的模型开始,逐一剔除预测变量,直到我们确信所有剩余的预测变量对模型都很重要。向前选择法从不含任何预测变量的模型开始,然后根据预测变量的重要性逐一加入,直到找不到其他重要的预测变量。注意,对于这两种方法,我们始终选择保留调整后 \(R^2\) 值最大的模型,即使差异小于半个百分点(例如,0.2597 与 0.2598)。有人可能会认为这两个模型之间的差异可以忽略不计,因为它们解释了 interest_rate中几乎相同数量的变异性。这些可忽略的差异是模型选择的一个重要方面。强烈建议 看到数据之前 你在开始模型选择过程之前,先确定对于你的数据背景而言,调整后 \(R^2\) 的多大差异才算“有意义”。这个差异可能是 1%,也可能是 5%。这个“阈值”将用于判断一个模型是否比另一个模型“更好”。在模型选择中使用有意义的阈值,需要对调整后 \(R^2\) 值的含义进行更深入的批判性思考。

此外,向后消元法和向前选择法可能会得到不同的最终模型,因为是否纳入某个给定预测变量的决定取决于模型中已有的其他预测变量。使用向前选择法时,你从一个不含任何预测变量的模型开始,一次加入一个预测变量。使用向后消元法时,你从一个包含所有潜在预测变量的模型开始,一次剔除一个预测变量。某个给定预测变量对模型所解释的结果变异百分比的影响程度,取决于模型中的其他预测变量,尤其是当预测变量之间彼此相关时。

没有“放之四海而皆准”的模型选择策略,这也是为什么存在如此多不同的模型选择方法。我们希望你在完成这一探索后,能够理解逐步选择是如何进行的,以及在回归模型中使用逐步选择时应考虑哪些因素。

8.4.2 其他模型选择策略

以调整后 \(R^2\) 作为决策标准的逐步选择法是许多常用模型选择策略之一。逐步选择法也可以使用调整后 \(R^2\),例如p值,你将在 第24章 及之后章节学到,或者AIC(赤池信息准则)或BIC(贝叶斯信息准则),你可能会在更高级的课程中学到。

另外,也可以根据专家意见或研究重点来选择在模型中纳入或排除预测变量。事实上,许多统计学家不鼓励仅使用逐步回归 单独 进行模型选择,而是提倡一种更审慎的方法,即仔细考虑研究重点和数据的特征。

8.5 本章复习

8.5.1 小结

对于真实数据,常常需要描述多个变量如何一起被建模。在本章中,我们介绍了使用多元线性回归的一种方法。每个系数表示,在模型中 给定 其余预测变量的情况下,该预测变量每增加一个单位,模型预测的结果会如何变化。使用和解释多变量模型可能比较棘手,尤其是当预测变量之间存在多重共线性时。通常没有完美或“正确”的最终模型,不过,使用调整后的 \(R^2\) 值是确定最终回归模型中重要预测变量的一种方法。在后面的章节中,我们将把多元线性回归模型推广到数据集所抽样的更大目标总体。

8.5.2 术语

本章中介绍的术语列于 表 8.8。如果您不确定其中一些术语的含义,我们建议您回到正文中复习它们的定义。您应该能够很容易地发现它们是 粗体文本.

表 8.8:本章介绍的术语。
调整后R方 前向选择 简约
后向剔除 完整模型 参考水平
共线性 如果我们仔细检查数据,会发现某些预测变量之间存在 逐步选择
自由度 多元回归

8.6 练习

奇数编号习题的答案见 附录 A.8.

  1. 高相关性,是好还是坏? 两位朋友Frances和Annika对于高相关值在回归中是否 总是 有益存在分歧。Frances认为,在构建线性模型时,数据集中所有变量彼此高度相关是理想的。Annika认为,虽然每个预测变量与结果变量高度相关是理想的,但预测变量彼此之间高度相关并不理想。谁是对的:Frances、Annika、都对,还是都不对?请使用适当的术语解释你的理由。

  2. 处理分类预测变量。 两位朋友Elliott和Adrian想建立一个模型,根据一个人是否戴眼镜来预测打字速度(每分钟平均打字字数)。在建模之前,他们想进行一些探索性分析来评估这两个变量之间关联的强度,但对于如何评估分类预测变量与数值结果变量之间关联的强度,他们意见不一。Elliott认为,无法计算相关系数来概括分类预测变量与数值结果变量之间的关系,但他不确定有什么更好的替代方法。Adrian认为,可以将二元预测变量重新编码为0/1变量(将其中一个水平赋值为0,另一个赋值为1),从而将其转换为数值变量。根据Adrian的说法,然后就可以计算预测变量与结果变量之间的相关系数。谁是对的:Elliott还是Adrian?如果你选择Elliott,你能为评估分类预测变量与数值结果变量之间的关联提出一个更好的替代方法吗?

  3. 肉类消费与预期寿命。 在为 You et al. (2022),在175个国家中,肉类总摄入量与预期寿命(出生时)相关。肉类摄入量以每人每年公斤为单位衡量(2011年至2013年的平均值)。左上角的散点图显示了出生时预期寿命与人均肉类消费量之间的关系。右上角的散点图显示了相同的关系,并按国家的收入状况着色。底部的一组散点图按国家收入状况显示了相同的关系。

    1. 描述肉类消费与预期寿命之间的关系。

    2. 你认为为什么这些变量呈正相关?

    3. 与左上角图中合并显示的关系相比,当按收入区间在底部的各个单独图中分解时,肉类消费与预期寿命之间的关系是更强、相似还是更弱?

  1. 到达延误。 考虑2013年从纽约市出发、飞往波多黎各(BQN)或旧金山(SFO)、且属于以下两家航空公司的所有航班:捷蓝航空(B6)或联合航空(UA)。我们考虑一年中的日期与到达延误(以分钟为单位)之间的关系。请注意,负的到达延误意味着航班提前到达。图中显示了到达延误与时间(一年中的日期)之间的最小二乘回归线。10

    1. 当将BQN和SFO机场合并观察时(左图),到达延误在不同时间上似乎是否存在差异?请解释。

    2. 分别观察 BQN 和 SFO 两个机场时(右图),到达延误在不同时间上是否存在差异?请解释。

    3. 展示合并的图还是分别展示各机场的图更合适?请解释。

  2. 为 5 公里跑进行训练。 Nico 在比赛前 30 天报名参加了一场 5K(5000 米跑步比赛)。他们决定每天跑一次 5K 来训练,并每天记录以下信息: days_since_start (开始训练以来的天数), days_till_race (距离比赛还剩的天数), mood (糟糕、良好、极好), tiredness (1-不累 到 10-非常累),以及 time (跑完 5K 所需的时间,记录为 mm:ss)。他们收集的数据的前几行如下所示。Nico 想用这些数据建立一个模型来预测 time ,自变量为其他变量。他们应该在模型中包含上面显示的所有变量吗?为什么?

    days_since_start days_till_race mood tiredness time
    1 29 good 3 25:45
    2 28 poor 5 27:13
    3 27 很棒 4 24:13
    ... ... ... ... ...
  3. 多元回归事实核查。 判断下列陈述的真假。对于每个错误的陈述,解释其错误的原因。

    1. 如果预测变量之间存在共线性,那么移除其中一个变量不会影响另一个变量系数的点估计。

    2. 假设一个数值变量 \(x\) 在多元回归模型中的系数为 \(b_1 = 2.5\) 。再假设第一个观测值为 \(x_1 = 7.2\),第二个观测值为 \(x_1 = 8.2\),并且这两个观测值在所有其他预测变量上的取值相同。那么基于多元回归模型,第二个观测值的预测值将比第一个观测值的预测值高 2.5。

    3. 如果回归模型的第一个变量的系数为 \(b_1 = 5.7\),那么如果我们能够影响数据,使某个观测值的 \(x_1\) 比原本大 1,则该观测值的 \(y_1\) 将增加 5.7。

  1. 婴儿体重与吸烟。 美国卫生与公众服务部下属的疾病控制与预防中心收集该国记录的出生信息。这里使用的数据是2014年1000例出生的随机样本。在此,我们研究吸烟与婴儿体重之间的关系。变量 smoke 在母亲为吸烟者时编码为1,否则为0。下面的汇总表显示了基于母亲吸烟状况预测婴儿平均出生体重(以磅为单位)的线性回归模型的结果。11 (ICPSR 2014)

    term 估计 的线性模型摘要 统计量 std.error
    p.value 7.270 0.0435 167.22 <0.0001
    habitsmoker -0.593 0.1275 -4.65 <0.0001
    1. 写出回归模型的方程。

    2. 在此背景下解释斜率的含义,并计算吸烟母亲和不吸烟母亲所生婴儿的预测出生体重。

  1. 婴儿体重与高龄产妇。 以下是一个根据母亲是否被归类为 mature 母亲(怀孕时35岁或以上)来预测婴儿体重的模型。 (ICPSR 2014)

    term 估计 的线性模型摘要 统计量 std.error
    p.value 7.354 0.103 71.02 <0.0001
    matureyounger mom(成熟-年轻母亲) -0.185 0.113 -1.64 0.102
    1. 写出回归模型的方程。

    2. 在此情境下解释斜率的含义,并计算成熟母亲和年轻母亲所生婴儿的预测出生体重。

  1. 电影回报,预测。 根据上映年份和类型(冒险、动作、剧情、恐怖和喜剧)拟合了一个模型,用于预测电影的投资回报率(ROI)。模型输出如下所示。 (FiveThirtyEight 2015)

    term 估计 的线性模型摘要 统计量 std.error
    p.value -156.04 169.15 -0.92 0.3565
    release_year(上映年份) 0.08 0.08 0.94 0.348
    genreAdventure(冒险类型) 0.30 0.74 0.40 0.6914
    genreComedy(喜剧类型) 0.57 0.69 0.83 0.4091
    genreDrama(剧情类型) 0.37 0.62 0.61 0.5438
    genreHorror(恐怖类型) 8.61 0.86 9.97 <0.0001
    1. 对于给定的上映年份,哪种类型的电影平均预测投资回报率最高?

    2. 调整后的 \(R^2\) 为10.71%。将电影的制作预算加入模型后,调整后 \(R^2\) 提高到10.84%。是否应该将制作预算加入模型?

  1. 按电影类型分析票房回报。 拟合了一个模型,根据上映年份和电影类型(冒险、动作、剧情、恐怖和喜剧)来预测电影的投资回报率(ROI)。下图分别显示了每种电影类型的预测 ROI 与实际 ROI 的对比。这些图表是否支持 FiveThirtyEight.com 文章中的评论,即“恐怖电影的投资回报潜力是荒谬的”?请注意,每个图的 x 轴范围各不相同。 (FiveThirtyEight 2015)

  1. 预测婴儿体重。 对婴儿体重建模的一种更现实的方法是同时考虑所有可能相关的变量。其他感兴趣的变量包括以周为单位的妊娠时长(weeks)、以岁为单位的母亲年龄(mage)、婴儿性别(sex)、母亲的吸烟状况(habit)以及孕期就诊医院的次数(visits)。以下是该数据集中的三个观测值。

    weight weeks mage sex visits habit
    6.96 37 34 男性 14 习惯
    8.86 41 31 女性 12 习惯
    7.51 37 36 女性 10 习惯

    下面的汇总表显示了基于上述所有变量预测婴儿平均出生体重的回归模型的结果。

    term 估计 的线性模型摘要 统计量 std.error
    p.value -3.82 0.57 -6.73 <0.0001
    weeks 0.26 0.01 18.93 <0.0001
    mage 0.02 0.01 2.53 0.0115
    sexmale 0.37 0.07 5.30 <0.0001
    visits 0.02 0.01 2.09 0.0373
    habitsmoker -0.43 0.13 -3.41 7e-04
    1. 写出包含所有变量的回归模型的方程。

    2. 在此背景下解释 weekshabit 的斜率。

    3. 如果我们仅用 habit (母亲是否吸烟)来拟合预测婴儿体重的模型,我们会观察到这个小模型中 habit 的斜率系数与 habit 在更大的模型中。为什么可能会有差异?

    4. 计算数据集中第一个观测值的残差。

  1. 帕尔默企鹅,预测体重。 研究南极企鹅群落的研究人员收集了344只企鹅的身体测量数据(喙长、喙深和鳍长,以毫米为单位;体重,以克为单位)、物种(阿德利, 帽带,或 巴布亚)以及性别(雌性或雄性)数据,这些企鹅生活在南极洲帕尔默群岛的三个岛屿(Torgersen、Biscoe 和 Dream)上。12 下面的汇总表显示了用数据集中其他变量预测体重(体重较难测量)的线性回归模型的结果。 (Gorman 等人 2014)

    term 估计 的线性模型摘要 统计量 std.error
    p.value -1461.0 571.3 -2.6 0.011
    预测 18.2 7.1 2.6 0.0109
    的较大回归模型的最小二乘估计 67.2 19.7 3.4 7e-04
    bill_length_mm 16.0 2.9 5.5 <0.0001
    sexmale 389.9 47.8 8.1 <0.0001
    flipper_length_mm -251.5 81.1 -3.1 0.0021
    sexmale 1014.6 129.6 7.8 <0.0001
    1. 写出回归模型的方程。

    2. 在此背景下解释每一个斜率。

    3. 计算一只体重为3,750克、具有以下身体测量数据的雄性阿德利企鹅的残差:

      • bill_length_mm = 39.1
      • bill_depth_mm = 18.7
      • flipper_length_mm = 181

      该模型是高估还是低估了这只企鹅的体重?

    4. \(R^2\) 该模型的 为87.5%。请在数据和模型的背景下解释这个值。

  1. 婴儿体重,向后消元法。 我们来考虑一个预测新生儿 的模型,使用以下几个预测变量:母亲是否被认为 weight 、妊娠 周数、住院 次数(住院天数)等。 mature,数量为 weeks 妊娠期、住院次数 visits 孕期、体重 gained 母亲在怀孕期间的 sex 婴儿的,以及母亲在怀孕期间是否吸烟(habit). (ICPSR 2014)

    调整后的 \(R^2\) 完整模型的 为 0.326。我们逐一移除每个变量,重新拟合模型,并记录调整后的 \(R^2\)。哪些变量(如果有的话)应该从模型中移除?

    • 去掉 mature: 0.321
    • 去掉 weeks: 0.061
    • 去掉 visits: 0.326
    • 去掉 gained: 0.327
    • 去掉 sex: 0.301
  1. 帕尔默企鹅,向后消元法。 以下完整模型用于预测生活在南极洲帕尔默群岛的三种企鹅(阿德利, 帽带,或 巴布亚)的体重。 (Gorman 等人 2014)

    term 估计 的线性模型摘要 统计量 std.error
    p.value -1461.0 571.3 -2.6 0.011
    预测 18.2 7.1 2.6 0.0109
    的较大回归模型的最小二乘估计 67.2 19.7 3.4 7e-04
    bill_length_mm 16.0 2.9 5.5 <0.0001
    sexmale 389.9 47.8 8.1 <0.0001
    flipper_length_mm -251.5 81.1 -3.1 0.0021
    sexmale 1014.6 129.6 7.8 <0.0001

    调整后的 \(R^2\) 完整模型的 为 0.9。为了评估是否可以在不损失模型预测性能的情况下从模型中删除任何预测变量,研究人员每次删除一个变量,重新拟合模型,并记录较小模型的调整后 \(R^2\) 。这些值如下所示。

    • 去掉 bill_length_mm: 0.87
    • 去掉 bill_depth_mm: 0.869
    • 去掉 flipper_length_mm: 0.861
    • 去掉 sex: 0.845
    • 去掉 species: 0.821

    哪些变量(如果有的话)应该首先从模型中移除?

  1. 婴儿体重,向前选择法。 利用关于母亲和婴儿性别的信息(可在出生前确定),我们想建立一个预测婴儿出生体重的模型。为此,我们将评估六个候选预测变量:母亲是否被认为 mature,数量为 weeks 妊娠期、住院次数 visits 孕期、体重 gained 母亲在怀孕期间的 sex 婴儿的,以及母亲在怀孕期间是否吸烟(habit)。我们将使用前向选择和调整后的 \(R^2\)来决定是否将它们纳入模型。以下是我们评估的六个模型及其调整后的 \(R^2\) 值。 (ICPSR 2014)

    • 预测 weight 表 25.6:用 mature: 0.002
    • 预测 weight 表 25.6:用 weeks: 0.3
    • 预测 weight 表 25.6:用 visits: 0.034
    • 预测 weight 表 25.6:用 gained: 0.021
    • 预测 weight 表 25.6:用 sex: 0.018
    • 预测 weight 表 25.6:用 habit: 0.021

    预测体重

  1. 帕尔默企鹅,前向选择。 使用三种物种(阿德利, 帽带,或 巴布亚)的企鹅,我们想预测它们的体重。为此,我们将评估五个候选预测变量,并使用前向选择和调整后 \(R^2\)来决定是否将它们纳入模型。以下是我们评估的五个模型及其调整后 \(R^2\) 值:

    • 根据 bill_length_mm: 0.352
    • 根据 bill_depth_mm: 0.22
    • 根据 flipper_length_mm: 0.758
    • 根据 sex: 0.178
    • 根据 species: 0.668

    预测体重


  1. verified_income 根据 Verified预测体重 \(11.10 + 1.42 \times 0 + 3.25 \times 1 = 14.35.\) 根据↩︎

  2. 预测体重 Not Verified 应该首先将哪个变量加入模型?↩︎

  3. 取值为 Not Verified ,则相应变量取值为 1,另一个取值为 0:”, Verified 类别的利率高出 3.25%,而 Source Verified 类别仅高出 1.42%。因此, Verified 借款人的利率往往比 \(3.25\% - 1.42\% = 1.83\%\) 借款人高约 Source Verified↩︎

  4. 在其他条件不变的情况下,申请人过去 12 个月内每增加一次信用查询,我们预计贷款利率平均会高出 0.23 个点。↩︎

  5. 要计算残差,我们首先需要预测值,即将数值代入之前的方程来计算。例如, \(\texttt{verified\_income}_{\texttt{Source Verified}}\) 取值为 0, \(\texttt{verified\_income}_{\texttt{Verified}}\) 取值为 1(因为借款人的收入来源和金额已得到核实), \(\texttt{debt\_to\_income}\) 为 18.01,依此类推。由此得到预测值 \(\widehat{\texttt{interest\_rate}}_1 = 17.84\)。实际观测到的利率为 14.07%,由此得到残差 \(e_1 = 14.07 - 17.84 = -3.77\).↩︎

  6. 许多变量至少在某一个数据点上取值为 0,对这些变量来说,这是合理的。然而,有一个变量从不取零值: term,它描述贷款期限,以月为单位。如果 term 被设为 0,那么贷款必须立即偿还;借款人一收到钱就必须归还,这意味着这并不是一笔真正的贷款。归根结底,在此情境下对截距的解释并无实际意义。↩︎

  7. \(R^2 = 1 - \frac{18.53}{25.01} = 0.2591\).↩︎

  8. \(R_{adj}^2 = 1 - \frac{18.53}{25.01}\times \frac{10000-1}{10000-9-1} = 0.2584\)。虽然差异非常小,但在下一节对模型进行微调时它将变得很重要。↩︎

  9. 未经调整的 \(R^2\) 将保持不变,而调整后的 \(R^2\) 将会下降。↩︎

  10. flights 本练习中使用的数据可在 nycflights13 R 包中找到。↩︎

  11. births14 本练习中使用的数据可在 openintro R 包中找到。↩︎

  12. penguins 本练习中使用的数据可在 palmerpenguins R 包中找到。↩︎