Chapter page 35 / 3826 Inference for logistic regression
English

26  Inference for logistic regression

Combining ideas from Chapter 9 on logistic regression, Chapter 13 on inference with mathematical models, and Chapter 24 and Chapter 25 which apply inferential techniques to the linear model, we wrap up the book by considering inferential methods applied to a logistic regression model. Additionally, we use cross-validation as a method for independent assessment of the logistic regression model.

As with multiple linear regression, the inference aspect for logistic regression will focus on interpretation of coefficients and relationships between explanatory variables. Both p-values and cross-validation will be used for assessing a logistic regression model.

Consider the email data which describes email characteristics which can be used to predict whether a particular incoming email is spam (unsolicited bulk email). Without reading every incoming message, it might be nice to have an automated way to identify spam emails. Which of the variables describing each email are important for predicting the status of the email?

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

Table 26.1: Variables and their descriptions for the email 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
spam Indicator for whether the email was spam.
to_multiple Indicator for whether the email was addressed to more than one recipient.
from Whether the message was listed as from anyone (this is usually set by default for regular outgoing email).
cc Number of people cc'ed.
sent_email Indicator for whether the sender had been sent an email in the last 30 days.
attach The number of attached files.
dollar The number of times a dollar sign or the word “dollar” appeared in the email.
winner Indicates whether “winner” appeared in the email.
format Indicates whether the email was written using HTML (e.g., may have included bolding or active links).
re_subj Whether the subject started with “Re:”, “RE:”, “re:”, or “rE:”
exclaim_subj Whether there was an exclamation point in the subject.
urgent_subj Whether the word “urgent” was in the email subject.
exclaim_mess The number of exclamation points in the email message.
number Factor variable saying whether there was no number, a small number (under 1 million), or a big number.

26.1 Model diagnostics

Before looking at the hypothesis tests associated with the coefficients (turns out they are very similar to those in linear regression!), it is valuable to understand the technical conditions that underlie the inference applied to the logistic regression model. Generally, as you’ve seen in the logistic regression modeling examples, it is imperative that the response variable is binary. Additionally, the key technical condition for logistic regression has to do with the relationship between the predictor variables (\(x_i\) values) and the probability the outcome will be a success. It turns out, the relationship is a specific functional form called a logit function, where \({\rm logit}(p) = \log_e(\frac{p}{1-p}).\) The function may feel complicated, and memorizing the formula of the logit is not necessary for understanding logistic regression. What you do need to remember is that the probability of the outcome being a success is a function of a linear combination of the explanatory variables.

Logistic regression conditions.

There are two key conditions for fitting a logistic regression model:

  1. Each outcome \(Y_i\) is independent of the other outcomes.
  2. Each predictor \(x_i\) is linearly related to logit\((p_i)\) if all other predictors are held constant.

The first logistic regression model condition — independence of the outcomes — is reasonable if we can assume that the emails that arrive in an inbox within a few months are independent of each other with respect to whether they’re spam or not.

The second condition of the logistic regression model is not easily checked without a fairly sizable amount of data. Luckily, we have 3921 emails in the dataset! Let’s first visualize these data by plotting the true classification of the emails against the model’s fitted probabilities, as shown in Figure 26.1.

Scatterplot with predicted probability of being spam on the x-axis and original class, either spam or not spam, on the y-axis. We can see that the predictions do not perfectly classify the emails; however, the not spam emails generally have lower prediction probabilities than the spam emails.
Figure 26.1: The predicted probability that each of the 3921 emails are spam. Points have been jittered so that those with nearly identical values aren’t plotted exactly on top of one another.

We’d like to assess the quality of the model. For example, we might ask: if we look at emails that we modeled as having 10% chance of being spam, do we find out 10% of them actually are spam? We can check this for groups of the data by constructing a plot as follows:

  1. Bucket the observations into groups based on their predicted probabilities.
  2. Compute the average predicted probability for each group.
  3. Compute the observed probability for each group, along with a 95% confidence interval for the true probability of success for those individuals.
  4. Plot the observed probabilities (with 95% confidence intervals) against the average predicted probabilities for each group.

If the model does a good job describing the data, the plotted points should fall close to the line \(y = x\), since the predicted probabilities should be similar to the observed probabilities. We can use the confidence intervals to roughly gauge whether anything might be amiss. Such a plot is shown in Figure 26.2.

Scatterplot with predicted probability of being spam on the x-axis and original class, either spam or not spam, on the y-axis. Superimposed are the observed probabilities of being spam, calculated by the proportion of spam emails in each bucket of predicted probabilities. The observed probabilities and predicted probabilities line up reasonably well.
Figure 26.2: A reconfiguration of Figure 26.1. Again, the predicted probabilities are on the x-axis and the truth is on the y-axis for each email. After data have been bucketed into predicted probability groups, the proportion of spam emails (i.e., the observed probability) is given by the black circles. The dashed line is within the confidence bound of the 95% confidence intervals for many of the buckets, suggesting the logistic fit is reasonable.

A plot like Figure 26.2 helps to better understand the deviations. Additional diagnostics may be created that are similar to those featured in Section 24.6. For instance, we could compute residuals as the observed outcome minus the expected outcome (\(e_i = Y_i - \hat{p}_i\)), and then we could create plots of these residuals against each predictor.

26.2 Multiple logistic regression output from software

As you learned in Chapter 8, optimization can be used to find the coefficient estimates for the logistic model. The unknown population model can be written as:

\[ \begin{aligned} \log_e\bigg(\frac{p}{1-p}\bigg) = \beta_0 &+ \beta_1 \times \texttt{to\_multiple}\\ &+ \beta_2 \times \texttt{cc} \\ &+ \beta_3 \times \texttt{dollar}\\ &+ \beta_4 \times \texttt{urgent\_subj} \end{aligned} \]

The estimated equation for the regression model may be written as a model with four predictor variables, where \(\hat{p}\) is the estimated probability of being a spam email message:

\[ \begin{aligned} \log_e\bigg(\frac{\hat{p}}{1-\hat{p}}\bigg) = -2.05 &-1.91 \times \texttt{to\_multiple}\\ &+ 0.02 \times \texttt{cc} \\ &- 0.07 \times \texttt{dollar}\\ &+ 2.66 \times \texttt{urgent\_subj} \end{aligned} \]

