Sebastian Garcia-Torres/Methods Lab

Methods and code

Methods overview

I am collecting here the methods I have used in my own projects and as a research assistant. Each chapter gives a quick overview of the goal, the gap and the implementation, and includes the code together with the data it runs on, so that anyone interested in working with these can have a smooth start.

My goals with this site are two. The first is to keep a collection of the methods I have learned. The second is to give an easy, clear start to people who are struggling with the same kind of challenge: both methods here are described in papers that assume you already know the surrounding literature, and have implementations that assume you already know the method.

RensembleHTE · grf · xgboost

Heterogeneous treatment effects

For: finding out who a treatment helps, in a randomised trial, without data-mining the answer.

The estimands are Chernozhukov, Demirer, Duflo and Fernández-Val's: the best linear predictor, sorted group effects and classification analysis, estimated fold by fold, pooled by median over 180 sample splits, and accompanied by a simulation check of the pooled test's size.

On the simulated trial: true average effect -0.019, estimated 0.009 (p = 0.93). Heterogeneity is strongly present: top minus bottom quartile 1.67 (p < 0.0001), against a true gap of 1.72.

Open

PythonXGBoost · SMOTENC · scikit-learn

Predicting a rare outcome

For: predicting a rare and sensitive survey answer that many respondents skipped, when the predictions are aggregated into shares rather than used for decisions about individuals.

The page goes through four steps, each with the experiment that motivates it: imputing missing features without the outcome, handling the imbalance, setting the threshold, and validating the predicted shares. It also examines whether oversampling improves the predictions and how performance changes as the outcome becomes rarer.

On the simulated survey, 2.6% of respondents report carrying a weapon, against 3.8% of the students who skipped the question. Filling in the gaps with recalibrated probabilities reduces the error in the overall share from 0.46 to 0.22 percentage points, averaged over 5 simulated surveys.

Open

How to use these pages

The goal box at the top of each page states what the method is for, which should be enough to decide whether it fits a given problem. The code is folded so that the text leads, and opens in place. Every number in the text, in the tables and in the figures is read from the results files the scripts write, so the pages cannot drift from the code.

Each demo simulates its own data with a known answer. That is deliberate: real data cannot show whether an estimator recovered the truth, and an implementation that cannot recover a planted truth should not be pointed at data where nobody knows it.

Run them

# from the repository root
Rscript code/ensemble_hte_demo.R            # ~11 min on 4 cores
python  code/imbalanced_prediction_demo.py  # ~28 min
Rscript code/mc_size_check.R                # size and power of the pooled test, ~30 min

R needs ranger, grf, glmnet, xgboost, rpart, sandwich and jsonlite. Python needs numpy, pandas, scikit-learn, xgboost and imbalanced-learn. Both write their results to results/ and print the tables shown on these pages.

Comments are welcome

If you have comments or suggestions, or find an error in any of the methods, I am happy to hear from you at sgarciatorres@hks.harvard.edu. I intend to keep developing this site, and these skills, as I go.