Warning: the CRAN version of estimatr has the incorrect variance for weighted, “CR2” variance estimates. If you want to use “CR2” variance estimation with weights, please download the development version of the package using the instructions below.

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 two functions for linear regression estimators, lm_robust() and lm_lin(). 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:

Warning: the CRAN version of estimatr has the incorrect variance for weighted, “CR2” variance estimates. If you want to use “CR2” variance estimation with weights, please download the development version instead.

install.packages("estimatr")

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("http://r.declaredesign.org", "https://cloud.r-project.org"))

### Easy to use

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

library(estimatr)

# sample data from cluster-randomized experiment
library(fabricatr)
library(randomizr)
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)
summary(res_cl)
#>
#> 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.180      0.200    0.428   -0.777    0.417 3.38
#> z              0.424      0.236    0.118   -0.142    0.990 6.54
#>
#> Multiple R-squared:  0.0419 ,    Adjusted R-squared:  0.0321
#> F-statistic: 4.28 on 1 and 98 DF,  p-value: 0.0411

# matched-pair design learned from blocks argument
data(sleep)
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)
library(lmtest)
library(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).