Get startedGet started for free

Specifying an interaction

So far you've used a single parallel slopes model, which gave an OK fit for the whole dataset, then three separate models for each house age category, which gave a better fit for each individual category, but was clunky because you had three separate models to work with and explain. Ideally, you'd have a single model that had all the predictive power of the individual models.

Defining this single model is achieved through adding interactions between explanatory variables. The syntax of statsmodels.formula is flexible, and gives you a couple of options, depending on whether you prefer concise code that is quick to type and to read, or explicit code that describes what you are doing in detail.

taiwan_real_estate is available.

This exercise is part of the course

Intermediate Regression with statsmodels in Python

View Course

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Model price vs both with an interaction using "times" syntax
mdl_price_vs_both_inter = ____

# Print the coefficients
print(mdl_price_vs_both_inter.params)
Edit and Run Code