Table 26.2: Summary of a logistic model for predicting whether an email is spam based on the variables to_multiple, cc, dollar, and urgent_subj. Each of the variables has its own coefficient estimate and p-value.
term estimate std.error statistic p.value
(Intercept) -2.05 0.06 -34.67 <0.0001
to_multiple1 -1.91 0.30 -6.37 <0.0001
cc 0.02 0.02 1.16 0.245
dollar -0.07 0.02 -3.38 7e-04
urgent_subj1 2.66 0.80 3.32 9e-04

Not only does Table 26.2 provide the estimates for the coefficients, it also provides information on the inference analysis (i.e., hypothesis testing) which is the focus of this chapter.

As in Chapter 25, with multiple predictors, each hypothesis test (for each of the explanatory variables) is conditioned on each of the other variables remaining in the model.

if multiple predictors \(H_0: \beta_i = 0\) given other variables in the model

Using the example above and focusing on each of the variable p-values (here we won’t discuss the p-value associated with the intercept), we can write out the four different hypotheses (associated with the p-value corresponding to each of the coefficients / rows in Table 26.2):

  • \(H_0: \beta_1 = 0\) given cc, dollar, and urgent_subj are included in the model
  • \(H_0: \beta_2 = 0\) given to_multiple, dollar, and urgent_subj are included in the model
  • \(H_0: \beta_3 = 0\) given to_multiple, cc, and urgent_subj are included in the model
  • \(H_0: \beta_4 = 0\) given to_multiple, cc, and dollar are included in the model

The very low p-values from the software output tell us that three of the variables (that is, not cc) act as statistically discernible predictors in the model at the discernibility level of 0.05, despite the inclusion of any of the other variables. Consider the p-value on \(H_0: \beta_1 = 0\). The low p-value says that it would be extremely unlikely to observe data that yield a coefficient on to_multiple at least as far from 0 as -1.91 (i.e., \(|b_1| > 1.91\)) if the true relationship between to_multiple and spam was non-existent (i.e., if \(\beta_1 = 0\)) and the model also included cc and dollar and urgent_subj. Note also that the coefficient on dollar has a small associated p-value, but the magnitude of the coefficient is also seemingly small (0.07). It turns out that in units of standard errors (0.02 here), 0.07 is actually quite far from zero, it’s all about context! The p-values on the remaining variables are interpreted similarly. From the initial output (p-values) in Table 26.2, it seems as though to_multiple, dollar, and urgent_subj are important variables for modeling whether an email is spam. We remind you that although p-values provide some information about the importance of each of the predictors in the model, there are many, arguably more important, aspects to consider when choosing the best model.

As with linear regression (see Section 25.2), existence of predictors that are correlated with each other can affect both the coefficient estimates and the associated p-values. However, investigating multicollinearity in a logistic regression model is saved for a text which provides more detail about logistic regression. Next, as a model building alternative (or enhancement) to p-values, we revisit cross-validation within the context of predicting status for each of the individual emails.

26.3 Cross-validation for prediction error

The p-value is a probability measure under a setting of no relationship. That p-value provides information about the degree of the relationship (e.g., above we measure the relationship between spam and to_multiple using a p-value), but the p-value does not measure how well the model will predict the individual emails (e.g., the accuracy of the model). Depending on the goal of the research project, you might be inclined to focus on variable importance (through p-values) or you might be inclined to focus on prediction accuracy (through cross-validation).

Here we present a method for using cross-validation accuracy to determine which variables (if any) should be used in a model which predicts whether an email is spam. A full treatment of cross-validation and logistic regression models is beyond the scope of this text. Using \(k\)-fold cross-validation, we can build \(k\) different models which are used to predict the observations in each of the \(k\) holdout samples. As with linear regression (see Section 25.3), we compare a smaller logistic regression model to a larger logistic regression model. The smaller model uses only the to_multiple variable, see the complete dataset (not cross-validated) model output in Table 26.3. The logistic regression model can be written as follows, where \(\hat{p}\) is the estimated probability of being a spam email message.

The smaller model:

\[\log_e\bigg(\frac{\hat{p}}{1-\hat{p}}\bigg) = -2.12 + -1.81 \times \texttt{to\_multiple}\]

Table 26.3: The smaller model. Summary of a logistic model for predicting whether an email is spam based on only the predictor variable to_multiple. Each of the variables has its own coefficient estimate and p-value.
term estimate std.error statistic p.value
(Intercept) -2.12 0.06 -37.67 <0.0001
to_multiple1 -1.81 0.30 -6.09 <0.0001

For each cross-validated model, the coefficients change slightly, and the model is used to make independent predictions on the holdout sample. The model from the first cross-validation sample is given in Figure 26.3 and can be compared to the coefficients in Table 26.3.

The left panel shows the logistic model predicting the probability of an email being spam as a function of whether the email was sent to multiple individuals; the model was built using the red, green, and yellow triangular sections of the observed data. The right panel shows a confusion matrix of the predicted spam label crossed with the observed spam label for the set of observations in the blue triangular section of the observed data. Of the 83 spam emails, 80 were correctly classified as spam. Of the 897 non-spam emails, 143 were correctly classified as not spam.
Figure 26.3: The smaller model. The coefficients are estimated using the least squares model on 3/4 of the data with a single predictor variable. Predictions are made on the remaining 1/4 of the observations. Note that the prediction error rate is quite high.
Table 26.4: The smaller model. One quarter at a time, the data were removed from the model building, and whether the email was spam (TRUE) or not (FALSE) was predicted. The logistic regression model was fit independently of the removed emails. Only to_multiple is used to predict whether the email is spam. Because we used a cutoff designed to identify spam emails, the accuracy of the non-spam email predictions is very low. spamTP is the proportion of true spam emails that were predicted to be spam. notspamTP is the proportion of true not spam emails that were predicted to be not spam.
fold count accuracy notspamTP spamTP
1st quarter 980 0.26 0.19 0.98
2nd quarter 981 0.23 0.15 0.96
3rd quarter 979 0.25 0.18 0.96
4th quarter 981 0.24 0.17 0.98

