Including an interaction
Just as in the case with one numeric and one categorical explanatory variable, it is possible for numeric explanatory variables to interact. With this model structure, you'll get a third slope coefficient: one for each explanatory variable and one for the interaction.
Here, you'll run, predict, and plot the same model as in the previous exercise, but this time including an interaction between the explanatory variables.
This exercise is part of the course
Intermediate Regression with statsmodels in Python
Exercise instructions
- Change the name of the model to
mdl_price_vs_conv_dist_inter
. - Adapt the rest of the code from the previous exercise to include an interaction.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Convert to mdl_price_vs_conv_dist_inter
mdl_price_vs_conv_dist = ols("price_twd_msq ~ n_convenience + sqrt_dist_to_mrt_m", data=taiwan_real_estate).fit()
# Use mdl_price_vs_conv_dist_inter to make predictions
n_convenience = np.arange(0, 11)
sqrt_dist_to_mrt_m = np.arange(0, 81, 10)
p = product(n_convenience, sqrt_dist_to_mrt_m)
explanatory_data = pd.DataFrame(p, columns=["n_convenience", "sqrt_dist_to_mrt_m"])
prediction_data = explanatory_data.assign(
price_twd_msq = mdl_price_vs_conv_dist.predict(explanatory_data))
sns.scatterplot(x="n_convenience", y="sqrt_dist_to_mrt_m", data=taiwan_real_estate, hue="price_twd_msq", legend=False)
sns.scatterplot(x="n_convenience", y="sqrt_dist_to_mrt_m", data=prediction_data, hue="price_twd_msq", marker="s")
plt.show()