model_no_int <- lm(fert_rate ~ life_exp + income, data = UN_data_ch6)more than one explanatory variable, with moderndive
two models, one question + vs *
lm(y ~ x1 + x2)lm(y ~ x1 * x2)geom_parallel_slopes()coef()Fertility rate, life expectancy, and income group
One explanatory variable can mislead you when a second one is doing part of the work.
Hold The Other One Fixed
The parallel slopes model. Each group gets its own intercept, but every group shares one common slope.
Parallel Slopes
One slope for everybody, shifted up or down by group.
Example
the verb (function) gives each group its own intercept
response ~ numerical + categorical the data frame
Draw the parallel slopes model. Same code as a scatterplot, with geom_smooth() swapped out.
Example
one color per group
ggplot(UN_data_ch6, aes(x = life_exp, y = fert_rate, color = income)) +
geom_point() +
geom_parallel_slopes(se = FALSE)one line per group, all the same slope
The interaction model. Each group gets its own intercept and its own slope, so the lines are free to fan out.
Interaction
Use it when the effect of x differs from group to group.
The star fits an interaction: each group gets its own slope as well as its own intercept.
Example
the verb (function) interaction: slopes differ too
the data frame
Read the coefficients. With an interaction, the extra rows are offsets from the baseline group, not slopes on their own.
Example
the fitted model
(Intercept) life_exp
11.91826460 -0.11848075
incomeLower middle income incomeUpper middle income
-1.50420617 -1.89291757
incomeHigh income life_exp:incomeLower middle income
-6.57953685 0.01265027
life_exp:incomeUpper middle income life_exp:incomeHigh income
0.01117481 0.07221696
the verb (function)
The baseline group is the first level of the factor: here, Low income.
Baseline & Offsets
Fit both, look at both, then choose.
Parallel Or Interaction