Because the email dataset has a ratio of roughly 90% non-spam and 10% spam emails, a model which randomly guessed all non-spam would have an overall accuracy of 90%! Clearly, we would like to capture the information with the spam emails, so our interest is in the percent of spam emails which are identified as spam (see Table 26.4). Additionally, in the logistic regression model, we use a 10% cutoff to predict whether the email is spam. Fortunately, we have done a great job of predicting! However, the trade-off was that most of the non-spam emails are now predicted to be spam which is not acceptable for a prediction algorithm. Adding more variables to the model may help with both the spam and non-spam predictions.

The larger model uses to_multiple, attach, winner, format, re_subj, exclaim_mess, and number as the set of seven predictor variables, see the complete dataset (not cross-validated) model output in Table 26.5. The logistic regression model can be written as follows, where \(\hat{p}\) is the estimated probability of being a spam email message.

The larger model:

\[ \begin{aligned} \log_e\bigg(\frac{\hat{p}}{1-\hat{p}}\bigg) = -0.34 &- 2.56 \times \texttt{to\_multiple} + 0.20 \times \texttt{attach} + 1.73 \times \texttt{winner}_{yes} \\ &- 1.28 \times \texttt{format} - 2.86 \times \texttt{re\_subj} + 0.00 \times \texttt{exclaim\_mess} \\ &- 1.07 \times \texttt{number}_{small} - 0.42 \times \texttt{number}_{big} \end{aligned} \]

Table 26.5: The larger model. Summary of a logistic model for predicting whether an email is spam based on the variables to_multiple, attach, winner, format, re_subj, exclaim_mess, and number. Each of the variables has its own coefficient estimate and p-value.
term estimate std.error statistic p.value
(Intercept) -0.34 0.11 -3.02 0.0025
to_multiple1 -2.56 0.31 -8.28 <0.0001
attach 0.20 0.06 3.29 0.001
winneryes 1.73 0.33 5.33 <0.0001
format1 -1.28 0.13 -9.80 <0.0001
re_subj1 -2.86 0.37 -7.83 <0.0001
exclaim_mess 0.00 0.00 0.26 0.7925
numbersmall -1.07 0.14 -7.54 <0.0001
numberbig -0.42 0.20 -2.10 0.0357
The left panel shows the logistic model predicting the probability of an email being spam as a function of the email being sent to multiple recipients, number of attachments, using the word winner, format of HTML, RE in the subject line, number of exclamation points in the message, and the existence of a number in the email; the model was built using the red, green, and yellow triangular sections of the observed data. The right panel shows a confusion matrix of the predicted spam label crossed with the observed spam label for the set of observations in the blue triangular section of the observed data. Of the 97 spam emails, 73 were correctly classified as spam. Of the 883 non-spam emails, 688 were correctly classified as not spam.
Figure 26.4: The larger model. The coefficients are estimated using the least squares model on 3/4 of the dataset with the seven specified predictor variables. Predictions are made on the remaining 1/4 of the observations. Note that the predictions are independent of the estimated model coefficients. The predictions are now much better for both the spam and the non-spam emails (than they were with a single predictor variable).
Table 26.6: The larger model. One quarter at a time, the data were removed from the model building, and whether the email was spam (TRUE) or not (FALSE) was predicted. The logistic regression model was fit independently of the removed emails. Now, the variables to_multiple, attach, winner, format, re_subj, exclaim_mess, and number are used to predict whether the email is spam. spamTP is the proportion of true spam emails that were predicted to be spam. notspamTP is the proportion of true not spam emails that were predicted to be not spam.’
fold count accuracy notspamTP spamTP
1st quarter 980 0.77 0.77 0.71
2nd quarter 981 0.80 0.81 0.70
3rd quarter 979 0.76 0.77 0.65
4th quarter 981 0.78 0.79 0.75

Somewhat expected, the larger model (see Table 26.6) was able to capture more nuance in the emails which lead to better predictions. However, it is not true that adding variables will always lead to better predictions, as correlated or noise variables may dampen the signal from the set of variables that truly predict the status. We encourage you to learn more about multiple variable models and cross-validation in your future exploration of statistical topics.

26.4 Chapter review

26.4.1 Summary

Throughout the text, we have presented a modern view to introduction to statistics. Earlier, we presented graphical techniques which communicated relationships across multiple variables. We also used modeling to formalize the relationships. In Chapter 26 we considered inferential claims on models which include many variables used to predict the probability of the outcome being a success. We continue to emphasize the importance of experimental design in making conclusions about research claims. In particular, recall that variability can come from different sources (e.g., random sampling vs. random allocation, see Figure 2.8).

As you might guess, this text has only scratched the surface of the world of statistical analyses that can be applied to different datasets. In particular, to do justice to the topic, the linear models and generalized linear models we have introduced can each be covered with their own course or book. Hierarchical models, alternative methods for fitting parameters (e.g., Ridge Regression or LASSO), and advanced computational methods applied to multivariable models (e.g., permuting the response variable? one explanatory variable? all the explanatory variables?) are all beyond the scope of this book. However, your successful understanding of the ideas we have covered has set you up perfectly to move on to a higher level of statistical modeling and inference. Enjoy!

26.4.2 Terms

The terms introduced in this chapter are presented in Table 26.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 26.7: Terms introduced in this chapter.
accuracy inference on logistic regression technical conditions
cross-validation multiple predictors

