estimatr: Fast Estimators for Design-Based Inference

CRAN Status Travis-CI Build Status AppVeyor Build Status Coverage Status

Technical papers and textbooks demand complex estimation strategies that are often difficult to implement, even for scientists who are expert coders. The result is slow code copied and pasted from the internet, where the result is taken on faith.

estimatr provides a small set of commonly-used estimators (methods for estimating quantities of interest like treatment effects or regression parameters), using C++ for speed, and implemented in R with simple, accessible syntax. We include two functions that implement means estimators, difference_in_means() and horvitz_thompson(). In addition, we include three functions for linear regression estimators, lm_robust(), lm_lin(), and iv_robust(). In each case, scientists can choose an estimator to reflect cluster-randomized, block-randomized, and block-and-cluster-randomized designs. The Getting Started Guide describes each estimator provided by estimatr and how it can be used in your analysis.

Fast estimators also enable fast simulation of research designs to learn about their properties (see DeclareDesign).

Installing estimatr

To install the latest stable release of estimatr, please ensure that you are running version 3.3 or later of R and run the following code:


If you would like to use the latest development release of estimatr, please ensure that you are running version 3.3 or later of R and run the following code:

install.packages("estimatr", dependencies = TRUE,
                 repos = c("", ""))

Easy to use

Once the package is installed, getting appropriate estimates and standard errors is now both fast and easy.


# sample data from cluster-randomized experiment
dat <- fabricate(
  N = 100,
  y = rnorm(N),
  clusterID = sample(letters[1:10], size = N, replace = TRUE),
  z = cluster_ra(clusterID)

# robust standard errors
res_rob <- lm_robust(y ~ z, data = dat)

# cluster robust standard errors
res_cl <- lm_robust(y ~ z, data = dat, clusters = clusterID)
#> Call:
#> lm_robust(formula = y ~ z, data = dat, clusters = clusterID)
#> Standard error type:  CR2 
#> Coefficients:
#>             Estimate Std. Error Pr(>|t|) CI Lower CI Upper   DF
#> (Intercept)    0.269      0.164     0.20   -0.255    0.793 2.99
#> z             -0.422      0.250     0.14   -1.027    0.182 6.30
#> Multiple R-squared:  0.041 , Adjusted R-squared:  0.0312 
#> F-statistic: 4.18 on 1 and 98 DF,  p-value: 0.0435

# matched-pair design learned from blocks argument
res_dim <- difference_in_means(extra ~ group, data = sleep, blocks = ID)

The Getting Started Guide has more examples and uses, as do the reference pages. The Mathematical Notes provide more information about what each estimator is doing under the hood.

Fast to use

Getting estimates and robust standard errors is also faster than it used to be. Compare our package to using lm() and the sandwich package to get HC2 standard errors. More speed comparisons are available here.

# example code
# estimatr
lm_robust(y ~ x1 + x2 + x3 + x4, data = dat)

# usual specification (lm + sandwich)
lm_out <- lm(y ~ x1 + x2 + x3 + x4, data = dat)
coeftest(lm_out, vcov = vcovHC(lm_out, type = 'HC2'))

This project is generously supported by a grant from the Laura and John Arnold Foundation and seed funding from Evidence in Governance and Politics (EGAP).