Skip to content

Instantly share code, notes, and snippets.

@apoorvalal
apoorvalal / omnibusTestsOfHeterogeneity.R
Created August 12, 2022 02:33
Omnibus tests of treatment effect heterogeneity with linear and nonlinear hetfx estimators.
rm(list = ls())
libreq(data.table, estimatr,
grf,
DoubleML, mlr3, mlr3learners, dmlUtils)
# %% linear effect heterogeneity
dfm_omnibus = function(y, w, X){
n1 = sum(w); n0 = sum(1-w); K = ncol(X)
# separate outcome models
m1 = lm.fit(X[w==1,], y[w==1]); m0 = lm.fit(X[w==0,], y[w==0])
@apoorvalal
apoorvalal / simple_text_parse.py
Last active May 5, 2022 19:57
example of text parsing using gettysburg address.
# %%
from bs4 import BeautifulSoup
from urllib import request
import re
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import word_tokenize
from collections import Counter
@apoorvalal
apoorvalal / Thompson.R
Created March 18, 2022 17:23
thompson sampling minimal example
rm(list = ls())
libreq(data.table, ggplot2)
set.seed(42)
# %%
thompson = function(n, K, reward_probs){
# init choices and reward vectors
choices <- rewards <- rep(NA, n)
# n+1 X K*2 matrix of S and F counts successes stored in first K, failures in next K
s_f = matrix(NA, nrow = n+1, K * 2) # +1 to accommodate last update step
@apoorvalal
apoorvalal / HC0_4_manual.R
Created February 14, 2022 18:29
manual implementation of HC0-HC4 robust standard errors for reference
library(car); library(sandwich)
data(auto)
# %%
fit = lm(price ~ mpg + weight, data = auto)
X = model.matrix(fit); n = nrow(X); k = ncol(X)
e = resid(fit)
A = crossprod(X)
H = X %*% solve(A) %*% t(X); h_ii = diag(H)
# cla = (t(e) %*% e)/(n-k) %*% solve(t(X) %*% X)
CLA = as.numeric(crossprod(e)/(n-k)) * solve(A)
@apoorvalal
apoorvalal / deltaMethodExamples.R
Created February 13, 2022 18:11
Hypothesis testing on linear and nonlinear combinations of coefficients using the Delta method
# %%
library(car); library(sandwich)
m1 <- lm(time ~ t1 + t2, data = Transact)
m1 |> summary()
vcovmat = vcovHC(m1)
# %%
deltaMethod(m1, "t1/t2")
deltaMethod(m1, "t1/t2", vcov = vcovmat)
@apoorvalal
apoorvalal / OaxacaBlinderATTEstimation.R
Created February 12, 2022 08:11
Implementation of OB treatment effect estimators
rm(list = ls())
libreq(data.table, fixest, rio, ggplot2, ebal)
# %%
cps3 = import("cps3re74.dta") |> setDT()
cps3 = cps3 |> na.omit()
setnames(cps3, c("re78", "treat"), c("y", "d"))
xs = c("age", "age2", "ed", "black", "hisp", "married", "nodeg", "re74", "re75")
@apoorvalal
apoorvalal / eur_leagues_competitiveness.R
Last active December 18, 2021 21:06
construct measures of competitiveness of european leagues using match level data from https://github.com/jalapic/engsoccerdata
# %% ####################################################
rm(list = ls())
library(LalRUtils)
LalRUtils::libreq(ggplot2, data.table, fixest, magrittr,
patchwork, IRdisplay, did, panelView, plotly)
theme_set(lal_plot_theme())
options(repr.plot.width=12, repr.plot.height=9)
options(ggplot2.discrete.fill = RColorBrewer::brewer.pal(7, "Set2"))
options(ggplot2.discrete.colour = RColorBrewer::brewer.pal(7, "Set2"))
options(ggplot2.continuous.fill = "viridis"); options(ggplot2.continuous.colour = "viridis")

Figlet for code signposting

  • Data prep code is long, horrible, and difficult to wrap in functions
  • Typical data-scraping/cleaning scripts end up being thousands of lines long
  • Parsing these scripts are a chore for future-you / other people
  • Do yourself/others a favour by clearly signposting your code (via the minimap feature in most modern editors)

example of minimap in Atom

  • To do this, use the trusty old unix tool figlet (available on all
@apoorvalal
apoorvalal / event_study_comparison.R
Created August 23, 2021 22:50
comparing estimators for event studies
rm(list = ls())
library(LalRUtils)
libreq(data.table, fixest, did2s, ggplot2)
theme_set(lal_plot_theme())
# %%
simulate = function() {
# dimensions
N = 250
T = 35
#%%
from google.cloud import translate_v2
from googletrans import Translator
import os
from pathlib import Path
import pandas as pd
# %%
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] ="/home/alal/keys/sandbox-7f8884d01b79.json"
translate_client = translate_v2.Client()