Skip to content

Instantly share code, notes, and snippets.

View carlislerainey's full-sized avatar

Carlisle Rainey carlislerainey

View GitHub Profile
@carlislerainey
carlislerainey / legit-example.R
Created September 17, 2025 01:40
Fitting a logit model with optim()
# ---- fit logit model with optim() ----
# data
devtools::install_github("jrnold/ZeligData")
turnout <- ZeligData::turnout
# formula
f <- vote ~ age + educate + income + race
@carlislerainey
carlislerainey / nominate-117-rs.R
Last active September 10, 2025 10:23
R code using the normal model to illustrate optim(), Fisher information, and delta method
# load packages
library(tidyverse)
library(numDeriv) # for numerical gradients
# get the data ready
# ------------------
# nominate data I use for teaching
nom <- read_csv("https://pos3713ri.github.io/data/nominate.csv") |>
@carlislerainey
carlislerainey / parametric-bs-toothpaste.R
Created September 9, 2025 15:26
Code for parametric bootstrap for the toothpaste cap problem
pi_hat <- 8/150 # ml estimate from observed data
n_bs <- 2000
bs_est <- numeric(n_bs) # a container for the estimates
for (i in 1:n_bs) {
bs_y <- rbinom(150, size = 1, prob = 8/150)
bs_est[i] <- mean(bs_y)
}
print(sd(bs_est), digits = 2) # se estimate
@carlislerainey
carlislerainey / poisson-ml-sampling-distribution.R
Last active September 10, 2025 12:19
R code to simulate the sampling distribution of Poisson ML estimator
# illustrate the sampling distribution of our poisson estimator
lambda <- 3
N <- 100 # sample size
n_reps <- 1000 # number of imagined repeated studies
lambda_hat <- numeric(n_reps) # a (now empty) container
for (i in 1:n_reps) {
y <- rpois(N, lambda = lambda) # simulate a dataset (or "study")
lambda_hat[i] <- mean(y) # compute ml estimate
# https://election.lab.ufl.edu/2024-general-election-turnout/
library(tibble)
turnout_2024 <- tribble(
~state, ~vep_turnout,
"Alabama", 0.5893,
"Alaska", 0.6378,
"Arizona", 0.6360,
"Arkansas", 0.5348,
@carlislerainey
carlislerainey / holland-predictive.R
Created August 29, 2025 17:56
Illustrate predictive distribution with Holland's data
# load packages
library(tidyverse)
library(patchwork)
# load holland's data (once per session)
holland2015 <- crdata::holland2015 |>
glimpse()
# get data for lima
@carlislerainey
carlislerainey / betas-and-baseball.R
Created August 29, 2025 17:38
Betas and Baseball
# load packages
library(tidyverse)
library(Lahman) # data from Lahman's baseball database
# using dbeta shortcut (good!)
ll_fn <- function(theta, y) {
alpha <- theta[1]
beta <- theta[2]
@carlislerainey
carlislerainey / gg-popular.R
Created June 3, 2025 18:42
Find most downloaded ggplot2 extensions
@carlislerainey
carlislerainey / herron-hockey.csv
Created April 10, 2025 09:54
Herron's hockey data
game_id period_id time_of_hit seconds_played_in_season seconds_btw_hits
1 1 25S 25S NA
1 1 2M 8S 128S 103S
1 1 2M 58S 178S 50S
1 1 3M 8S 188S 10S
1 1 4M 13S 253S 65S
1 1 9M 20S 560S 307S
1 1 10M 43S 643S 83S
1 2 3M 27S 1407S 764S
1 2 10M 25S 1825S 418S
@carlislerainey
carlislerainey / 5747-latexit-preable.tex
Created March 28, 2025 10:53
A LaTeXit preamble for my 5747 slides
% custom LaTeXit preamble
% for 5747 slides
% maintained at: https://gist.github.com/carlislerainey/1096250dee536183dfc054c22ac2dd3a
\documentclass[10pt]{article}
% Font: Helvetica (not Neue, compatible with pdfLaTeX)
\usepackage[scaled]{helvet}
\renewcommand{\familydefault}{\sfdefault}