Skip to content

Instantly share code, notes, and snippets.

@carlislerainey
Created September 24, 2025 10:22
Show Gist options
  • Select an option

  • Save carlislerainey/507332fe1f30ea097f3513ad2d195404 to your computer and use it in GitHub Desktop.

Select an option

Save carlislerainey/507332fe1f30ea097f3513ad2d195404 to your computer and use it in GitHub Desktop.
Illustrating {marginaleffects} with turnout
# load packages
library(tidyverse)
library(marginaleffects)
# data
devtools::install_github("jrnold/ZeligData")
turnout <- ZeligData::turnout
# fit model
f <- vote ~ age + educate + income + race
fit <- glm(f, family = binomial, data = turnout)
# ev as age ranges from 18 to 90; others at mean/mode
predictions(fit, datagrid(age = 18:90))
# fd as age moves across iqr; others at every observed value
comparisons(fit, variables = list(age = "iqr"))
# avg of the fds above
avg_comparisons(fit, variables = list(age = "iqr"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment