Chapter page 15 / 389 Logistic regression
English

9  Logistic regression

In this chapter we introduce logistic regression as a tool for building models when there is a categorical response variable with two levels, e.g., yes and no. Logistic regression is a type of generalized linear model (GLM) for response variables where regular multiple regression does not work very well. GLMs can be thought of as a two-stage modeling approach. We first model the response variable using a probability distribution, such as the binomial or Poisson distribution. Second, we model the parameter of the distribution using a collection of predictors and a special form of multiple regression. Ultimately, the application of a GLM will feel very similar to multiple regression, even if some of the details are different.

9.1 Discrimination in hiring

We will consider experiment data from a study that sought to understand the effect of race and sex on job application callback rates (Bertrand and Mullainathan 2003). To evaluate which factors were important, job postings were identified in Boston and Chicago for the study, and researchers created many fake resumes to send off to these jobs to see which would elicit a callback.1 The researchers enumerated important characteristics, such as years of experience and education details, and they used these characteristics to randomly generate fake resumes. Finally, they randomly assigned a name to each resume, where the name would imply the applicant’s sex and race.

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

The first names that were used and randomly assigned in the experiment were selected so that they would predominantly be recognized as belonging to Black or White individuals; other races were not considered in the study. While no name would definitively be inferred as pertaining to a Black individual or to a White individual, the researchers conducted a survey to check for racial association of the names; names that did not pass the survey check were excluded from usage in the experiment. You can find the full set of names that did pass the survey test and were ultimately used in the study in Table 9.1. For example, Lakisha was a name that their survey indicated would be interpreted as a Black woman, while Greg was a name that would generally be interpreted to be associated with a White male.

Table 9.1: List of all 36 unique names along with the commonly inferred race and sex associated with these names.
first_name race sex first_name race sex first_name race sex
Aisha Black female Hakim Black male Laurie White female
Allison White female Jamal Black male Leroy Black male
Anne White female Jay White male Matthew White male
Brad White male Jermaine Black male Meredith White female
Brendan White male Jill White female Neil White male
Brett White male Kareem Black male Rasheed Black male
Carrie White female Keisha Black female Sarah White female
Darnell Black male Kenya Black female Tamika Black female
Ebony Black female Kristen White female Tanisha Black female
Emily White female Lakisha Black female Todd White male
Geoffrey White male Latonya Black female Tremayne Black male
Greg White male Latoya Black female Tyrone Black male

The response variable of interest is whether there was a callback from the employer for the applicant, and there were 8 attributes that were randomly assigned that we’ll consider, with special interest in the race and sex variables. Race and sex are protected classes in the United States, meaning they are not legally permitted factors for hiring or employment decisions. The full set of attributes considered is provided in Table 26.1.

Table 9.2: Descriptions of nine variables from the resume dataset. Many of the variables are indicator variables, meaning they take the value 1 if the specified characteristic is present and 0 otherwise.
variable description
received_callback Specifies whether the employer called the applicant following submission of the application for the job.
job_city City where the job was located: Boston or Chicago.
college_degree An indicator for whether the resume listed a college degree.
years_experience Number of years of experience listed on the resume.
honors Indicator for the resume listing some sort of honors, e.g. employee of the month.
military Indicator for if the resume listed any military experience.
has_email_address Indicator for if the resume listed an email address for the applicant.
race Race of the applicant, implied by their first name listed on the resume.
sex Sex of the applicant (limited to only man and woman), implied by the first name listed on the resume.

All of the attributes listed on each resume were randomly assigned, which means that no attributes that might be favorable or detrimental to employment would favor one demographic over another on these resumes. Importantly, due to the experimental nature of the study, we can infer causation between these variables and the callback rate, if substantial differences are found. Our analysis will allow us to compare the practical importance of each of the variables relative to each other.

9.2 Modelling the probability of an event

Logistic regression is a generalized linear model where the outcome is a two-level categorical variable. The outcome, \(Y_i\), takes the value 1 (in our application, the outcome represents a callback for the resume) with probability \(p_i\) and the value 0 with probability \(1 - p_i\). Because each observation has a slightly different context, e.g., different education level or a different number of years of experience, the probability \(p_i\) will differ for each observation. Ultimately, it is the probability of the outcome taking the value 1 (i.e., being a “success”) that we model in relation to the predictor variables: we will examine which resume characteristics correspond to higher or lower callback rates.

Notation for a logistic regression model.

The outcome variable for a GLM is denoted by \(Y_i\), where the index \(i\) is used to represent observation \(i\). In the resume application, \(Y_i\) will be used to represent whether resume \(i\) received a callback (\(Y_i=1\)) or not (\(Y_i=0\)).

The predictor variables are represented as follows: \(x_{1,i}\) is the value of variable 1 for observation \(i\), \(x_{2,i}\) is the value of variable 2 for observation \(i\), and so on.

\[ transformation(p_i) = \beta_0 + \beta_1 x_{1,i} + \beta_2 x_{2,i} + \cdots + \beta_k x_{k,i} \]

We want to choose a transformation in the equation that makes practical and mathematical sense. For example, we want a transformation that makes the range of possibilities on the left hand side of the equation equal to the range of possibilities for the right hand side; if there was no transformation in the equation, the left hand side could only take values between 0 and 1, but the right hand side could take values well outside of the range from 0 to 1.

A common transformation for \(p_i\) is the logit transformation, which may be written as

\[ logit(p_i) = \log_{e}\left( \frac{p_i}{1-p_i} \right) \]

The logit transformation is shown in Figure 9.1. Below, we rewrite the equation relating \(Y_i\) to its predictors using the logit transformation of \(p_i\):

\[ \log_{e}\left( \frac{p_i}{1-p_i} \right) = \beta_0 + \beta_1 x_{1,i} + \beta_2 x_{2,i} + \cdots + \beta_k x_{k,i} \]

A scatterplot with a superimposed line connecting values of p from -5 to +6 (on the x-axis) with their logit transformed value (on the y-axis).
Figure 9.1: Values of \(p_i\) against values of \(logit(p_i)\).

In our resume example, there are 8 predictor variables, so \(k = 8\). While the precise choice of a logit function isn’t intuitive, it is based on theory that underpins generalized linear models, which is beyond the scope of this book. Fortunately, once we fit a model using software, it will start to feel like we are back in the multiple regression context, even if the interpretation of the coefficients is more complex.

To convert from values on the logistic regression scale to the probability scale, we need to back transform and then solve for \(p_i\):

\[ \begin{aligned} \log_{e}\left( \frac{p_i}{1-p_i} \right) &= \beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i} \\ \frac{p_i}{1-p_i} &= e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}} \\ p_i &= \left( 1 - p_i \right) e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}} \\ p_i &= e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}} - p_i \times e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}} \\ p_i + p_i \text{ } e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}} &= e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}} \\ p_i(1 + e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}}) &= e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}} \\ p_i &= \frac{e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}}}{1 + e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}}} \end{aligned} \]

As with most applied data problems, we substitute in the point estimates (the observed \(b_i\)) to calculate relevant probabilities.

We start by fitting a model with a single predictor: honors. This variable indicates whether the applicant had any type of honors listed on their resume, such as employee of the month. A logistic regression model was fit using statistical software and the following model was found:

\[\log_e \left( \frac{\widehat{p}_i}{1-\widehat{p}_i} \right) = -2.4998 + 0.8668 \times {\texttt{honors}}\]

  1. If a resume is randomly selected from the study and it does not have any honors listed, what is the probability it resulted in a callback?

  2. What would the probability be if the resume did list some honors?


  1. If a randomly chosen resume from those sent out is considered, and it does not list honors, then honors takes the value of 0 and the right side of the model equation equals -2.4998. Solving for \(p_i\): \(\frac{e^{-2.4998}}{1 + e^{-2.4998}} = 0.076\). Just as we labeled a fitted value of \(y_i\) with a “hat” in single-variable and multiple regression, we do the same for this probability: \(\hat{p}_i = 0.076{}\).

  2. If the resume had listed some honors, then the right side of the model equation is \(-2.4998 + 0.8668 \times 1 = -1.6330\), which corresponds to a probability \(\hat{p}_i = 0.163\). Notice that we could examine -2.4998 and -1.6330 in Figure 9.1 to estimate the probability before formally calculating the value.

