7 Linear and logistic regression

 

This chapter covers

  • Using linear regression to predict quantities
  • Using logistic regression to predict probabilities or categories
  • Extracting relations and advice from linear models
  • Interpreting the diagnostics from R’s lm() call
  • Interpreting the diagnostics from R’s glm() call
  • Using 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 discuss if a model is good or bad, we’ll move on to the modeling step, as shown in the mental model (figure 7.1). In this chapter, we’ll cover fitting and interpreting linear models in R.

Figure 7.1. Mental model

Linear models are especially useful when you don’t want only to predict an outcome, but also 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’ll 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