Skip to content

Instantly share code, notes, and snippets.

View carlislerainey's full-sized avatar

Carlisle Rainey carlislerainey

View GitHub Profile
@carlislerainey
carlislerainey / logit-metropolis.R
Created October 8, 2025 10:22
Illustrating the metropolis algorithm with logistic regression
# ---- setup ----
# nice printing
options(digits = 3)
# load packages
library(tidyverse)
library(posterior)
library(foreach)
library(doParallel)
@carlislerainey
carlislerainey / toothpaste-metropolis.R
Created October 8, 2025 10:04
Illustrating the Metropolis algorithm with a beta(11, 165) posterior
# ---- setup ----
# nice printing
options(digits = 3)
# load packages
library(tidyverse)
library(posterior)
library(foreach)
library(doParallel)
@carlislerainey
carlislerainey / duration-examples.R
Created October 1, 2025 10:20
R code to illustrate fitting duration models
# load packages
library(survival)
# load data
canc <- survival::cancer |>
mutate(sex = case_when(sex == 1 ~ "Male",
sex == 2 ~ "Female"))
# quick look
glimpse(canc)
@carlislerainey
carlislerainey / rejection-weird.R
Created October 1, 2025 10:18
Code to illustrate the rejection algorithm via a weird example
# a function for rejection algorithm
rej <- function(f, S, M) {
# record start time
start_time <- Sys.time()
# create containers and initialize counters
samples <- numeric(S) # container to store samples
rejects <- NULL # container to track rejected values; for teaching; slow!
s <- 1 # currently trying to take sample 1
@carlislerainey
carlislerainey / beta-posterior.R
Created September 30, 2025 13:10
Illustrate beta posterior
# prior
alpha_star <- 3
beta_star <- 15
# data
N <- 150
k <- 8
# posterior
alpha_prime <- alpha_star + k
@carlislerainey
carlislerainey / illustrate-marginaleffects.R
Created September 24, 2025 10:22
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
@carlislerainey
carlislerainey / illustrate-zi.R
Last active September 24, 2025 17:39
Illustrating zero-inflation with Holland (2015)
# load package
library(tidyverse)
library(marginaleffects)
library(modelsummary)
library(glmmTMB)
# load data for santiago
sant <- crdata::holland2015 |>
filter(city == "santiago") |>
glimpse()
@carlislerainey
carlislerainey / illustrate-ic.R
Created September 24, 2025 09:51
Illustrating IC with the turnout dataset
# load packages
library(tidyverse)
library(tinytable)
# data
devtools::install_github("jrnold/ZeligData")
turnout <- ZeligData::turnout
# fit model
f <- vote ~ age + educate + income + race
@carlislerainey
carlislerainey / nb-example.R
Created September 17, 2025 16:06
Example of how to fit and NB model with optim()
# ---- fit negative binomial model with optim() ----
# data; see ?crdata::holland2015
holland <- crdata::holland2015 |>
filter(city == "santiago")
# formula corresponds to model 1 for each city in holland (2015) table 2
f <- operations ~ lower + vendors + budget + population
# ---- create a function to fit the model ----
@carlislerainey
carlislerainey / poisson-example.R
Created September 17, 2025 16:05
Example of how to fit a Poisson model with optim()
# ---- fit poisson model with optim() ----
# data; see ?crdata::holland2015
holland <- crdata::holland2015 |>
filter(city == "santiago")
# formula corresponds to model 1 for each city in holland (2015) table 2
f <- operations ~ lower + vendors + budget + population