Skip to content

Instantly share code, notes, and snippets.

@tvladeck
tvladeck / income-inequality.R
Created March 18, 2019 16:42
income inequality analysis
# from IPUMS
# includes 1950 forward
# includes individual income
# sex
# age
raw_data <- data.table::fread("~/Downloads/usa_00008.csv")
library(tidyverse)
less_uncertainty_smaller_step <-
.25 * (1:1000 %>% map(~ run_simulation(initial = 1 - 0.1)) %>% unlist %>% mean) +
.5 * (1:1000 %>% map(~ run_simulation(initial = 1)) %>% unlist %>% mean) +
.25 * (1:1000 %>% map(~ run_simulation(initial = 1 + 0.1)) %>% unlist %>% mean)
greater_uncertainty_bigger_step <-
.05 * (1:1000 %>% map(~ run_simulation(initial = 1 - 3, step_size = 0.5)) %>% unlist %>% mean) +
.1 * (1:1000 %>% map(~ run_simulation(initial = 1 - 2, step_size = 0.5)) %>% unlist %>% mean) +
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@tvladeck
tvladeck / economist-check.R
Created July 18, 2020 12:56
economist forecast consistency check
p_over_time =
read_csv("https://cdn.economistdatateam.com/us-2020-forecast/data/president/electoral_college_probability_over_time.csv") %>%
# convert win probabilites to logit scale
mutate(win_prob_logit = boot::logit(win_prob)) %>%
# look only at one side
filter(party == "democratic")
# take the variance of the differences
set.seed(1)
margins <- c()
for(i in 1:1000) {
sim <- c()
for(j in 1:10) {
experts <- rbeta(1000, 1.5, 1)
expert_votes <- runif(1000) < experts
uninformed <- runif(1000) < .5
sim[j] = sum(expert_votes) + sum(uninformed) - sum(!expert_votes)-sum(!uninformed)
@tvladeck
tvladeck / algo-fairness.R
Created April 28, 2021 19:38
simulation of algorithmic fairness
library(tidyverse)
set.seed(1)
sims <- map(1:200, function(x) {
intercept = -runif(1)/2
beta_male = runif(1)/4
beta_smoker = runif(1)/4
dat =
@tvladeck
tvladeck / vaccine_threshold_price.stan
Created May 3, 2021 21:57
vaccine threshold price model
data {
int n_respondents;
int n_choices;
int n_vaccines;
int responses[n_choices];
int respondent[n_choices];
int vaccine[n_choices];
vector[n_choices] money;
}