library(easystats)
library(ggplot2)
library(marginaleffects)
library(qqplotr)
library(sandwich)
data("penguins", "mtcars", "cars", "airquality")
theme_set(theme_bw(base_size = 20))Linear Modeling Review (2/2)
Spring 2026 | CLAS | PSYC 894
Jeffrey M. Girard | Lecture 02b
Continous-by-Continous
Variable Moderation
Continuous-by-Discrete
Variable Moderation
Assumptions, Diagnostics,
and Extensions of LM
New Packages:
Continuous-by-Continuous Moderation
\[y_i = \beta_0 + \beta_1 x_{1i} + \beta_2 x_{2i} + \beta_3 (x_{1i} x_{2i}) + \varepsilon_{i}\]
\[\text{MASS}_i = \beta_0 + \beta_1 \text{FLEN}_{i} + \beta_2 \text{BDEP}_{i} + \\ \beta_3 \text{FLEN}_i \text{BDEP}_i + \varepsilon_i\]
y ~ 1 + x1 * x2
body_mass ~ 1 + flipper_len * bill_dep

Parameter | Coefficient | SE | 95% CI | t(338) | p
-----------------------------------------------------------------------------------
(Intercept) | 4062.79 | 29.56 | [4004.65, 4120.93] | 137.46 | < .001
flipper len | 48.63 | 1.82 | [ 45.05, 52.22] | 26.71 | < .001
bill dep | 44.64 | 13.02 | [ 19.04, 70.25] | 3.43 | < .001
flipper len × bill dep | -8.60 | 1.34 | [ -11.23, -5.96] | -6.41 | < .001
After centering the flipper length and bill depth predictors, …
Intercept \((\beta_0=4063.8\), \(p<.001)\)
The expected body mass for a penguin with average flipper length and bill depth.
Flipper Length Simple Effect \((\beta_1=48.6\), \(p<.001)\)
The expected change in body mass associated with an increase of 1mm flipper length,
specifically for a penguin with average bill depth.
Bill Depth Simple Effect \((\beta_2=44.6\), \(p<.001)\)
The expected change in body mass associated with an increase of 1mm bill depth,
specifically for a penguin with average flipper length.
Flipper-Length-by-Bill-Depth Interaction Effect \((\beta_3=-8.6\), \(p<.001)\)
The expected change in the slope of one predictor for an increase of 1 in the other.
The negative sign here means the flipper length effect gets weaker as bills get deeper.
Estimated Marginal Effects
bill_dep | Slope | SE | 95% CI | t(338) | p
----------------------------------------------------------
-1.55 | 61.97 | 2.40 | [57.25, 66.69] | 25.84 | < .001
0.15 | 47.35 | 1.88 | [43.65, 51.05] | 25.18 | < .001
1.55 | 35.32 | 3.08 | [29.25, 41.38] | 11.45 | < .001
Marginal effects estimated for flipper_len
Type of slope was dY/dX
Estimated Marginal Effects
flipper_len | Slope | SE | 95% CI | t(338) | p
-----------------------------------------------------------------
-10.91 | 138.47 | 22.01 | [ 95.17, 181.77] | 6.29 | < .001
-3.92 | 78.30 | 15.28 | [ 48.25, 108.34] | 5.13 | < .001
12.09 | -59.25 | 17.90 | [-94.46, -24.03] | -3.31 | 0.001
Marginal effects estimated for bill_dep
Type of slope was dY/dX
Continuous-by-Discrete Moderation
\[ \begin{aligned} y_i &= \overbrace{(\beta_0 + \beta_2 D_{1i} + \dots)}^{\text{Group-Specific Intercept}} + \overbrace{(\beta_1 + \beta_3 D_{1i} + \dots)}^{\text{Group-Specific Slope}} \cdot x_i + \varepsilon_i \end{aligned} \]
\[ \begin{aligned} \text{MASS}_i &= (\beta_0 + \beta_2 \text{CHIN}_{i} + \beta_3 \text{GENT}_i) \\ &\quad + (\beta_1 + \beta_4 \text{CHIN}_{i} + \beta_5 \text{GENT}_i) \cdot \text{FLEN}_i + \varepsilon_i \end{aligned} \]
y ~ 1 + x * f
mass ~ 1 + flipper * species

