The Predict() Function
Learn to predict values using a built-in predict() function in R.
Background
Now that we’ve built our linear regression model, let’s try predicting values on our test data.
Previously, we’ve been calculating our predicted values by hand, but now, let’s calculate them using a function built into R called predict()
.
The most important reason for using predict()
is that it provides an easy way to calculate 95% confidence intervals (CIs) around regression lines. It is also convenient for nonlinear functions, which we’ll explore more in the upcoming chapter. As we’ve seen, it’s easy to use the geom_smooth()
function to plot regressions with confidence intervals.
However, sometimes we might want to know what those numbers are, which we can’t deduce from the plot—although, of course, we can visually estimate them.
Using the predict()
function takes a little getting used to, but it’s beneficial once we get the hang of it.
Parameters required
The predict()
function only requires two arguments, but in practical use, we’ll most likely want to provide it with three. We need to provide the predict()
...