This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from joblib import Parallel, delayed | |
import numpy as np | |
import pandas as pd | |
class LinearMediation: | |
def __init__(self): | |
pass | |
def fit(self, X, W, y, store=True): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\documentclass[% | |
article, | |
% beamer, | |
beameroptions={ignorenonframetext,14pt}, | |
articleoptions={a4paper,12pt}, | |
also={trans,handout,article} | |
]{beamerswitch} | |
\handoutlayout{nup=3plus,border=1pt} | |
\articlelayout{maketitle,frametitles=none} | |
\mode<article>{\usepackage[hmargin=2cm,vmargin=2cm]{geometry}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
from scipy.stats import norm | |
from sklearn.model_selection import cross_val_predict, KFold | |
# learners | |
from xgboost import XGBRegressor | |
from glum import GeneralizedLinearRegressorCV | |
from sklearn.kernel_ridge import KernelRidge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(estimatr) | |
data(auto) | |
# %% FWL regression coefficient | |
auto$ytil = lm(price ~ displacement, auto)$resid | |
auto$x2til = lm(weight ~ displacement, auto)$resid | |
(fwlest = lm_robust(ytil ~ x2til, auto, se_type = "HC0") | |
%>% summary %>% .$coefficients %>% .[2, 1:2]) | |
# %% | |
(fullest = | |
lm_robust(price ~ weight + displacement, auto, se_type = "HC0") %>% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from scipy.linalg import lstsq | |
np.random.seed(42) | |
# %% | |
def ols(X, y, vcov = 'HC1', driver = 'gelsy'): | |
""" | |
Fast, minimal implementation of least squares regression with robust SEs | |
Args: | |
X: n X p array of covariates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% | |
pacman::p_load(knitr, tidyverse, DeclareDesign, glmnet) | |
set.seed(42) | |
# %% estimator functions | |
p_hacker = function(data) { | |
fit_1 = lm_robust(Y ~ Z + X1, data = data) | |
fit_3 = lm_robust(Y ~ Z + X1 + X2, data = data) | |
fit_2 = lm_robust(Y ~ Z + X2 + X3 + X4, data = data) | |
fit_4 = lm_robust(Y ~ Z + X3 + X4 + X5 + X6 + X7 + X8 + X9, data = data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pacman::p_load(synthdid, ebal, glue, augsynth, MCPanel, glue) | |
# needs https://github.com/apoorvalal/ebal - solves ebal problem in torch - far more stable than old version | |
# remotes::install_github("apoorvalal/ebal") | |
# %% simulator for panel balancing | |
#' @param n number of units | |
#' @param t number of time periods | |
#' @param parallel_trends boolean for parallel trends | |
#' @param random_assignment boolean for random assignment of treatment | |
#' @param σ noise level in mapping from factor to outcome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% # obs lalonde data from Kline paper - init housekeeping | |
libreq(data.table, fixest, rio) | |
cps3 = import("cps3re74.dta") %>% setDT() %>% na.omit() | |
setnames(cps3, c("re78", "treat"), c("y", "W")); | |
xs = setdiff(colnames(cps3), c("y", 'W')) | |
W = cps3$W %>% as.matrix(); Y = cps3$y %>% as.matrix() | |
X = cbind(1, cps3[, ..xs]) %>% as.matrix() | |
X1 = X[W==1,]; X0 = X[W==0,] | |
N = length(W); N_t = sum(W) | |
# %% first way - KOB / kline - page 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(CVXR); library(data.table) | |
# %% functions | |
# reshape panel data from long to wide for factor models / outcomes | |
panelMatrices = function(dt, unit_id, time_id, treat, outcome) { | |
dt = as.data.table(dt) | |
# function to extract first column, convert it to rownames for a matrix | |
matfy = function(X) { | |
idnames = as.character(X[[1]]) | |
X2 = as.matrix(X[, -1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Compute Neyman allocation propensity scores for inference-optimal treatment assignment in data table [very fast] | |
#' @param df data.table | |
#' @param y outcome name | |
#' @param w treatment name | |
#' @param x covariate names (must all be discrete) | |
#' @return data.table with strata level conditional means, variances, propensity scores, | |
#' and neyman allocation propensities. | |
#' @export | |
neymanAllocation = function(df, y, w, x){ | |
df1 = copy(df); N = nrow(df1) |
NewerOlder