Modeling an interaction
In this exercise, you will use interactions to model the effect of gender and gastric activity on alcohol metabolism.
The alcohol
data frame has been pre-loaded, and has the columns:
Metabol
: the alcohol metabolism rateGastric
: the rate of gastric alcohol dehydrogenase activitySex
: the sex of the drinker (Male
orFemale
)
In the video, we fit three models to the alcohol
data:
- one with only additive (main effect) terms :
Metabol ~ Gastric + Sex
- two models, each with interactions between gastric activity and sex
You saw that one of the models with interaction terms had a better R-squared than the additive model, suggesting that using interaction terms gives a better fit. In this exercise, you will compare the R-squared of one of the interaction models to the main-effects-only model.
Recall that the operator :
designates the interaction between two variables. The operator *
designates the interaction between the two variables, plus the main effects.
x*y = x + y + x:y
This exercise is part of the course
Supervised Learning in R: Regression
Exercise instructions
- Write a formula that expresses
Metabol
as a function ofGastric
andSex
with no interactions.- Assign the formula to the variable
fmla_add
and print it.
- Assign the formula to the variable
- Write a formula that expresses
Metabol
as a function of the interaction betweenGastric
andSex
.- Add
Gastric
as a main effect, but notSex
. - Assign the formula to the variable
fmla_interaction
and print it.
- Add
- Fit a linear model with only main effects:
model_add
to the data. - Fit a linear model with the interaction:
model_interaction
to the data. - Call
summary()
on both models. Which has a better R-squared?
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# alcohol is available
summary(alcohol)
# Create the formula with main effects only
(fmla_add <- ___ )
# Create the formula with interactions
(fmla_interaction <- ___ )
# Fit the main effects only model
model_add <- ___
# Fit the interaction model
model_interaction <- ___
# Call summary on both models and compare
___
___