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
glmnetpackage 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
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.