Skip to content

Instantly share code, notes, and snippets.

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(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] %>%
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
@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 -----------------------------------------
@acoppock
acoppock / gelman_and_hill_chapter_3.R
Created September 16, 2018 14:33
Gelman and Hill Chapter 3 Using ggplot and estimatr
# Gelman and Hill 2007: Chapter 3 Regression Examples
# Using ggplot and estimatr
rm(list = ls())
# Uncomment to install
# install.packages("ggplot2")
# install.packges("haven")
# install.pacakges("estimatr")
library(ggplot2)
@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),
rm(list = ls())
library(tidyverse)
library(estimatr)
N <- 1000
dat <-
data.frame(
Y = rnorm(N),
@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")))
@acoppock
acoppock / interaction_power_plot.R
Last active September 13, 2021 19:07
Interaction Power Plot
library(DeclareDesign)
library(tidyverse)
set.seed(343)
design <-
declare_population(N = N) +
declare_potential_outcomes(
# Difference 1: 0.6 - 0.4 = 0.2
Y_Z_1 = draw_binary(prob = 0.4, N = N),