While knowing whether a resume listed honors provides some signal when predicting whether the employer would call, we would like to account for many different variables at once to understand how each of the different resume characteristics affected the chance of a callback.

9.3 Logistic model with many variables

We used statistical software to fit the logistic regression model with all 8 predictors described in Table 26.1. Like multiple regression, the result may be presented in a summary table, which is shown in Table 9.3.

Table 9.3: Summary table for the full logistic regression model for the resume callback example.
term estimate std.error statistic p.value
(Intercept) -2.66 0.18 -14.64 <0.0001
job_cityChicago -0.44 0.11 -3.85 1e-04
college_degree1 -0.07 0.12 -0.55 0.5821
years_experience 0.02 0.01 1.96 0.0503
honors1 0.77 0.19 4.14 <0.0001
military1 -0.34 0.22 -1.59 0.1127
has_email_address1 0.22 0.11 1.93 0.0541
raceWhite 0.44 0.11 4.10 <0.0001
sexman -0.18 0.14 -1.32 0.1863

Just like multiple regression, we could trim some variables from the model. Here we’ll use a statistic called Akaike information criterion (AIC), which is analogous to how we used adjusted \(R^2\) in multiple regression. AIC is a popular model selection method used in many disciplines, and is praised for its emphasis on model uncertainty and parsimony. AIC selects a “best” model by ranking models from best to worst according to their AIC values. In the calculation of a model’s AIC, a penalty is given for including additional variables. The penalty for added model complexity attempts to strike a balance between underfitting (too few variables in the model) and overfitting (too many variables in the model). When using AIC for model selection, models with a lower AIC value are considered to be “better.” Remember that when using adjusted \(R^2\) we select models with higher values instead. It is important to note that AIC provides information about the quality of a model relative to other models, but does not provide information about the overall quality of a model.

Table 9.4 provides the AIC and the number of observations used to fit the model. We also know from Table 9.3 that eight variables (with nine coefficients, including the intercept) were fit.

Table 9.4: AIC for the full logistic regression model fit to the full resume callback example.
AIC number_observations
2677 4870

We will look for models with a lower AIC using a backward elimination strategy. Table 9.5 provides the AIC values for the model with variables as given in Table 9.6. Notice that the same number of observations are used, but one fewer variable (college_degree is dropped from the model).

Table 9.5: AIC for the logistic regression model fit to the resume callback example without college_degree.
AIC number_observations
2676 4870

After using the AIC criteria, the variable college_degree is eliminated (the AIC value without college_degree is smaller than the AIC value on the full model), giving the model summarized in Table 9.6 with fewer variables, which is what we’ll rely on for the remainder of the section.

Table 9.6: Summary table for the logistic regression model for the resume callback example, where variable selection has been performed using AIC and college_degree has been dropped from the model.
term estimate std.error statistic p.value
(Intercept) -2.72 0.16 -17.51 <0.0001
job_cityChicago -0.44 0.11 -3.83 1e-04
years_experience 0.02 0.01 2.02 0.043
honors1 0.76 0.19 4.12 <0.0001
military1 -0.34 0.22 -1.60 0.1105
has_email_address1 0.22 0.11 1.97 0.0494
raceWhite 0.44 0.11 4.10 <0.0001
sexman -0.20 0.14 -1.45 0.1473

The race variable had taken only two levels: Black and White. Based on the model results, what does the coefficient of the race variable say about callback decisions?


The coefficient shown corresponds to the level of White, and it is positive. The positive coefficient reflects a positive gain in callback rate for resumes where the candidate’s first name implied they were White. The model results suggest that prospective employers favor resumes where the first name is typically interpreted to be White.

The coefficient of \(\texttt{race}_{\texttt{White}}\) in the full model in Table 9.3, is nearly identical to the model shown in Table 9.6. The predictors in the experiment were thoughtfully laid out so that the coefficient estimates would typically not be much influenced by which other predictors were in the model, which aligned with the motivation of the study to tease out which effects were important to getting a callback. In most observational data, it’s common for point estimates to change a little, and sometimes a lot, depending on which other variables are included in the model.

Use the model summarized in Table 9.6 to estimate the probability of receiving a callback for a job in Chicago where the candidate lists 14 years experience, no honors, no military experience, includes an email address, and has a first name that implies they are a White male.


We can start by writing out the equation using the coefficients from the model:

\[ \begin{aligned} log_e \left(\frac{\widehat{p}}{1 - \widehat{p}}\right) = -2.7162 &- 0.4364 \times \texttt{job\_city}_{\texttt{Chicago}} + 0.0206 \times \texttt{years\_experience} \\ &+ 0.7634 \times \texttt{honors} - 0.3443 \times \texttt{military} + 0.2221 \times \texttt{email} \\ &+ 0.4429 \times \texttt{race}_{\texttt{White}} - 0.1959 \times \texttt{sex}_{\texttt{man}} \end{aligned} \]

Now we can add in the corresponding values of each variable for the individual of interest:

\[ \begin{aligned} log_e \left(\frac{\widehat{p}}{1 - \widehat{p}}\right) = - 2.7162 &- 0.4364 \times 1 + 0.0206 \times 14 \\ &+ 0.7634 \times 0 - 0.3443 \times 0 + 0.2221 \times 1 \\ &+ 0.4429 \times 1 - 0.1959 \times 1 = - 2.3955 \end{aligned} \]

We can now back-solve for \(\widehat{p}\): the chance such an individual will receive a callback is about \(\frac{e^{-2.3955}}{1 + e^{-2.3955}} = 0.0835.\)

Compute the probability of a callback for an individual with a name commonly inferred to be from a Black male but who otherwise has the same characteristics as the one described in the previous example.


We can complete the same steps for an individual with the same characteristics who is Black, where the only difference in the calculation is that the indicator variable \(\texttt{race}_{\texttt{White}}\) will take a value of 0. Doing so yields a probability of 0.0553. Let’s compare the results with those of the previous example.

In practical terms, an individual perceived as White based on their first name would need to apply to \(\frac{1}{0.0835} \approx 12\) jobs on average to receive a callback, while an individual perceived as Black based on their first name would need to apply to \(\frac{1}{0.0553} \approx 18\) jobs on average to receive a callback. That is, applicants who are perceived as Black need to apply to 50% more employers to receive a callback than someone who is perceived as White based on their first name for jobs like those in the study.

What we have quantified in the current section is alarming and disturbing. However, one aspect that makes the racism so difficult to address is that the experiment, as well-designed as it is, cannot send us much signal about which employers are discriminating. It is only possible to say that discrimination is happening, even if we cannot say which particular callbacks — or non-callbacks — represent discrimination. Finding strong evidence of racism for individual cases is a persistent challenge in enforcing anti-discrimination laws.

9.4 Groups of different sizes

Any form of discrimination is concerning, which is why we decided it was so important to discuss the topic using data. The resume study also only examined discrimination in a single aspect: whether a prospective employer would call a candidate who submitted their resume. There was a 50% higher barrier for resumes simply when the candidate had a first name that was perceived to be of a Black individual. It’s unlikely that discrimination would stop there.

Let’s consider a sex-imbalanced company that consists of 20% women and 80% men, and we’ll suppose that the company is very large, consisting of perhaps 20,000 employees. (A more deliberate example would include more inclusive gender identities.) Suppose when someone goes up for promotion at the company, 5 of their colleagues are randomly chosen to provide feedback on their work.

Now let’s imagine that 10% of the people in the company are prejudiced against the other sex. That is, 10% of men are prejudiced against women, and similarly, 10% of women are prejudiced against men. Who is discriminated against more at the company, men or women?


Let’s suppose we took 100 men who have gone up for promotion in the past few years. For these men, \(5 \times 100 = 500\) random colleagues will be tapped for their feedback, of which about 20% will be women (100 women). Of these 100 women, 10 are expected to be biased against the man they are reviewing. Then, of the 500 colleagues reviewing them, men will experience discrimination by about 2% of their colleagues when they go up for promotion.

Let’s do a similar calculation for 100 women who have gone up for promotion in the last few years. They will also have 500 random colleagues providing feedback, of which about 400 (80%) will be men. Of these 400 men, about 40 (10%) hold a bias against women. Of the 500 colleagues providing feedback on the promotion packet for these women, 8% of the colleagues hold a bias against the women.

