Interactions tips
Goal
To be able to run, interpret and visualize interaction analysis.
End result we are aiming for
What does this praoh mean? (Focusing on Age Categories as an Example)
This graph presents hazard ratios (HRs) with 95% confidence intervals (CIs) for different subgroups in relation to Railway Noise and Road Traffic Noise.
Using the age categories (<65 and =65) as an example, the aim is to compare the association between traffic-related noise exposure and health risks (e.g., mortality or disease incidence) across different age groups.
For this graph we would need input data like below i.e. output of results of regression different subgroups.
Group | Subgroup | Hazard_Ratio | Lower_CI | Upper_CI | Noise_Type |
---|---|---|---|---|---|
Age | 1.04 | 1.01 | 1.08 | Road Traffic Noise | |
Age | =65 | 1.03 | 0.99 | 1.07 | Railway Noise |
Age | 1.01 | 0.96 | 1.06 | Road Traffic Noise | |
Age | <65 | 1.05 | 1.01 | 1.10 | Railway Noise |
But how do get these results from regression analysis? We can perfrom effect modification analysis.
Why effect modification
Effect modification occurs when the impact of noise exposure on health outcomes varies by age. If the hazard ratio (HR) for road or railway noise differs between <65 and =65 groups, it suggests that age modifies the effect of noise exposure. This means older or younger individuals may be more susceptible to noise-related health risks, indicating potential differences in biological vulnerability or lifestyle factors.
What is effect modification?
Effect modification occurs when the association between an exposure (e.g., noise) and an outcome (e.g., disease risk) varies across levels of another variable (e.g., age). This means that the impact of noise on health differs for younger vs. older individuals, rather than being uniform across all age groups.
Effect Modification Example Using mtcars
Dataset
We’ll analyze whether the effect of horsepower (hp
) on miles per gallon (mpg
) is modified by cylinder category (cyl
).
Without Effect Modification (No Interaction)
mpg=β0+β1(hp)+β2(cyl6)+β3(cyl8)+β4(wt)+ε
What Each Term Means:
β0
(Intercept) → Expectedmpg
whenhp = 0
,cyl = 4
, andwt = 0
.β1
(hp
) → Effect of increasinghp
onmpg
, assumed to be the same across allcyl
categories.β2
(cyl6
) → Difference inmpg
for6-cylinder
cars compared to 4-cylinder cars.β3
(cyl8
) → Difference inmpg
for8-cylinder
cars compared to 4-cylinder cars.β4
(wt
) → Effect of car weight (wt
) onmpg
.ε
(error term) → Captures unexplained variability.
How to Calculate Effects:
For 4-Cylinder Cars (
cyl = 4
):mpg=β0+β1(hp)+β4(wt)
- Effect of
hp
remainsβ1
, constant for all cylinder groups.
- Effect of
For 6-Cylinder Cars (
cyl = 6
):mpg=β0+β1(hp)+β2+β4(wt)
β2
shifts the intercept but does not change the slope ofhp
.
For 8-Cylinder Cars (
cyl = 8
):mpg=β0+β1(hp)+β3+β4(wt)
β3
shifts the intercept but does not change the slope ofhp
.
# Load dataset
data(mtcars)
# View first few rows
# head(mtcars)
# Check correlations
# cor(mtcars[, c("mpg", "hp", "wt")])
<- lm(mpg ~ hp + wt + cyl, data = mtcars)
lm_simple # summary(lm_simple)
library(ggplot2)
# Create plot
ggplot(mtcars, aes(x = hp, y = mpg,
# color = factor(wt > median(wt))
# color = factor(cyl)
+
)) geom_point() +
labs(title = "Horsepower and Weight on MPG",
x = "Horsepower (hp)", y = "Miles Per Gallon (mpg)") +
theme_minimal()
We can try to fit regression over it
# Create plot
ggplot(mtcars, aes(x = hp, y = mpg,
# color = factor(wt > median(wt))
# color = factor(cyl)
+
)) geom_point() +
geom_smooth(method = "lm", color="black", se = FALSE) +
labs(title = "Horsepower and Weight on MPG",
x = "Horsepower (hp)", y = "Miles Per Gallon (mpg)") +
theme_minimal()
The scatter plot shows the negative relationship between horsepower (hp) and miles per gallon (mpg), with a clear downward trend. The fitted regression line indicates that as horsepower increases, fuel efficiency decreases. This suggests that higher-powered cars consume more fuel, leading to lower MPG. However, since weight (wt) is mentioned in the title but not visually represented, it may be a confounder or an adjustment factor in the model.
# Create plot
ggplot(mtcars, aes(x = hp, y = mpg,
# color = factor(wt > median(wt))
color = factor(cyl)
+
)) geom_point() +
geom_smooth(method = "lm", color="black", se = FALSE) +
labs(title = "Horsepower and Weight on MPG",
x = "Horsepower (hp)", y = "Miles Per Gallon (mpg)", color = "Cylinders") +
theme_minimal()
Model With Effect Modification (Including Interaction Term)
mpg=β0+β1(hp)+β2(cyl6)+β3(cyl8)+β4(hp×cyl6)+β5(hp×cyl8)+β6(wt)+ε
β4
(hp × cyl6
) → Additional effect ofhp
onmpg
for6-cylinder
cars.β5
(hp × cyl8
) → Additional effect ofhp
onmpg
for8-cylinder
cars.
How to Calculate Effects:
For 4-Cylinder Cars (
cyl = 4
) (Reference Group):mpg=β0+β1(hp)+β6(wt) = _0 + _1() + _6()mpg=β0+β1(hp)+β6(wt)
- Effect of
hp
=β1
.
- Effect of
For 6-Cylinder Cars (
cyl = 6
):mpg=β0+β1(hp)+β2+(β4×hp)+β6(wt) = _0 + _1() + _2 + (_4 ) + _6()mpg=β0+β1(hp)+β2+(β4×hp)+β6(wt)
- Effect of
hp
=β1 + β4
(modification bycyl6
).
- Effect of
For 8-Cylinder Cars (
cyl = 8
):mpg=β0+β1(hp)+β3+(β5×hp)+β6(wt) = _0 + _1() + _3 + (_5 ) + _6()mpg=β0+β1(hp)+β3+(β5×hp)+β6(wt)
- Effect of
hp
=β1 + β5
(modification bycyl8
).
- Effect of
# Create plot
ggplot(mtcars, aes(x = hp, y = mpg,
# color = factor(wt > median(wt))
color = factor(cyl)
+
)) geom_point() +
# geom_smooth(method = "lm", aes(group = factor(wt > median(wt))), se = FALSE) +
geom_smooth(method = "lm", aes(group = factor(cyl)), se = FALSE) +
# geom_smooth(method = "lm", color="black", se = FALSE) +
labs(title = "Interaction Between Horsepower and Weight on MPG",
x = "Horsepower (hp)", y = "Miles Per Gallon (mpg)", color = "Cylinders") +
theme_minimal()
This plot shows the interaction between horsepower (hp
) and cylinders (cyl
) on fuel efficiency (mpg
).
Key Insights:
Different slopes for each cylinder group indicate effect modification—the impact of horsepower on MPG depends on the number of cylinders.
4-cylinder cars (red line) show a steeper decline, meaning
hp
has a stronger negative effect onmpg
for smaller engines.6-cylinder cars (green line) have a flatter slope, indicating
hp
has a weaker effect onmpg
compared to 4-cyl cars.8-cylinder cars (blue line) show a gradual decline, meaning they start with lower fuel efficiency but
hp
does not affectmpg
as drastically as in 4-cylinder cars.
Conclusion:
Effect modification is present—the influence of horsepower on MPG varies by cylinder category.
Larger engines (8-cyl) are less sensitive to increased horsepower in terms of MPG reduction.
Smaller engines (4-cyl) suffer greater fuel efficiency losses as horsepower increases.