Skip to content

Instantly share code, notes, and snippets.

@carlislerainey
Created October 8, 2025 12:33
Show Gist options
  • Select an option

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

Select an option

Save carlislerainey/a0b5cb6e92ded08d6f70bfc426e6610f to your computer and use it in GitHub Desktop.
Illustrating logistic regression with {brms}
# ---- setup ----
# nice printing
options(digits = 3)
# load packages
library(tidyverse)
library(brms)
# load only the turnout data frame and hard-code rescaled variables
turnout <- ZeligData::turnout |>
mutate(across(age:income, arm::rescale, .names = "rs_{.col}")) |>
glimpse()
# build model frame and design matrices
f <- vote ~ rs_age + rs_educate + rs_income + race
# fit model with brms
fit <- brm(f, data = turnout, family = bernoulli)
# compute qi
comparisons(fit, variables = list(rs_age = c(-0.5, 0.5)),
newdata = datagrid(grid_type = "mean_or_mode"))
# fit model with brms via cmdstan!
fit <- brm(f, data = turnout, family = bernoulli,
backend = "cmdstanr")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment