Get startedGet started for free

Making predictions with interactions

1. Making predictions with interactions

Let's run through the prediction flow again, this time with the model containing an interaction. I'm hoping you get a sense of deja vu, because you've seen all this code before.

2. The model with the interaction

Here's the model of mass versus length and species with an interaction. I've used the version of the formula that gives the clearest coefficients, but I could also have used the simpler length times species syntax; it doesn't affect the predictions.

3. The prediction flow

Here's the code for the prediction flow. It's exactly the same as the code in the parallel slopes model. The important thing to remember here is the use of product from itertools to get all the combinations of lengths and species. Then you convert this to a DataFrame, naming the columns accordingly. statsmodels will automatically take care of the interaction when creating prediction data, so you don't need to change anything. The final prediction data will look like this.

4. Visualizing the predictions

Here's the plot of predictions - both seaborn's automatic prediction lines, and the points predicted from the model. The plot is identical to the one you saw in the first video of this chapter. This time however, the code is simpler because you don't have four separate models to worry about.

5. Manually calculating the predictions

To see how the predictions work, let's manually calculate them. First we get the coefficients from the model using the params attribute. Then we unpack all the coefficients into the four intercepts and the four slopes.

6. Manually calculating the predictions

Recall numpy's select function, which takes a list of conditions and an equal-sized list of choices as arguments. In the case of the intercept choices, np dot select will retrieve the corresponding intercept for each of the fish species. You can read it as: 'if the species is Bream, pick the Bream intercept from the choice list. If the species is Perch, pick the Perch intercept, and so on.' Since we now have one slope for each of the species as well, we also apply the same np dot select function on the slope choices, each time picking the right slope from the choice list.

7. Manually calculating the predictions

Finally, to calculate the predictions themselves, use the formula intercept plus slope times explanatory data. Thanks to np dot select, the appropriate intercept and slope will be used for each fish species. The calculated values are the same as those returned by the predict function.

8. Let's practice!

Time for you to try this yourself.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.