The example highlights something profound: even in a hypothetical setting where each demographic has the same degree of prejudice against the other demographic, the smaller group experiences the negative effects more frequently. Additionally, if we would complete a handful of examples like the one above with different numbers, we would learn that the greater the imbalance in the population groups, the more the smaller group is disproportionately impacted.2

Of course, there are other considerable real-world omissions from the hypothetical example. For example, studies have found instances where people from an oppressed group also discriminate against others within their own oppressed group. As another example, there are also instances where a majority group can be oppressed, with apartheid in South Africa being one such historic example. Ultimately, discrimination is complex, and there are many factors at play beyond the mathematics property we observed in the previous example.

We close the chapter on the serious topic of discrimination, and we hope it inspires you to think about the power of reasoning with data. Whether it is with a formal statistical model or by using critical thinking skills to structure a problem, we hope the ideas you have learned will help you do more and do better in life.

9.5 Chapter review

9.5.1 Summary

Logistic and linear regression models have many similarities. The strongest of which is the linear combination of the explanatory variables which is used to form predictions related to the response variable. However, with logistic regression, the response variable is binary and therefore a prediction is given on the probability of a successful event. Logistic model fit and variable selection can be carried out in similar ways as multiple linear regression.

9.5.2 Terms

The terms introduced in this chapter are presented in Table 9.7. 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 9.7: Terms introduced in this chapter.
AIC logistic regression transformation
Akaike information criterion logit transformation
generalized linear model probability of an event