26.5 Exercises

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

  1. Passing the driver’s test. A consulting company is hired to assess whether certain characteristics of a DMV (e.g., location, number of test takers, number of test givers, hours of operation, etc.) are associated with the pass rate of the driver’s test. The company is given information on 100 randomly selected DMVs across the country. For each DMV the annual driving test pass rate is recorded, along with other characteristics describing the DMV. Can logistic regression with multiple predictors be used to predict annual driving test pass rate for DMVs in this setting? Explain your reasoning.
  1. Oceans and skin cancer. A researcher wants to investigate the relationship between living within 10 miles of an ocean for at least one year of life and developing skin cancer before the age of 50.

    1. Explain why logistic regression can be used to study the relationship between these two binary variables? What is the technical assumption describing the relationship between the response (outcome) and explanatory (predictor) variables?

    2. What other methods covered in this text might be used to address the research question of interest? What advantages does logistic regression have over these methods?

  1. Marijuana use in college. Researchers studying whether the value systems of adolescents conflict with those of their parents asked 445 college students if they use marijuana. They also asked the students’ parents if they used marijuana when they were in college. Based on the regression output shown below for predicting student drug use from parent drug use, evaluate whether parents’ marijuana usage is a discernible predictor of their kids’ marijuana usage. State the hypotheses, the test statistics, the p-value, and the conclusion in context of the data and the research question.1 (Ellis and Stone 1979)

    term estimate std.error statistic p.value
    (Intercept) -0.405 0.133 -3.04 0.0023
    parentsused 0.791 0.194 4.09 <0.0001
  1. Treating heart attacks. Researchers studying the effectiveness of Sulfinpyrazone in the prevention of sudden death after a heart attack conducted a randomized experiment on 1,475 patients. Based on the regression output shown below for predicting the outcome (died or lived, where success is defined as lived) from the treatment group (control and treatment), evaluate whether treatment group is a discernible predictor of the outcome. State the hypotheses, the test statistics, the p-value, and the conclusion in context of the data and the research question.2 (Anturane Reinfarction Trial Research Group 1980)

    term estimate std.error statistic p.value
    (Intercept) 2.431 0.135 18.05 <0.0001
    grouptreatment 0.395 0.210 1.89 0.0594
  1. Possum classification, cross-validation. The common brushtail possum of the Australia region is a bit cuter than its distant cousin, the American opossum. 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. 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.3

    \[ \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 + \beta_1 \times \texttt{tail\_l} \]

     

    \[ \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 + \beta_1 \times \texttt{total\_l} + \beta_2\times \texttt{sex} \]

    1. How many observations are in Fold2? Use the model with only tail length as a predictor variable. Of the observations in Fold2, how many of them were correctly predicted to be from Victoria? How many of them were incorrectly predicted to be from Victoria?

    2. How many observations are used to build the model which predicts for the observations in Fold2?

    3. For one of the cross-validation folds, how many coefficients were estimated for the model which uses tail length as a predictor? For one of the cross-validation folds, how many coefficients were estimated for the model which uses total length and sex as predictors?

  1. Premature babies, cross-validation. 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 1000 births from 2014 (with some rows removed due to missing data). We use logistic regression to model whether the baby is premature from various explanatory variables.4 (ICPSR 2014)

    \[ \begin{aligned} \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 &+ \beta_1\times \texttt{mage} \\ &+ \beta_2 \times \texttt{weight} \\ &+ \beta_3 \times \texttt{mature} \\ &+ \beta_4 \times \texttt{visits} \\ &+ \beta_5 \times \texttt{gained} \\ &+ \beta_6 \times \texttt{habit} \end{aligned} \]

     

    \[ \begin{aligned} \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 &+ \beta_1\times \texttt{weight} \\ &+ \beta_2 \times \texttt{mature} \end{aligned} \]

    1. How many observations are in Fold2? Use the model with only weight and mature as predictor variables. Of the observations in Fold2, how many of them were correctly predicted to be premature? How many of them were incorrectly predicted to be premature?

    2. How many observations are used to build the model which predicts for the observations in Fold2?

    3. In the original dataset, are most of the births premature or full term? Explain.

    4. For one of the cross-validation folds, how many coefficients were estimated for the model which uses mage, weight, mature, visits, gained, and habit as predictors? For one of the cross-validation folds, how many coefficients were estimated for the model which uses weight and mature as predictors?

  1. Possum classification, cross-validation to choose model. In this exercise we consider 104 brushtail possums from two regions in Australia (the first region is Victoria and the second is New South Wales and Queensland), where the possums may be considered a random sample from the population. We use logistic regression to classify the possums into the 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.

    \[ \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 + \beta_1\times \texttt{tail\_l} \]

     

    \[ \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 + \beta_1 \times \texttt{total\_l} + \beta_2 \times \texttt{sex} \]

    1. For the model with tail length, how many of the observations were correctly classified? What proportion of the observations were correctly classified?

    2. For the model with total length and sex, how many of the observations were correctly classified? What proportion of the observations were correctly classified?

    3. If you have to choose between using only tail length as a predictor versus using total length and sex as predictors (for classification into region), which model would you choose? Explain.

    4. Given the predictions above, what third model might be preferable to either of the models above? Explain.

  1. Premature babies, cross-validation to choose model. 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 1000 births from 2014 (with some rows removed due to missing data). We use logistic regression to model whether the baby is premature from various explanatory variables. (ICPSR 2014)

    \[ \begin{aligned} \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 &+ \beta_1 \times \texttt{mage} \\ &+ \beta_2 \times \texttt{weight} \\ &+ \beta_3 \times \texttt{mature} \\ &+ \beta_4 \times \texttt{visits} \\ &+ \beta_5 \times \texttt{gained} \\ &+ \beta_6 \times \texttt{habit} \end{aligned} \]

     

    \[ \begin{aligned} \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 &+ \beta_1\times \texttt{weight} \\ &+ \beta_2 \times \texttt{mature} \end{aligned} \]

    1. For the model with 6 predictors, how many of the observations were correctly classified? What proportion of the observations were correctly classified?

    2. For the model with 2 predictors, how many of the observations were correctly classified? What proportion of the observations were correctly classified?

    3. If you have to choose between the model with 6 predictors and the model with 2 predictors (for predicting whether a baby will be premature), which model would you choose? Explain.


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

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

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

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

中文

26  逻辑回归的推断

结合来自 第 9 的逻辑回归内容、 第 13 的数学模型推断内容,以及 第 24第 25 中将推断技术应用于线性模型的内容,我们在本书最后考虑将推断方法应用于逻辑回归模型。此外,我们使用交叉验证作为对逻辑回归模型进行独立评估的方法。

与多元线性回归一样,逻辑回归的推断部分将重点放在系数的解释以及解释变量之间的关系上。p值和交叉验证都将用于评估逻辑回归模型。

考虑 email 数据集,该数据描述了电子邮件的特征,可用于预测某封收到的邮件是否为垃圾邮件(未经请求的群发邮件)。无需阅读每一封收到的邮件,有一种自动识别垃圾邮件的方法可能会很有用。在描述每封邮件的变量中,哪些对预测邮件的状态很重要?

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

