Skip to content

Instantly share code, notes, and snippets.

@acoppock
acoppock / gg_robust_ses.R
Created May 12, 2018 15:11
Add Robust SEs to a ggplot
library(fabricatr)
library(randomizr)
library(estimatr)
library(ggplot2)
dat <- fabricate(N = 200,
Z = complete_ra(N, conditions = c("Control", "Treatment")),
X = sample(c("A", "B"), size = N, replace = TRUE),
Y = (X == "A") + (Z == "Treatment") + rnorm(N, sd = 1 + (Z == "Treatment")))
rm(list = ls())
library(tidyverse)
library(estimatr)
N <- 1000
dat <-
data.frame(
Y = rnorm(N),
@acoppock
acoppock / list_experiment_declaration.r
Created September 12, 2018 21:16
"Shy Trump" list experiment design declaration
library(DeclareDesign)
proportion_shy <- .06
population <- declare_population(
N = 5000,
# true trump vote (unobservable)
truthful_trump_vote = draw_binary(.45, N),
# shy voter (unobservable)
shy = draw_binary(proportion_shy, N),
@acoppock
acoppock / blockTools_and_randomizr
Created October 23, 2018 21:14
randomizr <3's blockTools
# This script shows how to make blocks with blockTools, then randomize with randomizr
# Alex Coppock
# declaredesign.org
library(randomizr)
library(blockTools)
library(tidyverse)
# Prepare data with dplyr + tidyr -----------------------------------------
rm(list = ls())
library(tidyverse)
fate <-
function(stopping_rule) {
# the order you meet people
the_shuffle = sample(1:100)
# everybody you've met
the_exes <- the_shuffle[1:stopping_rule]
# how good was the best so far
library(tidyverse)
library(rvest)
pew_tables <-
read_html("http://www.pewforum.org/fact-sheet/changing-attitudes-on-gay-marriage/") %>%
html_nodes("table") %>%
html_table
gg_df <-
pew_tables[-1] %>%
library(rvest)
library(tidyverse)
library(stringr)
library(lubridate)
library(ggrepel)
pres_terms <-
read_html("https://www.presidentsusa.net/presvplist.html") %>%
html_nodes("table") %>%
pluck(1) %>%
library(DeclareDesign)
library(tidyverse)
library(ggridges)
design <-
declare_population(
N = 1000,
X1 = rbinom(N, 1, .5),
X2 = rbinom(N, 1, .5),
X3 = rbinom(N, 1, .5),
rm(list = ls())
library(DeclareDesign)
library(tidyverse)
library(reshape2)
# Declare the design ------------------------------------------------------
ATE <- 0.0
@acoppock
acoppock / ATTandCACE.R
Created April 29, 2020 15:20
ATTandCACE
library(DeclareDesign)
library(tidyverse)
design <-
declare_population(N = 50,
type = sample(c("complier", "nevertaker"), N, replace = TRUE)) +
declare_potential_outcomes(D ~ Z * (type == "complier")) +
declare_potential_outcomes(Y ~ 5 * Z + rnorm(N)) +
declare_assignment(prob = 0.5) +
declare_estimand(CACE = mean(Y_Z_1 - Y_Z_0),