9.6 Exercises

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

  1. True / False. Determine which of the following statements are true and false. For each statement that is false, explain why it is false.

    1. In logistic regression we fit a line to model the relationship between the predictor(s) and the binary outcome.

    2. In logistic regression, we expect the residuals to be even scattered on either side of zero, just like with linear regression.

    3. In logistic regression, the outcome variable is binary but the predictor variable(s) can be either binary or continuous.

  1. Logistic 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. Suppose we consider the first two observations based on a logistic regression model, where the first variable in observation 1 takes a value of \(x_1 = 6\) and observation 2 has \(x_1 = 4\). Suppose we realized we made an error for these two observations, and the first observation was actually \(x_1 = 7\) (instead of 6) and the second observation actually had \(x_1 = 5\) (instead of 4). Then the predicted probability from the logistic regression model would increase the same amount for each observation after we correct these variables.

    2. When using a logistic regression model, it is impossible for the model to predict a probability that is negative or a probability that is greater than 1.

    3. Because logistic regression predicts probabilities of outcomes, observations used to build a logistic regression model need not be independent.

    4. When fitting logistic regression, we typically complete model selection using adjusted \(R^2\).

  1. Possum classification, comparing models. The common brushtail possum of the Australia region is a bit cuter than its distant cousin, the American opossum (see Figure 7.4). We consider 104 brushtail possums from two regions in Australia, where the possums may be considered a random sample from the population. The first region is Victoria, which is in the eastern half of Australia and traverses the southern coast. The second region consists of New South Wales and Queensland, which make up eastern and northeastern Australia.3

    We use logistic regression to differentiate between possums in these two regions. The outcome variable, called pop, takes value 1 when a possum is from Victoria and 0 when it is from New South Wales or Queensland. We consider five predictors: sex (an indicator for a possum being male), head_l (head length), skull_w (skull width), total_l (total length), and tail_l (tail length). Each variable is summarized in a histogram. The full logistic regression model and a reduced model after variable selection are summarized in the tables below.

    term estimate std.error statistic p.value
    (Intercept) 39.23 11.54 3.40 7e-04
    sexmale -1.24 0.67 -1.86 0.0632
    head_l -0.16 0.14 -1.16 0.248
    skull_w -0.20 0.13 -1.52 0.1294
    total_l 0.65 0.15 4.24 <0.0001
    tail_l -1.87 0.37 -5.00 <0.0001
    term estimate std.error statistic p.value
    (Intercept) 33.51 9.91 3.38 7e-04
    sexmale -1.42 0.65 -2.20 0.0278
    skull_w -0.28 0.12 -2.27 0.0231
    total_l 0.57 0.13 4.30 <0.0001
    tail_l -1.81 0.36 -5.02 <0.0001
    1. Examine each of the predictors given by the individual graphs. Are there any outliers that are likely to have a very large influence on the logistic regression model?

    2. Two models are provided above for predicting the region of the possum. (In Chapter 26 we will cover a method for deciding between the models based on p-values.) The first model includes head_l and the second model does not. Explain why the remaining estimates (model coefficients) change between the two models.

  1. Challenger disaster and model building. On January 28, 1986, a routine launch was anticipated for the Challenger space shuttle. Seventy-three seconds into the flight, disaster happened: the shuttle broke apart, killing all seven crew members on board. An investigation into the cause of the disaster focused on a critical seal called an O-ring, and it is believed that damage to these O-rings during a shuttle launch may be related to the ambient temperature during the launch. The table below summarizes observational data on O-rings for 23 shuttle missions, where the mission order is based on the temperature at the time of the launch. temperature gives the temperature in Fahrenheit, damaged represents the number of damaged O-rings, and undamaged represents the number of O-rings that were not damaged.4

    mission 1 2 3 4 5 6 7 8 9 10 11 12
    temperature 53 57 58 63 66 67 67 67 68 69 70 70
    damaged 5 1 1 1 0 0 0 0 0 0 1 0
    undamaged 1 5 5 5 6 6 6 6 6 6 5 6
    mission 13 14 15 16 17 18 19 20 21 22 23
    temperature 70 70 72 73 75 75 76 76 78 79 81
    damaged 1 0 0 0 0 1 0 0 0 0 0
    undamaged 5 6 6 6 6 5 6 6 6 6 6
    term estimate std.error statistic p.value
    (Intercept) 11.66 3.30 3.54 4e-04
    temperature -0.22 0.05 -4.07 <0.0001
    1. Each column of the table above represents a different shuttle mission. Examine these data and describe what you observe with respect to the relationship between temperatures and damaged O-rings.

    2. Failures have been coded as 1 for a damaged O-ring and 0 for an undamaged O-ring, and a logistic regression model was fit to these data. The regression output for this model is given above. Describe the key components of the output in words.

    3. Write out the logistic model using the point estimates of the model parameters.

    4. Based on the model, do you think concerns regarding O-rings are justified? Explain.

  1. Possum classification, prediction. A logistic regression model was proposed for classifying common brushtail possums into their two regions. The outcome variable took value 1 if the possum was from Victoria and 0 otherwise.

    term estimate std.error statistic p.value
    (Intercept) 33.51 9.91 3.38 7e-04
    sexmale -1.42 0.65 -2.20 0.0278
    skull_w -0.28 0.12 -2.27 0.0231
    total_l 0.57 0.13 4.30 <0.0001
    tail_l -1.81 0.36 -5.02 <0.0001
    1. Write out the form of the model. Also identify which of the variables are positively associated with the outcome of living in Victoria, when controlling for other variables.

    2. Suppose we see a brushtail possum at a zoo in the US, and a sign says the possum had been captured in the wild in Australia, but it doesn’t say which part of Australia. However, the sign does indicate that the possum is male, its skull is about 63 mm wide, its tail is 37 cm long, and its total length is 83 cm. What is the reduced model’s computed probability that this possum is from Victoria? How confident are you in the model’s accuracy of this probability calculation?

  1. Challenger disaster and prediction. On January 28, 1986, a routine launch was anticipated for the Challenger space shuttle. Seventy-three seconds into the flight, disaster happened: the shuttle broke apart, killing all seven crew members on board. An investigation into the cause of the disaster focused on a critical seal called an O-ring, and it is believed that damage to these O-rings during a shuttle launch may be related to the ambient temperature during the launch. The investigation found that the ambient temperature at the time of the shuttle launch was closely related to the damage of O-rings, which are a critical component of the shuttle.

    1. The data provided in the previous exercise are shown in the plot. The logistic model fit to these data may be written as

    \[\log\left( \frac{\hat{p}}{1 - \hat{p}} \right) = 11.6630 - 0.2162\times \texttt{temperature}\]

    where \(\hat{p}\) is the model-estimated probability that an O-ring will become damaged. Use the model to calculate the probability that an O-ring will become damaged at each of the following ambient temperatures: 51, 53, and 55 degrees Fahrenheit. The model-estimated probabilities for several additional ambient temperatures are provided below, where subscripts indicate the temperature:

    \[ \begin{aligned} &\hat{p}_{57} = 0.341 && \hat{p}_{59} = 0.251 && \hat{p}_{61} = 0.179 && \hat{p}_{63} = 0.124 \\ &\hat{p}_{65} = 0.084 && \hat{p}_{67} = 0.056 && \hat{p}_{69} = 0.037 && \hat{p}_{71} = 0.024 \end{aligned} \]

    1. Add the model-estimated probabilities from part (a) on the plot, then connect these dots using a smooth curve to represent the model-estimated probabilities.

    2. Describe any concerns you may have regarding applying logistic regression in this application, and note any assumptions that are required to accept the model’s validity.

  1. Spam filtering, model selection. Spam filters are built on principles similar to those used in logistic regression. Using characteristics of individual emails, we fit a probability that each message is spam or not spam. We have several email variables for this problem, and we won’t describe what each variable means here for the sake of brevity, but each is either a numerical or indicator variable.5

    term estimate std.error statistic p.value
    (Intercept) -0.69 0.09 -7.42 <0.0001
    to_multiple1 -2.82 0.31 -9.05 <0.0001
    cc 0.03 0.02 1.41 0.1585
    attach 0.28 0.08 3.44 6e-04
    dollar -0.08 0.02 -3.45 6e-04
    winneryes 1.72 0.34 5.09 <0.0001
    inherit 0.32 0.15 2.10 0.0355
    password -0.79 0.30 -2.64 0.0083
    format1 -1.50 0.13 -12.01 <0.0001
    re_subj1 -1.92 0.38 -5.10 <0.0001
    exclaim_subj 0.26 0.23 1.14 0.2531
    sent_email1 -16.67 293.19 -0.06 0.9547

    The AIC of the full model is 1863.5. We remove each variable one by one, refit the model, and record the updated AIC.

    1. For variable selection, we fit the full model, which includes all variables, and then we also fit each model where we’ve dropped exactly one of the variables. In each of these reduced models, the AIC value for the model is reported below. Based on these results, which variable, if any, should we drop as part of model selection? Explain.

     

    • None Dropped: 1863.5
    • Drop to_multiple: 2023.5
    • Drop cc: 1863.2
    • Drop attach: 1871.9
    • Drop dollar: 1879.7
    • Drop winner: 1885
    • Drop inherit: 1865.5
    • Drop password: 1879.3
    • Drop format: 2008.9
    • Drop re_subj: 1904.6
    • Drop exclaim_subj: 1862.8
    • Drop sent_email: 1958.2
    1. Consider the subsequent model selection stage (where the variable from part (a) has been removed, and we are considering removal of a second variable). Here again we’ve computed the AIC for each leave-one-variable-out model. Based on the results, which variable, if any, should we drop as part of model selection? Explain.

     

    • None dropped: 1862.8
    • Drop to_multiple: 2021.5
    • Drop cc: 1862.4
    • Drop attach: 1871.2
    • Drop dollar: 1877.8
    • Drop winner: 1885.2
    • Drop inherit: 1864.8
    • Drop password: 1878.4
    • Drop format: 2007
    • Drop re_subj: 1904.3
    • Drop sent_email: 1957.3
    1. Consider one more step in the process. Here again we’ve computed the AIC for each leave-one-variable-out model. Based on the results, which variable, if any, should we drop as part of model selection? Explain.

     

    • None Dropped: 1862.4
    • Drop to_multiple: 2019.6
    • Drop attach: 1871.2
    • Drop dollar: 1877.7
    • Drop winner: 1885
    • Drop inherit: 1864.5
    • Drop password: 1878.2
    • Drop format: 2007.4
    • Drop re_subj: 1902.9
    • Drop sent_email: 1957.6
  1. Spam filtering, prediction. Recall running a logistic regression to aid in spam classification for individual emails. In this exercise, we’ve taken a small set of the variables and fit a logistic model with the following output:

    term estimate std.error statistic p.value
    (Intercept) -0.81 0.09 -9.34 <0.0001
    to_multiple1 -2.64 0.30 -8.68 <0.0001
    winneryes 1.63 0.32 5.11 <0.0001
    format1 -1.59 0.12 -13.28 <0.0001
    re_subj1 -3.05 0.36 -8.40 <0.0001
    1. Write down the model using the coefficients from the model fit.

    2. Suppose we have an observation where \(\texttt{to\_multiple} = 0\), \(\texttt{winner}= 1\), \(\texttt{format} = 0\), and \(\texttt{re\_subj} = 0\). What is the predicted probability that this message is spam?

    3. Put yourself in the shoes of a data scientist working on a spam filter. For a given message, how high must the probability a message is spam be before you think it would be reasonable to put it in a spambox (which the user is unlikely to check)? What tradeoffs might you consider? Any ideas about how you might make your spam-filtering system even better from the perspective of someone using your email service?

  1. Possum classification, model selection via AIC. A logistic regression model was proposed for classifying common brushtail possums into their two regions. The outcome variable took value 1 if the possum was from Victoria and 0 otherwise.

    We use logistic regression to classify the 104 possums in our dataset in these two regions. The outcome variable, called pop, takes value 1 when the possum is from Victoria and 0 when it is from New South Wales or Queensland. We consider five predictors: sex (an indicator for a possum being male), head_l (head length), skull_w (skull width), total_l (total length), and tail_l (tail length).

    A summary of the three models we fit and their AIC values are given below:

    formula AIC
    sex + head_l + skull_w + total_l + tail_l 84.2
    sex + skull_w + total_l + tail_l 83.5
    sex + head_l + total_l + tail_l 84.7
    1. Using the AIC metric, which of the three models would be best to report?

    2. If, for example, the AIC is virtually equivalent for two models that have differing numbers of variables, which model would be prefered: the model with more variables or the model with fewer variables? Explain.

  1. Model selection. An important aspect of building a logistic regression model is figuring out which variables to include in the model. In Chapter 9 we covered using AIC to choose between variable subsets. In Chapter 26 we will cover using something called p-values to choose between variables subsets. Alternatively, you might hope that a model gave the smallest number of false positives, the smallest number of false negatives, or the highest overall accuracy. If different criteria produce outcomes of different variable subsets for the final model, how might you decide which model to put forward? (Hint: There is no single correct answer to this question.)

  1. We did omit discussion of some structure in the data for the analysis presented: the experiment design included blocking, where typically four resumes were sent to each job: one for each inferred race/sex combination (as inferred based on the first name). We did not worry about the blocking aspect, since accounting for the blocking would reduce the standard error without notably changing the point estimates for the race and sex variables versus the analysis performed in the section. That is, the most interesting conclusions in the study are unaffected even when completing a more sophisticated analysis.↩︎

  2. If a proportion \(p\) of a company are women and the rest of the company consists of men, then under the hypothetical situation the ratio of rates of discrimination against women versus men would be given by \((1 - p) / p,\) a ratio that is always greater than 1 when \(p < 0.5\).↩︎

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

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

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

中文

9  逻辑回归

在本章中,我们介绍 逻辑回归 ,它是一种在响应变量为具有两个水平的分类变量(例如,是和否)时构建模型的工具。Logistic回归是一种 广义线性模型(GLM) ,适用于普通多元回归效果不佳的响应变量。GLM可以被视为一种两阶段建模方法。我们首先使用一个概率分布(如二项分布或泊松分布)对响应变量建模。其次,我们使用一组预测变量和一种特殊形式的多元回归来对该分布的参数建模。最终,GLM的应用感觉上与多元回归非常相似,即使某些细节有所不同。

9.1 招聘中的歧视

我们将考虑一项研究的实验数据,该研究旨在了解种族和性别对求职申请回复率的影响 (Bertrand and Mullainathan 2003)。为了评估哪些因素是重要的,该研究在波士顿和芝加哥确定了招聘信息,研究人员制作了许多虚假简历投递到这些职位,以观察哪些简历会获得回复。1 研究人员列举了重要的特征,如工作年限和教育背景,并使用这些特征随机生成虚假简历。最后,他们为每份简历随机分配一个名字,名字会暗示申请人的性别和种族。

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

