5.7 Adding Fitted Lines from an Existing Model

5.7.1 Problem

You have already created a fitted regression model object for a data set, and you want to plot the lines for that model.

5.7.2 Solution

Usually the easiest way to overlay a fitted model is to simply ask stat_smooth() to do it for you, as described in Recipe 5.6. Sometimes, however, you may want to create the model yourself and then add it to your graph. This allows you to be sure that the model you’re using for other calculations is the same one that you see.

In this example, we’ll build a quadratic model using lm() with ageYear as a predictor of heightIn. Then we’ll use the predict() function and find the predicted values of heightIn across the range of values for the predictor, ageYear:

We can now plot the data points along with the values predicted from the model (as you’ll see in Figure 5.22):

5.7.3 Discussion

Any model object (e.g. lm) can be used, so long as it has a corresponding predict() method. For example, lm has predict.lm(), loess has predict.loess(), and so on. Adding lines from a model can be simplified by using the function predictvals(), defined below. If you simply pass a model to predictvals(), the function will do the work of finding the variable names and the range of the predictor, and will return a data frame with predictor and predicted values. That data frame can then be passed to geom_line() to draw the fitted line, as we did earlier:

With the heightweight data set, we’ll make a linear model with lm() and a LOESS model with loess() (Figure 5.22):

Then we can call predictvals() on each model, and pass the resulting data frames to geom_line():

A quadratic prediction line from an lm object (left); Prediction lines from linear (in red) and LOESS (in blue) models (right)A quadratic prediction line from an lm object (left); Prediction lines from linear (in red) and LOESS (in blue) models (right)

Figure 5.22: A quadratic prediction line from an lm object (left); Prediction lines from linear (in red) and LOESS (in blue) models (right)

For glm models use a nonlinear link function, you need to specify type = "response" to the predictvals() function. This is because the default behavior of glm is to return predicted values in the scale of the linear predictors, instead of in the scale of the response (y) variable.

To illustrate this, we’ll use the biopsy data set from the MASS package. As we did in Recipe 5.6, we’ll use V1 to predict class. Since logistic regressions require numeric values from 0 to 1, we need to convert the factor class to 0s and 1s:

Next, we’ll perform the logistic regression:

Finally, we’ll make the graph with jittered points and the fitlogistic line. We’ll make the line in a shade of blue by specifying a color in RGB values, and slightly thicker, with size = 1 (Figure 5.23):

A fitted logistic model

Figure 5.23: A fitted logistic model