Parameter | Coefficient | 95% CI | p
-----------------------------------------------------------------------------
(Intercept) | 4060.55 | [3944.47, 4176.62] | < .001
flipper len | 32.83 | [ 23.73, 41.93] | < .001
species [Chinstrap] | -151.42 | [-310.58, 7.73] | 0.062
species [Gentoo] | 126.66 | [ -85.98, 339.31] | 0.242
flipper len × species [Chinstrap] | 1.74 | [ -13.71, 17.19] | 0.825
flipper len × species [Gentoo] | 21.79 | [ 8.14, 35.44] | 0.002
After centering the Flipper Length predictor and setting Adelie as reference, …
Intercept \((\beta_0=4060.6\), \(p<.001)\)
The expected body mass for an Adelie penguin with average flipper length.
Flipper Length Simple Effect \((\beta_1=32.8\), \(p<.001)\)
The expected change in mass associated with an increase of 1mm flipper length,
specifically for the reference group (Adelie).
Chinstrap Simple Effect \((\beta_2=-151.4\), \(p=.062)\)
The difference in expected mass between Chinstrap and Adelie penguins,
specifically for penguins with average flipper length.
Gentoo Simple Effect \((\beta_3=126.7\), \(p=.242)\)
The difference in expected mass between Gentoo and Adelie penguins,
specifically for penguins with average flipper length.
Flipper-by-Chinstrap Interaction Effect \((\beta_4=1.7\), \(p=.825)\)
The adjustment to the flipper slope for Chinstraps compared to Adelies.
The flipper–mass relationship is not significantly different for Chinstraps and Adelies.
Flipper-by-Gentoo Interaction Effect \((\beta_5=21.8\), \(p=.002)\)
The adjustment to the flipper slope for Gentoos compared to Adelies.
The flipper–mass relationship is significantly steeper for Gentoos than for Adelies.
Estimated Marginal Effects
species | Slope | SE | 95% CI | t(336) | p
-----------------------------------------------------------
Adelie | 32.83 | 4.63 | [23.73, 41.93] | 7.10 | < .001
Chinstrap | 34.57 | 6.33 | [22.11, 47.03] | 5.46 | < .001
Gentoo | 54.62 | 5.17 | [44.46, 64.79] | 10.57 | < .001
Marginal effects estimated for flipper_len
Type of slope was dY/dX
Notice the Gentoo slope (54.6) is the Adelie slope (32.8) plus the interaction (21.8).
| Formula | Slopes Estimated | ||||||
|---|---|---|---|---|---|---|---|
y ~ x
|
\(x\) | ||||||
y ~ x + w
|
\(x\) | \(w\) | |||||
y ~ x * w
|
\(x\) | \(w\) | \(xw\) | ||||
y ~ x + w + z
|
\(x\) | \(w\) | \(z\) | ||||
y ~ x * w + z
|
\(x\) | \(w\) | \(z\) | \(xw\) | |||
y ~ x * (w + z)
|
\(x\) | \(w\) | \(z\) | \(xw\) | \(xz\) | ||
y ~ (x + w + z)^2
|
\(x\) | \(w\) | \(z\) | \(xw\) | \(xz\) | \(wz\) | |
y ~ x * w * z
|
\(x\) | \(w\) | \(z\) | \(xw\) | \(xz\) | \(wz\) | \(xwz\) |
Note that predictors can be continuous or discrete, but if a discrete predictor has more than two levels, you will end up with additional slopes due to dummy coding. To represent \(g\) groups, it will create \(g-1\) dummy codes (with a slope for each).
Note: Adding horsepower hurts fuel economy, but this levels off (you can’t go below 0 mpg), creating the curve that the linear model misses.

poly(x, degree = 2) or poly(x, 2)Note: At low speeds, points are near the line. At high speeds, they spread out. We predict distance for “slow” stops well, but not for “fast” stops.

Parameter | Coefficient | SE | 95% CI | t(48) | p
-------------------------------------------------------------------
(Intercept) | -17.58 | 6.76 | [-31.17, -3.99] | -2.60 | 0.012
speed | 3.93 | 0.42 | [ 3.10, 4.77] | 9.46 | < .001
Parameter | Coefficient | SE | 95% CI | t(48) | p
-------------------------------------------------------------------
(Intercept) | -17.58 | 5.93 | [-29.51, -5.65] | -2.96 | 0.005
speed | 3.93 | 0.43 | [ 3.07, 4.79] | 9.20 | < .001
Observation: In this case, the penalty is small! But we pay it anyway because it ensures our inferences and conclusions are valid.
Note: The model fits a straight line, but there are outliers and negative Ozone (impossible) is being predicted.

Parameter | Coefficient | SE | 95% CI | t(114) | p
--------------------------------------------------------------------
(Intercept) | 96.87 | 7.24 | [82.53, 111.21] | 13.38 | < .001
Wind | -5.55 | 0.69 | [-6.92, -4.18] | -8.04 | < .001
Parameter | Coefficient | 95% CI | p
----------------------------------------------------
(Intercept) | 97.09 | [80.15, 114.61] | < .001
Wind | -5.56 | [-7.15, -4.07] | < .001
Observation: The confidence interval changed slightly. Because we had a decent sample size (\(n=153\)), the Central Limit Theorem helped the standard approach. In smaller samples, the difference would likely be larger.