表 26.1: email 数据集的变量及其描述。其中许多变量是指示变量,即如果存在指定特征则取值为 1,否则为 0。
变量 描述
spam 指示该邮件是否为垃圾邮件。
to_multiple 指示该邮件是否发送给多个收件人。
from 邮件是否列出了发件人(对于常规外发邮件,这通常由默认设置)。
cc 被抄送的人数。
sent_email 发件人在过去30天内是否曾发送过电子邮件的指示变量。
attach 附件文件的数量。
dollar 美元符号或“dollar”一词在邮件中出现的次数。
winner 表示“winner”一词是否出现在邮件中。
format 表示邮件是否使用HTML格式编写(例如,可能包含加粗文本或活动链接)。
re_subj 邮件主题是否以“Re:”、“RE:”、“re:”或“rE:”开头。
exclaim_subj 邮件主题中是否包含感叹号。
urgent_subj 邮件主题中是否包含“urgent(紧急)”一词。
exclaim_mess 邮件正文中感叹号的数量。
number 因子变量,表示没有数字、小数字(低于100万)或大数字。

26.1 模型诊断

在查看与系数相关的假设检验(结果发现它们与线性回归中的检验非常相似!)之前,理解逻辑回归模型推断所依赖的技术条件是很有价值的。一般来说,正如你在逻辑回归建模示例中所看到的,响应变量必须是二元的,这一点至关重要。此外,逻辑回归的关键技术条件与预测变量(\(x_i\) 值)与结果为成功的概率之间的关系有关。事实证明,这种关系是一种称为logit函数的特定函数形式,其中 \({\rm logit}(p) = \log_e(\frac{p}{1-p}).\) 这个函数可能感觉比较复杂,而且理解逻辑回归并不需要记住logit的公式。你需要记住的是,结果为成功的概率是解释变量线性组合的函数。

逻辑回归的条件。

拟合逻辑回归模型有两个关键条件:

  1. 每个结果 \(Y_i\) 与其他结果相互独立。
  2. 每个预测变量 \(x_i\) 与logit呈线性关系\((p_i)\) (假设所有其他预测变量保持不变。)

逻辑回归模型的第一个条件——结果相互独立——是合理的,前提是我们可以假设在几个月内到达收件箱的电子邮件在是否为垃圾邮件方面彼此独立。

逻辑回归模型的第二个条件在没有相当大量数据的情况下不易检验。幸运的是,我们的数据集中有 3921 封电子邮件!让我们首先通过将电子邮件的真实分类与模型拟合的概率进行绘图来可视化这些数据,如图所示 图 26.1.

Scatterplot with predicted probability of being spam on the x-axis and original class, either spam or not spam, on the y-axis. We can see that the predictions do not perfectly classify the emails; however, the not spam emails generally have lower prediction probabilities than the spam emails.
图 26.1:3921 封电子邮件中每封为垃圾邮件的预测概率。数据点经过了抖动处理,因此数值几乎相同的点不会完全重叠绘制在一起。

我们想评估模型的质量。例如,我们可能会问:如果我们查看被模型认为有 10% 概率是垃圾邮件的电子邮件,我们是否发现其中实际上有 10% 是垃圾邮件?我们可以通过按如下方式构建图形来对数据分组进行检验:

  1. 根据预测概率将观测值分桶成组。
  2. 计算每组的平均预测概率。
  3. 计算每组的观测概率,以及这些个体真实成功概率的 95% 置信区间。
  4. 将观测概率(带 95% 置信区间)与每组的平均预测概率进行绘图对比。

如果模型能很好地描述数据,绘制的点应该落在直线 \(y = x\)附近,因为预测概率应与观测概率相似。我们可以利用置信区间来粗略判断是否有任何地方可能出了问题。这样的图如图所示 图 26.2.

Scatterplot with predicted probability of being spam on the x-axis and original class, either spam or not spam, on the y-axis. Superimposed are the observed probabilities of being spam, calculated by the proportion of spam emails in each bucket of predicted probabilities. The observed probabilities and predicted probabilities line up reasonably well.
图 26.2: 图 26.1的重新配置。同样,每封电子邮件的预测概率在 x 轴上,真实情况在 y 轴上。在将数据按预测概率分桶成组后,垃圾邮件的比例(即观测概率)由黑色圆圈表示。虚线位于许多分桶的 95% 置信区间内,这表明逻辑回归拟合是合理的。

图 26.2 这样的图有助于更好地理解偏差。可以创建类似于 第 24.6 节. 例如,我们可以将残差计算为观测结果减去预期结果(\(e_i = Y_i - \hat{p}_i\)),然后我们可以绘制这些残差与每个预测变量的关系图。

26.2 软件输出的多元逻辑回归结果

正如你在 第 8 章中学到的,优化可用于求出逻辑模型的系数估计值。未知的总体模型可以写为:

\[ \begin{aligned} \log_e\bigg(\frac{p}{1-p}\bigg) = \beta_0 &+ \beta_1 \times \texttt{to\_multiple}\\ &+ \beta_2 \times \texttt{cc} \\ &+ \beta_3 \times \texttt{dollar}\\ &+ \beta_4 \times \texttt{urgent\_subj} \end{aligned} \]

回归模型的估计方程可以写为一个包含四个预测变量的模型,其中 \(\hat{p}\) 是某封电子邮件为垃圾邮件的估计概率:

\[ \begin{aligned} \log_e\bigg(\frac{\hat{p}}{1-\hat{p}}\bigg) = -2.05 &-1.91 \times \texttt{to\_multiple}\\ &+ 0.02 \times \texttt{cc} \\ &- 0.07 \times \texttt{dollar}\\ &+ 2.66 \times \texttt{urgent\_subj} \end{aligned} \]

表 26.2:基于 to_multiple、cc、dollar 和 urgent_subj 变量预测电子邮件是否为垃圾邮件的逻辑回归模型摘要。每个变量都有自己的系数估计值和 p 值。
term 估计 的线性模型摘要 统计量 std.error
p.value -2.05 0.06 -34.67 <0.0001
to_multiple1 -1.91 0.30 -6.37 <0.0001
cc 0.02 0.02 1.16 0.245
dollar -0.07 0.02 -3.38 7e-04
urgent_subj1 2.66 0.80 3.32 9e-04

不仅 表 26.2 提供了系数的估计值,它还提供了推断分析(即假设检验)的信息,而这正是本章的重点。

