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,
horvitz_thompson(). In addition, we include two functions for linear regression estimators,
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).
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("http://r.declaredesign.org", "https://cloud.r-project.org"))
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)
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'))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.