Get startedGet started for free

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 rate
  • Gastric: the rate of gastric alcohol dehydrogenase activity
  • Sex: the sex of the drinker (Male or Female)

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

View Course

Exercise instructions

  • Write a formula that expresses Metabol as a function of Gastric and Sex with no interactions.
    • Assign the formula to the variable fmla_add and print it.
  • Write a formula that expresses Metabol as a function of the interaction between Gastric and Sex.
    • Add Gastric as a main effect, but not Sex.
    • Assign the formula to the variable fmla_interaction and print it.
  • 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
___
___
Edit and Run Code