第 25 章中所示,当有 多个预测变量时,每个假设检验(针对每个解释变量)都是以模型中保留的其他变量为条件的。

如果有多个预测变量 \(H_0: \beta_i = 0\) 在给定模型中其他变量的条件下

使用上面的例子,并关注每个变量的 p 值(这里我们不讨论与截距相关的 p 值),我们可以写出四个不同的假设(与 表 26.2):

  • \(H_0: \beta_1 = 0\) 包含在模型中的条件下 cc, dollarurgent_subj 在给定
  • \(H_0: \beta_2 = 0\) 包含在模型中的条件下 to_multiple, dollarurgent_subj 在给定
  • \(H_0: \beta_3 = 0\) 包含在模型中的条件下 to_multiple, ccurgent_subj 在给定
  • \(H_0: \beta_4 = 0\) 包含在模型中的条件下 to_multiple, ccdollar 在给定

包含在模型中的条件下 cc) 在 0.05 的可辨别性水平上充当模型中具有统计可辨别性的预测变量,无论是否包含其他任何变量。考虑 \(H_0: \beta_1 = 0\)的 p 值。较低的 p 值表明,如果 to_multiple 之间的真实关系不存在(即,如果 \(|b_1| > 1.91\),那么观察到 to_multiplespam 的系数至少达到 -1.91 那样远离 0(即 \(\beta_1 = 0\)) 而且 )的数据将是极其不可能的。该模型还包含了 ccdollarurgent_subj。另请注意, dollar 的系数具有较小的相关 p 值,但系数的大小似乎也很小(0.07)。事实证明,以标准误为单位(此处为 0.02),0.07 实际上离零相当远,这完全取决于具体情境!其余变量的 p 值可类似地解释。从 表 26.2中的初始输出(p 值)来看, to_multiple, dollarurgent_subj 似乎是对电子邮件是否为 spam进行建模的重要变量。我们提醒您,尽管 p 值提供了关于模型中每个预测变量重要性的某些信息,但在选择最佳模型时还有许多可以说更重要的方面需要考虑。

与线性回归一样(参见 第 25.2 节),彼此相关的预测变量的存在会影响系数估计值和相关的 p 值。然而,关于逻辑回归模型中多重共线性的研究将留给更详细讨论逻辑回归的教材。接下来,作为 p 值的模型构建替代方法(或增强方法),我们在预测每封电子邮件状态的背景下重新审视交叉验证。

26.3 用于预测误差的交叉验证

p 值是在无关系设定下的一种概率度量。该 p 值提供了关于关系程度的信息(例如,上面我们测量了 spamto_multiple 使用 p 值),但 p 值并不能衡量模型对单个电子邮件的预测效果(例如模型的准确率)。根据研究项目的目标,你可能倾向于关注变量重要性(通过 p 值),也可能倾向于关注预测准确率(通过交叉验证)。

在这里我们介绍一种使用 交叉验证 准确率 的方法,用于确定哪些变量(如果有的话)应该用于预测电子邮件是否为垃圾邮件的模型中。对交叉验证和逻辑回归模型的完整讨论超出了本书的范围。使用 \(k\)折交叉验证,我们可以构建 \(k\) 个不同的模型,用于预测每个 \(k\) 留出样本中的观测值。与线性回归一样(参见 第 25.3 节),我们将一个较小的逻辑回归模型与一个较大的逻辑回归模型进行比较。较小的模型仅使用 to_multiple 变量,完整数据集(未经交叉验证)的模型输出见 表 26.3。逻辑回归模型可以写成如下形式,其中 \(\hat{p}\) 是邮件为垃圾邮件的估计概率。

较小的模型:

\[\log_e\bigg(\frac{\hat{p}}{1-\hat{p}}\bigg) = -2.12 + -1.81 \times \texttt{to\_multiple}\]

表 26.3:较小的模型。仅基于预测变量 to_multiple 预测电子邮件是否为垃圾邮件的逻辑模型摘要。每个变量都有自己的系数估计值和 p 值。
term 估计 的线性模型摘要 统计量 std.error
p.value -2.12 0.06 -37.67 <0.0001
to_multiple1 -1.81 0.30 -6.09 <0.0001

对于每个交叉验证模型,系数会略有变化,并且该模型被用于对留出样本进行独立预测。来自第一个交叉验证样本的模型见 图 26.3 ,可与 表 26.3.

The left panel shows the logistic model predicting the probability of an email being spam as a function of whether the email was sent to multiple individuals; the model was built using the red, green, and yellow triangular sections of the observed data. The right panel shows a confusion matrix of the predicted spam label crossed with the observed spam label for the set of observations in the blue triangular section of the observed data. Of the 83 spam emails, 80 were correctly classified as spam. Of the 897 non-spam emails, 143 were correctly classified as not spam.
中的系数进行比较。
图 26.3:较小的模型。系数是使用单一预测变量在 3/4 的数据上通过最小二乘模型估计的。预测是在其余 1/4 的观测上进行的。请注意,预测错误率相当高。 to_multiple 表 26.4:较小的模型。每次移除四分之一的数据用于建模,并预测该邮件是否为垃圾邮件(TRUE)或非垃圾邮件(FALSE)。逻辑回归模型在拟合时独立于被移除的邮件。仅使用 spamTP 是被预测为垃圾邮件的真实垃圾邮件的比例。 notspamTP 是被预测为垃圾邮件的真实垃圾邮件的比例。
fold count 准确率 notspamTP spamTP
第 1 个四分之一 980 0.26 0.19 0.98
第 2 个四分之一 981 0.23 0.15 0.96
第 3 个四分之一 979 0.25 0.18 0.96
第 4 个四分之一 981 0.24 0.17 0.98

因为第二个观测值的Z分数的 email 第4个四分之一 表 26.4)。此外,在逻辑回归模型中,我们使用10%的阈值来预测邮件是否为垃圾邮件。幸运的是,我们的预测做得非常好!然而,代价是大多数非垃圾邮件现在被预测为垃圾邮件,这对于预测算法来说是不可接受的。向模型中添加更多变量可能有助于改善垃圾邮件和非垃圾邮件的预测。

更大的模型使用 to_multiple, attach, winner, format, re_subj, exclaim_messnumber 作为七个预测变量的集合,完整数据集(非交叉验证)的模型输出见 表 26.5。逻辑回归模型可以写成如下形式,其中 \(\hat{p}\) 是邮件为垃圾邮件的估计概率。