实验中使用并随机分配的名字经过挑选,使其主要被识别为属于黑人或白人;该研究未考虑其他种族。虽然没有任何名字会被明确推断为属于黑人或白人,但研究人员进行了一项调查以检验名字的种族关联;未通过调查检验的名字被排除在实验之外。你可以在 表 9.1中找到通过了调查检验并最终用于研究的完整名字集合。例如,Lakisha是他们的调查表明会被解读为黑人女性的名字,而Greg则是一个通常会被解读为与白人男性相关的名字。

表9.1:全部36个独特名字的列表,以及与这些名字相关的常见推断种族和性别。
first_name race sex first_name race sex first_name race sex
Aisha 黑人 女性 Hakim 黑人 男性 Laurie 白人 女性
Allison 白人 女性 Jamal 黑人 男性 Leroy 黑人 男性
Anne 白人 女性 Jay 白人 男性 Matthew 白人 男性
布拉德 白人 男性 杰梅因 黑人 男性 梅雷迪思 白人 女性
布伦丹 白人 男性 吉尔 白人 女性 尼尔 白人 男性
布雷特 白人 男性 卡里姆 黑人 男性 拉希德 黑人 男性
凯莉 白人 女性 凯莎 黑人 女性 莎拉 白人 女性
达内尔 黑人 男性 肯尼亚 黑人 女性 塔米卡 黑人 女性
埃博妮 黑人 女性 克里斯汀 白人 女性 坦尼莎 黑人 女性
艾米丽 白人 女性 拉基莎 黑人 女性 托德 白人 男性
杰弗里 白人 男性 拉托尼娅 黑人 女性 特雷梅恩 黑人 男性
格雷格 白人 男性 拉托娅 黑人 女性 蒂龙 黑人 男性

我们感兴趣的结果变量是雇主是否对申请者进行了回复(回电),其中有 8 个属性是随机分配的,我们将予以考虑,并特别关注种族和性别变量。种族和性别在美国属于受保护类别,即它们不是法律上允许用于招聘或雇佣决策的因素。所考虑的全部属性见 表 26.1.

表 9.2:来自 resume 数据集的变量及其描述。其中许多变量是指示变量,即如果存在指定特征则取值为 1,否则为 0。
变量 description
received_callback 指定雇主在收到求职申请后是否给申请者打了电话。
job_city 工作所在城市:波士顿或芝加哥。
college_degree 简历中是否列出大学学位的指示变量。
years_experience 简历中列出的工作年限。
honors 简历中是否列出某种荣誉的指示变量,例如月度最佳员工。
military 简历中是否列出任何军事经历的指示变量。
has_email_address 简历中是否列出求职者电子邮件地址的指示变量。
race 求职者的种族,由简历中列出的名字所暗示。
sex 求职者的性别(仅限男性和女性),由简历中列出的名字所暗示。

每份简历上列出的所有属性都是随机分配的,这意味着任何可能有利于或不利于就业的属性都不会在这些简历上偏向某个人口群体。重要的是,由于该研究具有实验性质,如果发现显著差异,我们可以推断这些变量与回电率之间的因果关系。我们的分析将使我们能够比较每个变量相对于其他变量的实际重要性。

9.2 对事件概率建模

逻辑回归是一种广义线性模型,其结果变量是一个二分类别变量。结果变量 \(Y_i\)取值为 1(在我们的应用中,结果表示简历获得回电)的概率为 \(p_i\) ,取值为 0 的概率为 \(1 - p_i\)。由于每个观测的情境略有不同,例如不同的教育水平或不同的工作年限,概率 \(p_i\) 对每个观测都会有所不同。归根结底,我们建模的是结果取值为 1(即“成功”)的 概率 与预测变量之间的关系:我们将考察哪些简历特征对应更高或更低的回电率。

逻辑回归模型的记号。

广义线性模型(GLM)的结果变量记为 \(Y_i\),其中下标 \(i\) 用于表示第 \(i\)个观测。在简历应用中, \(Y_i\) 用于表示简历 \(i\) 是否获得回电(\(Y_i=1\))或未获得(\(Y_i=0\)).

预测变量表示如下: \(x_{1,i}\) 是观测中变量 1 的取值 \(i\), \(x_{2,i}\) 是观测中变量 2 的取值 \(i\),依此类推。

\[ transformation(p_i) = \beta_0 + \beta_1 x_{1,i} + \beta_2 x_{2,i} + \cdots + \beta_k x_{k,i} \]

我们想要选择一个 变换 使其在实际和数学意义上都合理。例如,我们希望该变换能使方程左边可能的取值范围等于右边可能的取值范围;如果方程中没有变换,左边的取值只能在 0 到 1 之间,而右边的取值则可能远远超出 0 到 1 的范围。

一种常见的 \(p_i\) 是该假设关系中的 logit变换logit 变换

\[ logit(p_i) = \log_{e}\left( \frac{p_i}{1-p_i} \right) \]

logit变换 is shown in 图 9.1logit 变换 \(Y_i\) 。下面,我们使用 \(p_i\):

\[ \log_{e}\left( \frac{p_i}{1-p_i} \right) = \beta_0 + \beta_1 x_{1,i} + \beta_2 x_{2,i} + \cdots + \beta_k x_{k,i} \]

A scatterplot with a superimposed line connecting values of p from -5 to +6 (on the x-axis) with their logit transformed value (on the y-axis).
的 logit 变换,将 \(p_i\) 与其预测变量的关系方程重写为 \(logit(p_i)\).

图 9.1: \(k = 8\)的取值与

的取值的关系图。在我们的简历示例中,有 8 个预测变量,因此 p = 9。虽然 logit 函数的具体选择并不直观,但它基于支撑广义线性模型的理论,这超出了本书的范围。幸运的是,一旦我们使用软件拟合了模型,即使系数的解释更为复杂,也会让我们感觉回到了多元回归的情境中。 \(p_i\):

\[ \begin{aligned} \log_{e}\left( \frac{p_i}{1-p_i} \right) &= \beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i} \\ \frac{p_i}{1-p_i} &= e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}} \\ p_i &= \left( 1 - p_i \right) e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}} \\ p_i &= e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}} - p_i \times e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}} \\ p_i + p_i \text{ } e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}} &= e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}} \\ p_i(1 + e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}}) &= e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}} \\ p_i &= \frac{e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}}}{1 + e^{\beta_0 + \beta_1 x_{1,i} + \cdots + \beta_k x_{k,i}}} \end{aligned} \]

与大多数应用型数据问题一样,我们代入点估计值(观测到的 \(b_i\))来计算相关概率。

我们首先用一个单一预测变量拟合模型: honors。该变量表示申请人的简历上是否列有任何类型的荣誉,例如月度最佳员工。使用统计软件拟合了一个逻辑回归模型,得到如下模型:

\[\log_e \left( \frac{\widehat{p}_i}{1-\widehat{p}_i} \right) = -2.4998 + 0.8668 \times {\texttt{honors}}\]

  1. 如果从研究中随机抽取一份简历,且其未列出任何荣誉,那么它获得回复的概率是多少?

  2. 如果该简历列出了一些荣誉,概率又是多少?


  1. 如果考虑从发出的简历中随机选择一份,且其未列出荣誉,则 honors 取值为 0,模型等式右边等于 -2.4998。求解 \(p_i\): \(\frac{e^{-2.4998}}{1 + e^{-2.4998}} = 0.076\)。正如我们在单变量和多元回归中用“帽子”标记拟合值 \(y_i\) 一样,我们对这个概率也做同样的处理: \(\hat{p}_i = 0.076{}\).

  2. 如果简历列出了一些荣誉,那么模型等式右边为 \(-2.4998 + 0.8668 \times 1 = -1.6330\),对应的概率为 \(\hat{p}_i = 0.163\)。注意,在正式计算数值之前,我们可以通过查看 图 9.1 中的 -2.4998 和 -1.6330 来估计概率。

虽然知道简历是否列出荣誉在预测雇主是否会回复时提供了一定的信号,但我们希望同时考虑许多不同的变量,以了解每项简历特征如何影响获得回复的机会。

9.3 包含多个变量的逻辑模型

我们使用统计软件对第 表 26.1节中描述的全部 8 个预测变量拟合了逻辑回归模型。与多元回归一样,结果可以用汇总表呈现,如表 表 9.3.

