Lin's covariate-adjusted estimator and Neyman/HC2 robust standard errors for randomized experiments — one fast function, design-based inference.
Input · what goes in
Treatment Z, outcome Y and pre-treatment covariates X from a randomized experiment.
Show data format & exampleHide example
| Y | Z | X (age) |
|---|---|---|
| 5.2 | 1 | 34 |
| 4.1 | 0 | 51 |
| 6.0 | 1 | 29 |
| 3.8 | 0 | 60 |
Pipeline · the recipe
↑ Click any step in the diagram to read its logic, code, assumptions & discussion.
Assemble the experiment
Data preparation — shapes the raw inputs into what the estimator expects.
Build a small completely-randomized experiment with one pre-treatment covariate.
# Install: install.packages("estimatr")
library(estimatr)
dat <- data.frame(Y = rnorm(100), Z = rbinom(100, 1, .5), X = rnorm(100))
- No comments on this step yet — be the first.
Log in to comment on this step.
Fit Lin's estimator
The core estimate — where the causal quantity itself is computed.
lm_lin centers covariates, adds the full treatment-by-covariate interactions, and uses HC2 standard errors.
lm_lin(Y ~ Z, covariates = ~ X, data = dat)
- No comments on this step yet — be the first.
Log in to comment on this step.
Output · what you get
Result figure rendered by StatsOtter from the package's documented example — unofficial community showcase; all credit to the original authors.
Result · the numbers
⚠️ Unofficial community showcase of estimatr (docs). Not affiliated with the authors — all credit to the DeclareDesign team (Blair, Cooper, Coppock, Humphreys & Sonnet); this summarizes public documentation.
What it does. estimatr is the canonical software for the design-based estimators central to Peng Ding's work. lm_lin() implements Lin's (2013) covariate adjustment (treatment fully interacted with centered covariates); lm_robust() and difference_in_means() give the Neyman difference-in-means with conservative HC2 variance.
How it works. lm_lin() centers the covariates, adds all treatment×covariate interactions, and reads the ATE off the treatment coefficient with HC2 standard errors — the estimator Ding's textbook derives as never-harmful in large samples. Everything is implemented in C++ for speed.
Assumptions. Complete randomization (or a known design), SUTVA, fixed pre-treatment covariates; the HC2 variance is finite-sample conservative.
Implements methods Prof. Ding is known for; package authored by the DeclareDesign team.
What you get — The covariate-adjusted ATE (the Z coefficient) with an HC2 robust standard error and confidence interval.
Example output
Estimate Std. Error t value Pr(>|t|) CI Lower CI Upper DF
(Intercept) 0.0123 0.1402 0.088 0.930 -0.266 0.290 96
Z -0.0457 0.1989 -0.230 0.819 -0.440 0.349 96
X_c 0.0712 0.1421 0.501 0.617 -0.211 0.353 96
Z:X_c 0.0331 0.2011 0.165 0.870 -0.366 0.432 96

Discussion (0)
Log in to join the discussion.