更大的模型:

\[ \begin{aligned} \log_e\bigg(\frac{\hat{p}}{1-\hat{p}}\bigg) = -0.34 &- 2.56 \times \texttt{to\_multiple} + 0.20 \times \texttt{attach} + 1.73 \times \texttt{winner}_{yes} \\ &- 1.28 \times \texttt{format} - 2.86 \times \texttt{re\_subj} + 0.00 \times \texttt{exclaim\_mess} \\ &- 1.07 \times \texttt{number}_{small} - 0.42 \times \texttt{number}_{big} \end{aligned} \]

表 26.5:更大的模型。基于变量 to_multiple, attach, winner, format, re_subj, exclaim_messnumber预测邮件是否为垃圾邮件的逻辑回归模型摘要。每个变量都有自己的系数估计值和p值。
term 估计 的线性模型摘要 统计量 std.error
p.value -0.34 0.11 -3.02 0.0025
to_multiple1 -2.56 0.31 -8.28 <0.0001
attach 0.20 0.06 3.29 0.001
winneryes 1.73 0.33 5.33 <0.0001
format1 -1.28 0.13 -9.80 <0.0001
re_subj1 -2.86 0.37 -7.83 <0.0001
exclaim_mess 0.00 0.00 0.26 0.7925
numbersmall -1.07 0.14 -7.54 <0.0001
numberbig -0.42 0.20 -2.10 0.0357
The left panel shows the logistic model predicting the probability of an email being spam as a function of the email being sent to multiple recipients, number of attachments, using the word winner, format of HTML, RE in the subject line, number of exclamation points in the message, and the existence of a number in the email; the model was built using the red, green, and yellow triangular sections of the observed data. The right panel shows a confusion matrix of the predicted spam label crossed with the observed spam label for the set of observations in the blue triangular section of the observed data. Of the 97 spam emails, 73 were correctly classified as spam. Of the 883 non-spam emails, 688 were correctly classified as not spam.
图 26.4:较大的模型。系数是使用最小二乘模型在数据集的 3/4 上、以七个指定的预测变量进行估计的。预测是在其余 1/4 的观测上进行的。请注意,预测与估计的模型系数无关。与使用单个预测变量时相比,现在对垃圾邮件和非垃圾邮件的预测都要好得多。
表 26.6:较大的模型。每次移除四分之一的数据,不参与模型构建,并预测该邮件是垃圾邮件(TRUE)还是不是(FALSE)。逻辑回归模型是在独立于被移除邮件的情况下拟合的。现在,使用变量 to_multiple, attach, winner, format, re_subj, exclaim_messnumber 来预测邮件是否为垃圾邮件。 spamTP 是被预测为垃圾邮件的真实垃圾邮件的比例。 notspamTP 是被预测为非垃圾邮件的真实非垃圾邮件的比例。’
fold count 准确率 notspamTP spamTP
第 1 个四分之一 980 0.77 0.77 0.71
第 2 个四分之一 981 0.80 0.81 0.70
第 3 个四分之一 979 0.76 0.77 0.65
第 4 个四分之一 981 0.78 0.79 0.75

somewhat 正如 somewhat 预期的那样,较大的模型(见 表 26.6)能够捕捉到邮件中更多的细微差别,从而带来更好的预测。然而,增加变量并不总是会带来更好的预测,因为相关变量或噪声变量可能会削弱真正能预测状态的变量集的信号。我们鼓励您在未来的统计学主题探索中进一步学习多变量模型和交叉验证。

26.4 本章复习

26.4.1 小结

在本书中,我们呈现了一种现代的统计学导论视角。前面我们介绍了用于传达多个变量之间关系的图形技术,还使用建模来形式化这些关系。在 第26章 我们讨论了包含许多用于预测结果为成功概率的变量的模型的推断性结论。我们继续强调实验设计在得出研究结论中的重要性。特别是,请回忆变异性可能来自不同的来源(例如,随机抽样与随机分配,参见 图 2.8).

正如你可能猜到的,本书仅仅触及了可应用于不同数据集的统计分析世界的皮毛。特别是,为了充分阐述这一主题,我们介绍的线性模型和广义线性模型各自都可以用一门课程或一本书来涵盖。层次模型、拟合参数的替代方法(例如,岭回归或LASSO),以及应用于多变量模型的高级计算方法(例如,对响应变量置换?对单个解释变量置换?还是对所有解释变量置换?)都超出了本书的范围。然而,你对我们所涵盖思想的成功理解,已经为你完美地进入更高层次的统计建模和推断做好了准备。祝你好运!

26.4.2 术语

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

表 26.7:本章介绍的术语。
准确率 逻辑回归的推断 技术条件
交叉验证 多个预测变量