表 9.3:简历回电示例的完整逻辑回归模型汇总表。
term 估计 的线性模型摘要 统计量 std.error
p.value -2.66 0.18 -14.64 <0.0001
job_cityChicago -0.44 0.11 -3.85 1e-04
college_degree1 -0.07 0.12 -0.55 0.5821
years_experience 0.02 0.01 1.96 0.0503
honors1 0.77 0.19 4.14 <0.0001
military1 -0.34 0.22 -1.59 0.1127
has_email_address1 0.22 0.11 1.93 0.0541
raceWhite 0.44 0.11 4.10 <0.0001
sexman -0.18 0.14 -1.32 0.1863

与多元回归一样,我们可以从模型中剔除一些变量。这里我们将使用一个称为 赤池信息准则(AIC)的统计量,其作用类似于多元回归中调整 \(R^2\) 的用法。AIC 是一种在许多学科中广泛使用的流行模型选择方法,因其强调模型的不确定性和简约性而备受推崇。AIC 通过根据各模型的 AIC 值从优到劣进行排序来选出“最佳”模型。在计算模型的 AIC 时,每增加一个变量都会受到一定的惩罚。这种对模型复杂度增加的惩罚旨在在欠拟合(模型中变量过少)和过拟合(模型中变量过多)之间取得平衡。使用 AIC 进行模型选择时,AIC 值较低的模型被认为是“更好”的。请记住,在使用调整 \(R^2\) 我们改为选择数值较高的模型。需要注意的是,AIC 提供的是一个模型相对于其他模型的质量信息,但并不能提供模型整体质量的信息。

表 9.4 给出了 AIC 以及用于拟合模型的观测数。我们还从 表 9.3 中得知,共拟合了八个变量(包括截距在内共九个系数)。

表 9.4:拟合完整简历回访示例的完整逻辑回归模型的 AIC。
AIC AIC
2677 4870

我们将使用向后剔除策略来寻找 AIC 较低的模型。 表 9.5 给出了变量如 表 9.6中所示的模型的 AIC 值。注意,使用的观测数相同,但少了一个变量(从模型中剔除了college_degree )。

表 9.5:不含 college_degree.
AIC AIC
2676 4870

number_observations college_degree 使用 AIC 准则后,变量 college_degree 被剔除(不含该变量的 AIC 值小于完整模型的 AIC 值),得到总结于 表 9.6 变量更少,这也是本节剩余部分将采用的形式。

表 9.6:简历回电示例的 logistic 回归模型汇总表,其中已使用 AIC 进行变量选择,且 college_degree 已从模型中剔除。
term 估计 的线性模型摘要 统计量 std.error
p.value -2.72 0.16 -17.51 <0.0001
job_cityChicago -0.44 0.11 -3.83 1e-04
years_experience 0.02 0.01 2.02 0.043
honors1 0.76 0.19 4.12 <0.0001
military1 -0.34 0.22 -1.60 0.1105
has_email_address1 0.22 0.11 1.97 0.0494
raceWhite 0.44 0.11 4.10 <0.0001
sexman -0.20 0.14 -1.45 0.1473

race 变量只取两个水平: BlackWhite。根据模型结果, race 变量的系数对回电决策说明了什么?


所示系数对应于 White的水平,且为正。该正系数反映了当候选人名字暗示其为白人时,简历回电率的正向提升。模型结果表明,潜在雇主更青睐名字通常被解读为白人的简历。

中完整模型中 \(\texttt{race}_{\texttt{White}}\) 的系数与 表 9.3中所示模型的系数几乎相同。实验中的预测变量经过精心设计,使得系数估计通常不会因模型中包含哪些其他预测变量而受到太大影响,这与该研究的动机一致,即梳理出哪些效应对获得回电很重要。在大多数观察性数据中,点估计通常会随着模型中包含哪些其他变量而略有变化,有时甚至变化很大。 表 9.6使用

中总结的模型来估计芝加哥一份工作的回电概率,该工作中候选人列出14年工作经验,无荣誉,无军队经历,包含电子邮件地址,且名字暗示其为白人男性。 表 9.6 我们可以先利用模型中的系数写出方程:


现在我们可以代入目标个体每个变量的对应取值:

\[ \begin{aligned} log_e \left(\frac{\widehat{p}}{1 - \widehat{p}}\right) = -2.7162 &- 0.4364 \times \texttt{job\_city}_{\texttt{Chicago}} + 0.0206 \times \texttt{years\_experience} \\ &+ 0.7634 \times \texttt{honors} - 0.3443 \times \texttt{military} + 0.2221 \times \texttt{email} \\ &+ 0.4429 \times \texttt{race}_{\texttt{White}} - 0.1959 \times \texttt{sex}_{\texttt{man}} \end{aligned} \]

现在我们可以反解出

\[ \begin{aligned} log_e \left(\frac{\widehat{p}}{1 - \widehat{p}}\right) = - 2.7162 &- 0.4364 \times 1 + 0.0206 \times 14 \\ &+ 0.7634 \times 0 - 0.3443 \times 0 + 0.2221 \times 1 \\ &+ 0.4429 \times 1 - 0.1959 \times 1 = - 2.3955 \end{aligned} \]

:这样的个体获得回电的概率约为 \(\widehat{p}\)计算一个名字通常被推断为黑人男性的个体的回电概率,但其其他特征与上一个例子中描述的个体相同。 \(\frac{e^{-2.3955}}{1 + e^{-2.3955}} = 0.0835.\)

我们可以对具有相同特征的黑人个体完成相同的步骤,计算中唯一的区别是指示变量


的取值为0。这样做得到的概率为0.0553。让我们将结果与上一个例子的结果进行比较。 \(\texttt{race}_{\texttt{White}}\) 从实际角度来看,基于名字被认为是有色人种白人的个体平均需要申请

从实际意义看,仅根据名字被认为是白人的求职者平均需要申请 \(\frac{1}{0.0835} \approx 12\) 份工作才能获得一次回电;仅根据名字被认为是黑人的求职者平均需要申请 \(\frac{1}{0.0553} \approx 18\) 份工作才能获得一次回电。也就是说,对于研究涉及的这类职位,被认为是黑人的求职者要获得一次回电,需要申请的雇主数量比被认为是白人的求职者多 50%。

我们在本节中所量化的情况令人震惊和不安。然而,使种族主义如此难以应对的一个方面是,尽管这项实验设计精良,但它无法向我们传递太多关于哪些雇主在实施歧视的信号。我们只能说歧视正在发生,即使我们无法判断哪些具体的回复——或不回复——代表了歧视。在执行反歧视法律时,为个别案例找到种族主义的有力证据始终是一个持续的挑战。

9.4 规模不同的群体

任何形式的歧视都令人担忧,这正是我们决定用数据来讨论这一话题的重要原因。简历研究也只考察了歧视的一个方面:潜在雇主是否会致电提交了简历的候选人。仅仅因为候选人的名字被认为属于黑人,简历就面临高出 50% 的门槛。歧视不太可能止步于此。

让我们考虑一家性别失衡的公司,其中女性占 20%,男性占 80%,并假设这家公司规模很大,拥有大约 20,000 名员工。(一个更周全的例子会包含更多元的性别身份。)假设当有人在公司申请晋升时,会随机选择 5 位同事对其工作提供反馈。

现在假设公司中有 10% 的人对另一性别抱有偏见。也就是说,10% 的男性对女性抱有偏见,同样,10% 的女性对男性抱有偏见。在公司里,男性还是女性受到的歧视更多?


假设我们选取 100 名在过去几年中申请过晋升的男性。对于这些男性, \(5 \times 100 = 500\) 位随机同事将被选中提供反馈,其中约 20% 是女性(100 名女性)。在这 100 名女性中,预计有 10 人对她们所评审的男性抱有偏见。那么,在评审他们的 500 位同事中,男性在申请晋升时会受到约 2% 的同事的歧视。

让我们对过去几年中申请过晋升的 100 名女性做类似的计算。她们同样会有 500 位随机同事提供反馈,其中约 400 位(80%)是男性。在这 400 名男性中,约有 40 人(10%)对女性抱有偏见。在为这些女性的晋升材料提供反馈的 500 位同事中,有 8% 的同事对女性抱有偏见。

这个例子揭示了一个深刻的道理:即使在一个假设情境中,每个人口群体对另一群体的偏见程度相同,规模较小的群体也会更频繁地经历负面影响。此外,如果我们用不同的数字完成几个类似上面的例子,我们会发现人口群体之间的失衡越严重,较小群体受到的影响就越不成比例。2

