Skip to content

Instantly share code, notes, and snippets.

@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;
}
@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 =
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 / 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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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) +
@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)
@tvladeck
tvladeck / gradient_heatmap.R
Last active February 19, 2019 21:00
heatmap of table
library(RColorBrewer)
library(fields) #to use designer.colors
library(reshape2)
library(dplyr)
gradient_heatmap <-
function(
df,
num_percentiles = 10,
@tvladeck
tvladeck / repel_plot.R
Last active February 19, 2019 21:00
repel plot from contingency table
library(ca)
library(ggplot2)
library(ggrepel)
library(stringr)
library(scales)
library(magrittr)
library(factoextra)
repel_ca_from_table <-
function(
@tvladeck
tvladeck / max-diff-power-analysis.R
Created August 21, 2018 16:37
Computing power of covariate in max diff experience
library(foreach)
library(doParallel)
library(tidyverse)
library(magrittr)
registerDoParallel(40)
runs <- 100
N_test <- c(250, 300, 350, 400, 500, 600) # sample in each group