Skip to content

Instantly share code, notes, and snippets.

@acoppock
acoppock / noncompliance_diagnosis.R
Created September 7, 2023 15:18
power analysis for an experiment with one-sided noncompliance
library(DeclareDesign)
library(tidyverse)
# Declare the design ----------------------------------------
compliance_rate <- 0.5
design <-
declare_model(
N = 1000,
@acoppock
acoppock / power_depends_on_arms
Created June 27, 2022 13:37
DeclareDesign simulation to see how power changes as we increase the number of arms (with corrections)
library(DeclareDesign)
library(tidyverse)
library(geomtextpath)
design_1 <-
declare_model(N = 1000,
U = rnorm(N),
Y_Z_0 = U,
Y_Z_1 = U + 0.2) +
declare_assignment(Zk = complete_ra(N = N, num_arms = k)) +
library(simpr)
library(DeclareDesign)
library(tidyverse)
# base ------------------------------------------------------
sims <- 500
ns <- seq(100, 300, by = 50)
p.values <- matrix(NA, nrow = sims, ncol = length(ns))
library(DeclareDesign)
library(tidyverse)
design <-
declare_model(
N = 100,
type =
rep(c("Always-Taker", "Never-Taker", "Complier", "Defier"),
c(0.2, 0.2, 0.6, 0.0)*N),
U = rnorm(N),
@acoppock
acoppock / conjoint_dd.R
Created October 6, 2020 15:58
Some code to simulate a conjoint
# Load packages
library(tidyverse)
library(DeclareDesign)
# helper
Y_function <- function(data) {
data %>%
group_by(pair) %>%
mutate(Y = if_else(E == max(E), 1, 0)) %>%
ungroup
@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),
rm(list = ls())
library(tidyverse)
library(ggrepel)
covid <-
read_csv(file = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv")
covid <-
covid %>% filter(`Country/Region` != "Cruise Ship")
covid_long <-
rm(list = ls())
library(DeclareDesign)
library(tidyverse)
library(reshape2)
# Declare the design ------------------------------------------------------
ATE <- 0.0
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),
@acoppock
acoppock / stata_challenge.R
Created February 25, 2019 17:29
stata_challenge
# How to make this plot in Stata?
library(tidyverse)
library(estimatr)
library(randomizr)
# make some fake data
dat <- tibble(Y = rnorm(100),
Z = complete_ra(100, conditions = c("Control", "Treament")))