当然,这个假设例子还遗漏了许多其他重要的现实因素。例如,研究发现,受压迫群体中的人也会歧视自己所属受压迫群体内的其他人。再举一个例子,也存在多数群体受压迫的情况,南非的种族隔离制度就是一个这样的历史例子。归根结底,歧视是复杂的,除了我们在上一个例子中观察到的数学性质之外,还有许多因素在起作用。

我们以歧视这一严肃话题来结束本章,希望它能激励你思考用数据进行推理的力量。无论是通过正式的统计模型,还是运用批判性思维技能来构建问题,我们都希望你所学到的思想能帮助你在生活中做得更多、做得更好。

9.5 本章复习

9.5.1 小结

逻辑回归模型和线性回归模型有许多相似之处。其中最突出的是用解释变量的线性组合来形成与响应变量相关的预测。然而,在逻辑回归中,响应变量是二元的,因此预测给出的是成功事件的概率。逻辑模型的拟合和变量选择可以采用与多元线性回归类似的方式进行。

9.5.2 术语

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

表 9.7:本章介绍的术语。
AIC 逻辑回归 变换
赤池信息准则 logit变换
广义线性模型 事件发生的概率

9.6 练习

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

  1. 当备择假设为真且可辨别性水平为 (i) 0.05 或 (ii) 0.10 时,犯第二类错误的概率。 判断下列陈述的真假。对于每个错误的陈述,解释其错误的原因。

    1. 在逻辑回归中,我们拟合一条直线来建模预测变量与二元结果之间的关系。

    2. 在逻辑回归中,我们期望残差在零的两侧均匀散布,就像线性回归一样。

    3. 在逻辑回归中,结果变量是二元的,但预测变量可以是二元的或连续的。

  1. 逻辑回归事实核查。 判断下列陈述的真假。对于每个错误的陈述,解释其错误的原因。

    1. 假设我们基于一个逻辑回归模型考虑前两个观测值,其中观测值1的第一个变量取值为 \(x_1 = 6\) 而观测值2为 \(x_1 = 4\)。假设我们意识到这两个观测值存在错误,第一个观测值实际上是 \(x_1 = 7\) (而不是6),第二个观测值实际上是 \(x_1 = 5\) (而不是4)。那么在修正这些变量之后,逻辑回归模型对每个观测值的预测概率会增加相同的幅度。

    2. 使用逻辑回归模型时,模型不可能预测出负的概率或大于1的概率。

    3. 由于逻辑回归预测的是结果的概率,用于构建逻辑回归模型的观测值不必相互独立。

    4. 在拟合逻辑回归时,我们通常使用调整后的 \(R^2\).

  1. 负鼠分类,比较模型。 澳大利亚地区的普通帚尾袋貂比它的远亲美洲负鼠要可爱一些(见 图 7.4)。我们考虑来自澳大利亚两个地区的 104 只帚尾袋貂,这些袋貂可以被视为来自该总体的随机样本。第一个地区是维多利亚州,位于澳大利亚东半部并横跨南部海岸。第二个地区由新南威尔士州和昆士兰州组成,构成了澳大利亚的东部和东北部。3

    我们使用逻辑回归来区分这两个地区的袋貂。结果变量称为 pop,当袋貂来自维多利亚州时取值为 1,当它来自新南威尔士州或昆士兰州时取值为 0。我们考虑五个预测变量: sex (负鼠为雄性的指示变量)、 head_l (头长)、 skull_w (颅骨宽度)、 total_l (总长度)和 tail_l (尾长)。每个变量都用直方图进行了汇总。完整的逻辑回归模型以及变量选择后的简化模型汇总在下表中。

    term 估计 的线性模型摘要 统计量 std.error
    p.value 39.23 11.54 3.40 7e-04
    sexmale -1.24 0.67 -1.86 0.0632
    head_l -0.16 0.14 -1.16 0.248
    skull_w -0.20 0.13 -1.52 0.1294
    total_l 0.65 0.15 4.24 <0.0001
    tail_l -1.87 0.37 -5.00 <0.0001
    term 估计 的线性模型摘要 统计量 std.error
    p.value 33.51 9.91 3.38 7e-04
    sexmale -1.42 0.65 -2.20 0.0278
    skull_w -0.28 0.12 -2.27 0.0231
    total_l 0.57 0.13 4.30 <0.0001
    tail_l -1.81 0.36 -5.02 <0.0001
    1. 逐一考察各个单独图形所给出的预测变量。是否存在可能对逻辑回归模型产生极大影响的异常值?

    2. 上面提供了两个用于预测负鼠地区的模型。(在 第26章 中,我们将介绍一种基于p值在模型之间进行选择的方法。)第一个模型包含 head_l ,而第二个模型不包含。请解释为什么其余估计值(模型系数)在两个模型之间会发生变化。

  1. 挑战者号灾难与模型构建。 1986年1月28日,挑战者号航天飞机按计划进行一次例行发射。升空73秒后,灾难发生了:航天飞机解体,机上七名机组人员全部遇难。对灾难原因的调查集中在一个被称为O形环的关键密封件上,据信航天飞机发射期间O形环的损坏可能与发射时的环境温度有关。下表总结了23次航天飞机任务中O形环的观测数据,其中任务顺序按发射时的温度排列。 temperature 给出了以华氏度表示的温度, damaged 表示受损的O形环数量, undamaged 表示未受损的O形环数量。4

    任务 1 2 3 4 5 6 7 8 9 10 11 12
    温度 53 57 58 63 66 67 67 67 68 69 70 70
    受损 5 1 1 1 0 0 0 0 0 0 1 0
    未受损 1 5 5 5 6 6 6 6 6 6 5 6
    任务 13 14 15 16 17 18 19 20 21 22 23
    温度 70 70 72 73 75 75 76 76 78 79 81
    受损 1 0 0 0 0 1 0 0 0 0 0
    未受损 5 6 6 6 6 5 6 6 6 6 6
    term 估计 的线性模型摘要 统计量 std.error
    p.value 11.66 3.30 3.54 4e-04
    温度 -0.22 0.05 -4.07 <0.0001
    1. 上表的每一列代表一次不同的航天飞机任务。请检查这些数据,并描述你所观察到的温度与受损O形圈之间的关系。

    2. 失败已被编码为:O形圈受损记为1,未受损记为0,并对这些数据拟合了一个逻辑回归模型。该模型的回归输出已在上方给出。请用文字描述该输出的关键组成部分。

    3. 使用模型参数的点估计写出该逻辑模型。

    4. 基于该模型,你认为对O形圈的担忧是否有依据?请解释。

  1. 负鼠分类与预测。 我们提出了一个逻辑回归模型,用于将普通帚尾袋鼠分类到它们的两个区域中。结果变量在负鼠来自维多利亚州时取值为 1,否则为 0。

    term 估计 的线性模型摘要 统计量 std.error
    p.value 33.51 9.91 3.38 7e-04
    sexmale -1.42 0.65 -2.20 0.0278
    skull_w -0.28 0.12 -2.27 0.0231
    total_l 0.57 0.13 4.30 <0.0001
    tail_l -1.81 0.36 -5.02 <0.0001
    1. 写出该模型的形式。同时,在控制其他变量的情况下,指出哪些变量与“生活在维多利亚”这一结果呈正相关。

    2. 假设我们在美国的一家动物园看到一只帚尾袋貂,标牌上写着它是在澳大利亚野外捕获的,但没有说明来自澳大利亚的哪个地区。不过,标牌确实表明这只袋貂是雄性,其颅骨宽约63毫米,尾巴长37厘米,体全长为83厘米。那么简化模型计算出的这只袋貂来自维多利亚的概率是多少?你对模型这一概率计算的准确性有多大信心?

  1. 挑战者号灾难与预测。 1986年1月28日,挑战者号航天飞机原计划进行一次例行发射。发射升空73秒后,灾难发生了:航天飞机解体,机上七名宇航员全部遇难。对灾难原因的调查集中在一个被称为O形环的关键密封部件上,据信航天飞机发射期间O形环的损坏可能与发射时的环境温度有关。调查发现,航天飞机发射时的环境温度与O形环的损坏密切相关,而O形环是航天飞机的关键部件。

    1. 前一练习中提供的数据如图所示。拟合这些数据的逻辑回归模型可以写为

    \[\log\left( \frac{\hat{p}}{1 - \hat{p}} \right) = 11.6630 - 0.2162\times \texttt{temperature}\]

    其中 \(\hat{p}\) 是模型估计的O形环损坏概率。使用该模型计算在以下各环境温度下O形环损坏的概率:51、53和55华氏度。若干其他环境温度下的模型估计概率已在下方给出,其中下标表示温度:

    \[ \begin{aligned} &\hat{p}_{57} = 0.341 && \hat{p}_{59} = 0.251 && \hat{p}_{61} = 0.179 && \hat{p}_{63} = 0.124 \\ &\hat{p}_{65} = 0.084 && \hat{p}_{67} = 0.056 && \hat{p}_{69} = 0.037 && \hat{p}_{71} = 0.024 \end{aligned} \]

    1. 将(a)部分得到的模型估计概率添加到图中,然后用平滑曲线连接这些点,以表示模型估计的概率。

    2. 描述你对于在此应用中使用逻辑回归可能存在的任何顾虑,并指出接受该模型有效性所需的任何假设。

  1. 垃圾邮件过滤,模型选择。 垃圾邮件过滤器的构建原理与逻辑回归中使用的原理类似。利用每封电子邮件的特征,我们拟合每条邮件是垃圾邮件或非垃圾邮件的概率。本题中我们有若干电子邮件变量,为简洁起见,这里不描述每个变量的含义,但每个变量要么是数值变量,要么是指示变量。5

    term 估计 的线性模型摘要 统计量 std.error
    p.value -0.69 0.09 -7.42 <0.0001
    to_multiple1 -2.82 0.31 -9.05 <0.0001
    cc 0.03 0.02 1.41 0.1585
    attach 0.28 0.08 3.44 6e-04
    dollar -0.08 0.02 -3.45 6e-04
    winneryes 1.72 0.34 5.09 <0.0001
    inherit 0.32 0.15 2.10 0.0355
    password -0.79 0.30 -2.64 0.0083
    format1 -1.50 0.13 -12.01 <0.0001
    re_subj1 -1.92 0.38 -5.10 <0.0001
    exclaim_subj 0.26 0.23 1.14 0.2531
    sent_email1 -16.67 293.19 -0.06 0.9547

    完整模型的AIC为1863.5。我们逐个移除每个变量,重新拟合模型,并记录更新后的AIC。

    1. 对于变量选择,我们拟合包含所有变量的完整模型,然后还拟合每个恰好去掉其中一个变量的模型。每个简化模型的AIC值报告如下。基于这些结果,我们应该(如果有)去掉哪个变量作为模型选择的一部分?请解释。

     

    • 不去掉任何变量:1863.5
    • 去掉 to_multiple: 2023.5
    • 去掉 cc: 1863.2
    • 去掉 attach: 1871.9
    • 去掉 dollar: 1879.7
    • 去掉 winner: 1885
    • 去掉 inherit: 1865.5
    • 去掉 password: 1879.3
    • 去掉 format: 2008.9
    • 去掉 re_subj: 1904.6
    • 去掉 exclaim_subj: 1862.8
    • 去掉 sent_email: 1958.2
    1. 考虑接下来的模型选择阶段(其中第 (a) 部分中的变量已被剔除,我们正在考虑剔除第二个变量)。这里我们再次计算了每个留一变量模型的 AIC。根据结果,我们应该剔除哪个变量(如果有的话)作为模型选择的一部分?请解释。

     

    • 未剔除任何变量:1862.8
    • 去掉 to_multiple: 2021.5
    • 去掉 cc: 1862.4
    • 去掉 attach: 1871.2
    • 去掉 dollar: 1877.8
    • 去掉 winner: 1885.2
    • 去掉 inherit: 1864.8
    • 去掉 password: 1878.4
    • 去掉 format: 2007
    • 去掉 re_subj: 1904.3
    • 去掉 sent_email: 1957.3
    1. 考虑流程中的再一步。这里我们再次计算了每个留一变量模型的 AIC。根据结果,哪个变量(如果有的话)应作为模型选择的一部分被剔除?请解释。

     

    • 未剔除任何变量:1862.4
    • 去掉 to_multiple: 2019.6
    • 去掉 attach: 1871.2
    • 去掉 dollar: 1877.7
    • 去掉 winner: 1885
    • 去掉 inherit: 1864.5
    • 去掉 password: 1878.2
    • 去掉 format: 2007.4
    • 去掉 re_subj: 1902.9
    • 去掉 sent_email: 1957.6
  1. 垃圾邮件过滤、预测。 回想一下运行逻辑回归以帮助对单个电子邮件进行垃圾邮件分类的过程。在本练习中,我们选取了一小组变量,并拟合了一个逻辑模型,输出如下:

    term 估计 的线性模型摘要 统计量 std.error
    p.value -0.81 0.09 -9.34 <0.0001
    to_multiple1 -2.64 0.30 -8.68 <0.0001
    winneryes 1.63 0.32 5.11 <0.0001
    format1 -1.59 0.12 -13.28 <0.0001
    re_subj1 -3.05 0.36 -8.40 <0.0001
    1. 使用模型拟合得到的系数写出该模型。

    2. 假设我们有一个观测值,其中 \(\texttt{to\_multiple} = 0\), \(\texttt{winner}= 1\), \(\texttt{format} = 0\)\(\texttt{re\_subj} = 0\)。该邮件被预测为垃圾邮件的概率是多少?

    3. 假设你是一名从事垃圾邮件过滤器开发的数据科学家。对于给定的邮件,邮件为垃圾邮件的概率必须达到多高,你才认为将其放入 垃圾邮件箱 (用户不太可能查看)是合理的?你可能需要考虑哪些权衡?从使用你的电子邮件服务的用户的角度来看,关于如何让你的垃圾邮件过滤系统变得更好,你有什么想法?

  1. 负鼠分类、通过 AIC 进行模型选择。 我们提出了一个逻辑回归模型,用于将普通帚尾袋鼠分类到它们的两个区域中。结果变量在负鼠来自维多利亚州时取值为 1,否则为 0。

    我们使用逻辑回归将数据集中的 104 只负鼠分类到这两个区域。结果变量称为 pop,当负鼠来自维多利亚州时取值为 1,当它来自新南威尔士州或昆士兰州时取值为 0。我们考虑五个预测变量: sex (负鼠为雄性的指示变量)、 head_l (头长)、 skull_w (颅骨宽度)、 total_l (总长度)和 tail_l (尾长)。

    我们拟合的三个模型及其 AIC 值的汇总如下:

    公式 AIC
    sex + head_l + skull_w + total_l + tail_l 84.2
    sex + skull_w + total_l + tail_l 83.5
    sex + head_l + total_l + tail_l 84.7
    1. 根据 AIC 指标,这三个模型中哪一个最适合报告?

    2. 例如,如果两个变量数量不同的模型的 AIC 几乎相同,那么应该优先选择哪个模型:变量较多的模型还是变量较少的模型?请解释。

  1. 模型选择。 构建逻辑回归模型的一个重要方面是确定模型中应包含哪些变量。在 第 9 章 中,我们介绍了使用 AIC 在变量子集之间进行选择。在 第26章 中,我们将介绍使用一种称为 p 值的方法在变量子集之间进行选择。或者,你可能希望模型给出最少的假阳性数量、最少的假阴性数量,或最高的总体准确率。如果不同的标准为最终模型产生了 不同的变量子集 结果,你会如何决定提出哪个模型?(提示:这个问题没有唯一正确的答案。)

  1. 在本文所呈现的分析中,我们确实省略了对数据中某些结构的讨论:实验设计包含了区组,即通常向每份工作发送四份简历,每个推断的种族/性别组合各一份(基于名字推断)。我们没有考虑区组这一因素,因为对区组进行校正只会 降低 标准误,而不会显著改变本节所做分析中 racesex 变量的点估计值。也就是说,即使进行更复杂的分析,该研究中最有趣的结论也不会受到影响。↩︎

  2. 如果某公司中比例为 \(p\) 的是女性,其余为男性,那么在假设情形下,针对女性与男性的歧视率之比将由 \((1 - p) / p,\) 给出,当 \(p < 0.5\).↩︎

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

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

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