26.5 练习

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

  1. 通过驾驶考试。 一家咨询公司受聘评估车管所(DMV)的某些特征(例如,位置、考生人数、考官人数、办公时间等)是否与驾驶考试的通过率相关。该公司获得了全国随机抽取的100家车管所的信息。对每家车管所,记录了年度驾驶考试通过率以及描述该车管所的其他特征。在这种情况下,能否使用含多个预测变量的逻辑回归来预测车管所的年度驾驶考试通过率?请解释你的理由。
  1. 海洋与皮肤癌。 一位研究人员想调查一生中至少有一年居住在距海洋10英里以内与50岁之前患皮肤癌之间的关系。

    1. 解释为什么逻辑回归可以用来研究这两个二元变量之间的关系?描述响应(结果)变量与解释(预测)变量之间关系的技术假设是什么?

    2. 本书中涵盖的哪些其他方法可用于解决所关注的研究问题?与这些方法相比,逻辑回归有哪些优势?

  1. 大学生吸食大麻。 研究青少年价值体系是否与其父母价值体系存在冲突的研究人员询问了445名大学生是否使用大麻。他们还询问了这些学生的父母在大学时期是否使用过大麻。根据下方显示的用于通过父母药物使用情况预测学生药物使用情况的回归输出,评估父母的大麻使用情况是否是其子女大麻使用情况的可辨识预测因子。请陈述假设、检验统计量、p值,并结合数据和研究问题给出结论。1 (Ellis and Stone 1979)

    term 估计 的线性模型摘要 统计量 std.error
    p.value -0.405 0.133 -3.04 0.0023
    parentsused 0.791 0.194 4.09 <0.0001
  1. 治疗心脏病发作。 研究磺吡酮(Sulfinpyrazone)在预防心脏病发作后猝死方面有效性的研究人员对1,475名患者进行了一项随机实验。根据下方显示的用于通过治疗组(diedlived,其中成功定义为 lived)预测结局(controltreatment)的回归输出,评估治疗组是否是结局的可辨识预测因子。请陈述假设、检验统计量、p值,并结合数据和研究问题给出结论。2 (Anturane Reinfarction Trial Research Group 1980)

    term 估计 的线性模型摘要 统计量 std.error
    p.value 2.431 0.135 18.05 <0.0001
    grouptreatment 0.395 0.210 1.89 0.0594
  1. 负鼠分类,交叉验证。 澳大利亚地区的普通刷尾负鼠比其远亲美洲负鼠要可爱一些。我们考虑来自澳大利亚两个地区的104只刷尾负鼠,这些负鼠可被视为来自该总体的随机样本。第一个地区是维多利亚州,位于澳大利亚东半部,横跨南部海岸。第二个地区由新南威尔士州和昆士兰州组成,构成澳大利亚的东部和东北部。我们使用逻辑回归来区分这两个地区的负鼠。结局变量称为 pop,当袋貂来自维多利亚州时取值为1,当它来自新南威尔士州或昆士兰州时取值为0。3

    \[ \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 + \beta_1 \times \texttt{tail\_l} \]

     

    \[ \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 + \beta_1 \times \texttt{total\_l} + \beta_2\times \texttt{sex} \]

    1. Fold2中有多少个观测值?使用仅以尾长作为预测变量的模型。在Fold2的观测值中,有多少个被正确预测为来自维多利亚州?有多少个被错误预测为来自维多利亚州?

    2. 用于构建为Fold2中的观测值进行预测的模型的观测值有多少个?

    3. 对于其中一个交叉验证折,使用尾长作为预测变量的模型估计了多少个系数?对于其中一个交叉验证折,使用总体长度和性别作为预测变量的模型估计了多少个系数?

  1. 早产儿,交叉验证。 美国卫生与公众服务部、疾病控制与预防中心收集该国登记出生的信息。这里使用的数据是从2014年出生记录中随机抽取的1000例样本(由于数据缺失删除了部分行)。我们使用逻辑回归,根据各种解释变量来预测婴儿是否早产。4 (ICPSR 2014)

    \[ \begin{aligned} \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 &+ \beta_1\times \texttt{mage} \\ &+ \beta_2 \times \texttt{weight} \\ &+ \beta_3 \times \texttt{mature} \\ &+ \beta_4 \times \texttt{visits} \\ &+ \beta_5 \times \texttt{gained} \\ &+ \beta_6 \times \texttt{habit} \end{aligned} \]

     

    \[ \begin{aligned} \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 &+ \beta_1\times \texttt{weight} \\ &+ \beta_2 \times \texttt{mature} \end{aligned} \]

    1. Fold2中有多少个观测值?使用仅以 weightmature 作为预测变量的模型。在Fold2的观测值中,有多少个被正确预测为早产?有多少个被错误预测为早产?

    2. 用于构建为Fold2中的观测值进行预测的模型的观测值有多少个?

    3. 在原始数据集中,大多数出生是早产还是足月?请解释。

    4. 对于其中一个交叉验证折,使用 mage, weight, mature, visits, gainedhabit 作为预测变量的模型估计了多少个系数?对于其中一个交叉验证折,使用 weightmature 作为预测变量的模型估计了多少个系数?

  1. 负鼠分类,用交叉验证选择模型。 在本题中,我们考虑来自澳大利亚两个地区(第一个地区是维多利亚州,第二个地区是新南威尔士州和昆士兰州)的104只帚尾袋貂,这些袋貂可视为来自该总体的随机样本。我们使用逻辑回归将袋貂分类到这两个地区。结果变量称为 pop,当袋貂来自维多利亚州时取值为1,当它来自新南威尔士州或昆士兰州时取值为0。

    \[ \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 + \beta_1\times \texttt{tail\_l} \]

     

    \[ \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 + \beta_1 \times \texttt{total\_l} + \beta_2 \times \texttt{sex} \]

    1. 对于包含尾长的模型,有多少个观测值被正确分类?被正确分类的观测值比例是多少?

    2. 对于包含总长和性别的模型,有多少个观测值被正确分类?被正确分类的观测值比例是多少?

    3. 如果只能在仅使用尾长作为预测变量与使用总长和性别作为预测变量之间(用于分类到地区)做出选择,你会选择哪个模型?请解释。

    4. 鉴于上述预测,第三个什么样的模型可能比上面两个模型中的任何一个都更可取?请解释。

  1. 早产儿,用交叉验证来选择模型。 美国卫生与公众服务部、疾病控制与预防中心收集该国登记出生的信息。这里使用的数据是从2014年出生记录中随机抽取的1000例样本(因数据缺失删除了部分行)。我们使用逻辑回归,根据各种解释变量来建模预测婴儿是否早产。 (ICPSR 2014)

    \[ \begin{aligned} \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 &+ \beta_1 \times \texttt{mage} \\ &+ \beta_2 \times \texttt{weight} \\ &+ \beta_3 \times \texttt{mature} \\ &+ \beta_4 \times \texttt{visits} \\ &+ \beta_5 \times \texttt{gained} \\ &+ \beta_6 \times \texttt{habit} \end{aligned} \]

     

    \[ \begin{aligned} \log_e \bigg(\frac{p}{1-p}\bigg) = \beta_0 &+ \beta_1\times \texttt{weight} \\ &+ \beta_2 \times \texttt{mature} \end{aligned} \]

    1. 对于包含6个预测变量的模型,有多少个观测被正确分类?被正确分类的观测占多大比例?

    2. 对于包含2个预测变量的模型,有多少个观测被正确分类?被正确分类的观测占多大比例?

    3. 如果必须在包含6个预测变量的模型和包含2个预测变量的模型之间做出选择(用于预测婴儿是否会早产),你会选择哪个模型?请解释。


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

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

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

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