chapter seven

7 Linear and logistic regression

 

This chapter covers:

  • How to use linear regression to predict quantities
  • How to use logistic regression to predict probabilities or categories
  • How to extract relations and advice from linear models
  • How to interpret the diagnostics from R’s lm() call
  • How to interpret the diagnostics from R’s glm() call
  • How to use regularization via the glmnet package to address issues that can arise with linear models.

In the previous chapter, you learned how to evaluate models. Now that we have the ability to dicuss if a model is good or bad, we move on to the modeling step, as shown in the mental model (Figure 7.1). In this chapter we cover fitting and interpreting linear models in R.

Figure 7.1. Mental model
Mental model

Linear models are especially useful when you don’t just want to predict an outcome, but you also want to know the relationship between the input variables and the outcome. This knowledge can prove useful because this relationship can often be used as advice on how to get the outcome that you want.

We first define linear regression and then use it to predict customer income. Later we will use logistic regression to predict the probability that a newborn baby will need extra medical attention. We’ll also walk through the diagnostics that R produces when you fit a linear or logistic model.

7.1  Using linear regression

7.1.1  Understanding linear regression

7.1.2  Building a linear regression model

7.1.3  Making predictions

7.1.4  Finding relations and extracting advice

7.1.5  Reading the model summary and characterizing coefficient quality

7.1.6  Linear regression takeaways

7.2  Using logistic regression

7.2.1  Understanding logistic regression

7.2.2  Building a logistic regression model

7.2.3  Making predictions

7.2.4  Finding relations and extracting advice from logistic models

7.2.5  Reading the model summary and characterizing coefficients

7.2.6  Logistic regression takeaways

7.3  Regularization

7.3.1  An Example of Quasi-separation