
Interaction Effects
Read It: Interaction Effects
When developing multiple linear regression models, it is possible to account for interactions between the variables. For example, maybe variable 'x' is not a great predictor of 'y', but the combined effect of 'x' and 'z' is an important predictor. In this case, the interactions can improve the accuracy of our model. In Python, we signify interactions with an asterisk (*
) or a colon (:
), depending on the type of interaction. The *
, for example, indicates that Python should consider both the interaction between two terms and as separate predictors, while the :
tells Python that you only want to consider the interaction. Below, we demonstrate the use of these key symbols in Python.
Watch It: Video - Interaction Effects (8:43 minutes)
ADD TRANSCRIPT TEXT HERE
Try It: DataCamp - Apply Your Coding Skills
Edit the following code to implement multiple linear regression with interaction effects. Your response variable is 'y' and all other variables can be used as explanatory variables.
# This will get executed each time the exercise gets initialized.
# libraries
import pandas as pd
import numpy as np
import statsmodels.formula.api as smf
# create some data
df = pd.DataFrame({'y': np.random.uniform(10.0, 25.0, 50),
'x1': np.random.uniform(0.001, 0.999, 50),
'x2': np.random.uniform(0.001, 0.999, 50),
'x3': np.random.uniform(175, 250, 50),
'x4': np.random.uniform(5.7, 6.9, 50),
'x5': np.random.uniform(10.0, 25.0, 50)})
# implement MLR with interactions
results = ...
# print model summary
...
# libraries
import pandas as pd
import numpy as np
import statsmodels.formula.api as smf
# create some data
df = pd.DataFrame({'y': np.random.uniform(10.0, 25.0, 50),
'x1': np.random.uniform(0.001, 0.999, 50),
'x2': np.random.uniform(0.001, 0.999, 50),
'x3': np.random.uniform(175, 250, 50),
'x4': np.random.uniform(5.7, 6.9, 50),
'x5': np.random.uniform(10.0, 25.0, 50)})
# implement MLR with interactions
results = smf.ols('y ~ x1:x2 + x3*x4 + x5', data = df, hasconst = True).fit()
# print model summary
print(results.summary())