# A tibble: 344 × 8
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
<fct> <fct> <dbl> <dbl> <int> <int>
1 Adelie Torgersen 39.1 18.7 181 3750
2 Adelie Torgersen 39.5 17.4 186 3800
3 Adelie Torgersen 40.3 18 195 3250
4 Adelie Torgersen NA NA NA NA
5 Adelie Torgersen 36.7 19.3 193 3450
6 Adelie Torgersen 39.3 20.6 190 3650
7 Adelie Torgersen 38.9 17.8 181 3625
8 Adelie Torgersen 39.2 19.6 195 4675
9 Adelie Torgersen 34.1 18.1 193 3475
10 Adelie Torgersen 42 20.2 190 4250
# ℹ 334 more rows
# ℹ 2 more variables: sex <fct>, year <fct>
model <- penguins_data %>%glm(fct_rev(sex) ~ flipper_length_mm * species + year,family = binomial,data = .)
1
Reversing our factor order so that “male” is the omitted category — and so that our model predicts whether a penguin is female.
2
Allowing multiplicative interaction between flipper length and species on the right-hand side.
3
Accounting for time trend via year fixed effects.
4
Using default logistic link function for binomial generalized linear models.
Here, we’re fitting a crude model to map how the association between flipper length (our focal predictor) and sex (our outcome of interest) is moderated by the species a penguin belongs to. We account for time trends, too.
Code
# For more information, see: https://popagingdataviz.com/day3.htmlvar_labels <-rev(c("flipper_length_mm"="Flipper Length (mm)", "flipper_length_mm:speciesGentoo"="Flipper Length (mm) x Gentoo","flipper_length_mm:speciesChinstrap"="Flipper Length (mm) x Chinstrap"))modelplot(model,# Variable names:coef_map = var_labels,# 99% confidence interval:conf_level =0.99) +# Marking significance; line:geom_vline(xintercept =0, lty ="dotted") +# Marking significance; colour scheme:aes(colour =ifelse(p.value <0.01, "Significant", "Not significant")) +scale_colour_manual(values =c("grey", "#3f1f69")) +labs(colour ="", x ="Log Odds That Penguin Is Female\n (with 99% Confidence Intervals)") +theme_bw(base_family ="IBM Plex Sans",base_size =13)
Leveraging {marginaleffects}
The Marginal Effects Zoo
A brilliant library developed by Vincent Arel-Bundock (2024).
King, Gary, Michael Tomz, and Jason Wittenberg. 2000. “Making the Most of Statistical Analyses: Improving Interpretation and Presentation.”American Journal of Political Science 44 (2): 347–61. https://doi.org/10.2307/2669316.
Long, J. Scott, and Sarah A. Mustillo. 2021. “Using Predictions and Marginal Effects to Compare Groups in Regression Models for Binary Outcomes.”Sociological Methods & Research 50 (3): 1284–1320. https://doi.org/10.1177/0049124118799374.
Mize, Trenton D. 2019. “Best Practices for Estimating, Interpreting, and Presenting Nonlinear Interaction Effects.”Sociological Science 6 (February): 81–117. https://doi.org/10.15195/v6.a4.
Mize, Trenton D., Long Doan, and J. Scott Long. 2019. “A General Framework for Comparing Predictions and Marginal Effects Across Models.”Sociological Methodology 49 (1): 152–89. https://doi.org/10.1177/0081175019852763.