Declares an estimator which generates estimates and associated statistics.

Use of declare_test is identical to use of declare_estimator. Use declare_test for hypothesis testing with no specific inquiry in mind; use declare_estimator for hypothesis testing when you can link each estimate to an inquiry. For example, declare_test could be used for a K-S test of distributional equality and declare_estimator for a difference-in-means estimate of an average treatment effect.

declare_estimator(
  ...,
  handler = label_estimator(model_handler),
  label = "estimator"
)

declare_estimators(
  ...,
  handler = label_estimator(model_handler),
  label = "estimator"
)

label_estimator(fn)

model_handler(
  data,
  ...,
  model = estimatr::lm_robust,
  model_summary = tidy_try,
  term = FALSE
)

Arguments

...

arguments to be captured, and later passed to the handler

handler

a tidy-in, tidy-out function

label

a string describing the step

fn

A function that takes a data.frame as an argument and returns a data.frame with the estimates, summary statistics (i.e., standard error, p-value, and confidence interval), and a term column for labeling coefficient estimates.

data

a data.frame

model

A model function, e.g. lm or glm. By default, the model is the lm_robust function from the estimatr package, which fits OLS regression and calculates robust and cluster-robust standard errors.

model_summary

A model-in data-out function to extract coefficient estimates or model summary statistics, such as tidy or glance. By default, the DeclareDesign model summary function tidy_try is used, which first attempts to use the available tidy method for the model object sent to model, then if not attempts to summarize coefficients using the coef(summary()) and confint methods. If these do not exist for the model object, it fails.

term

Symbols or literal character vector of term that represent quantities of interest, i.e. Z. If FALSE, return the first non-intercept term; if TRUE return all term. To escape non-standard-evaluation use !!.

Value

A function that accepts a data.frame as an argument and returns a data.frame containing the value of the estimator and associated statistics.

Details

declare_estimator is designed to handle two main ways of generating parameter estimates from data.

In declare_estimator, you can optionally provide the name of an inquiry or an objected created by declare_inquiry to connect your estimate(s) to inquiry(s).

The first is through label_estimator(model_handler), which is the default value of the handler argument. Users can use standard modeling functions like lm, glm, or iv_robust. The models are summarized using the function passed to the model_summary argument. This will usually be a "tidier" like broom::tidy. The default model_summary function is tidy_try, which applies a tidy method if available, and if not, tries to make one on the fly.

An example of this approach is:

declare_estimator(Y ~ Z + X, model = lm_robust, model_summary = tidy, term = "Z", inquiry = "ATE")

The second approach is using a custom data-in, data-out function, usually first passed to label_estimator. The reason to pass the custom function to label_estimator first is to enable clean labeling and linking to inquiries.

An example of this approach is:

my_fun <- function(data){ with(data, median(Y[Z == 1]) - median(Y[Z == 0])) }

declare_estimator(handler = label_estimator(my_fun), inquiry = "ATE")

label_estimator takes a data-in-data out function to fn, and returns a data-in-data-out function that first runs the provided estimation function fn and then appends a label for the estimator and, if an inquiry is provided, a label for the inquiry.

Examples

# base design
design <-
  declare_model(
    N = 100,
    female = rbinom(N, 1, 0.5),
    U = rnorm(N),
    potential_outcomes(
     Y ~ rbinom(N, 1, prob = pnorm(0.2 * Z + 0.2 * female + 0.1 * Z * female + U)))
  ) +
  declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0)) + 
  declare_assignment(Z = complete_ra(N, m = 50)) + 
  declare_measurement(Y = reveal_outcomes(Y ~ Z))

# Most estimators are modeling functions like lm or glm.
  
# Default statistical model is estimatr::difference_in_means
design + declare_estimator(Y ~ Z, inquiry = "ATE")
#> 
#> Design Summary
#> 
#> Step 1 (model): declare_model(N = 100, female = rbinom(N, 1, 0.5), U = rnorm(N),     potential_outcomes(Y ~ rbinom(N, 1, prob = pnorm(0.2 * Z +         0.2 * female + 0.1 * Z * female + U)))) 
#> 
#> N = 100 
#> 
#> Added variable: ID 
#>  N_missing N_unique     class
#>          0      100 character
#> 
#> Added variable: female 
#>     0    1
#>    51   49
#>  0.51 0.49
#> 
#> Added variable: U 
#>   min median  mean  max   sd N_missing N_unique
#>  -2.4  -0.15 -0.07 2.01 1.01         0      100
#> 
#> Added variable: Y_Z_0 
#>     0    1
#>    55   45
#>  0.55 0.45
#> 
#> Added variable: Y_Z_1 
#>     0    1
#>    44   56
#>  0.44 0.56
#> 
#> Step 2 (inquiry): declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0)) -------------------
#> 
#> A single draw of the inquiry:
#>  inquiry estimand
#>      ATE     0.11
#> 
#> Step 3 (assignment): declare_assignment(Z = complete_ra(N, m = 50)) ------------
#> 
#> Added variable: Z 
#>     0    1
#>    50   50
#>  0.50 0.50
#> 
#> Step 4 (measurement): declare_measurement(Y = reveal_outcomes(Y ~ Z)) ----------
#> 
#> Added variable: Y 
#>     0    1
#>    49   51
#>  0.49 0.51
#> 
#> Step 5 (estimator): declare_estimator(Y ~ Z, inquiry = "ATE") ------------------
#> 
#> Formula: Y ~ Z 
#> 
#> A single draw of the estimator:
#>  estimator term estimate std.error statistic   p.value   conf.low conf.high df
#>  estimator    Z     0.02 0.1009748 0.1980691 0.8434013 -0.1803813 0.2203813 98
#>  outcome inquiry
#>        Y     ATE
#> 

# lm from base R (classical standard errors assuming homoskedasticity)
design + declare_estimator(Y ~ Z, model = lm, inquiry = "ATE")
#> 
#> Design Summary
#> 
#> Step 1 (model): declare_model(N = 100, female = rbinom(N, 1, 0.5), U = rnorm(N),     potential_outcomes(Y ~ rbinom(N, 1, prob = pnorm(0.2 * Z +         0.2 * female + 0.1 * Z * female + U)))) 
#> 
#> N = 100 
#> 
#> Added variable: ID 
#>  N_missing N_unique     class
#>          0      100 character
#> 
#> Added variable: female 
#>     0    1
#>    44   56
#>  0.44 0.56
#> 
#> Added variable: U 
#>    min median  mean  max   sd N_missing N_unique
#>  -2.27  -0.16 -0.06 2.48 0.95         0      100
#> 
#> Added variable: Y_Z_0 
#>     0    1
#>    47   53
#>  0.47 0.53
#> 
#> Added variable: Y_Z_1 
#>     0    1
#>    40   60
#>  0.40 0.60
#> 
#> Step 2 (inquiry): declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0)) -------------------
#> 
#> A single draw of the inquiry:
#>  inquiry estimand
#>      ATE     0.07
#> 
#> Step 3 (assignment): declare_assignment(Z = complete_ra(N, m = 50)) ------------
#> 
#> Added variable: Z 
#>     0    1
#>    50   50
#>  0.50 0.50
#> 
#> Step 4 (measurement): declare_measurement(Y = reveal_outcomes(Y ~ Z)) ----------
#> 
#> Added variable: Y 
#>     0    1
#>    44   56
#>  0.44 0.56
#> 
#> Step 5 (estimator): declare_estimator(Y ~ Z, model = lm, inquiry = "ATE") ------
#> 
#> Formula: Y ~ Z 
#> 
#> Model:	lm 
#> 
#> A single draw of the estimator:
#>  estimator term estimate std.error  statistic   p.value   conf.low conf.high
#>  estimator    Z    -0.04 0.1002039 -0.3991862 0.6906246 -0.2388513 0.1588513
#>  inquiry
#>      ATE
#> 

# Use lm_robust (linear regression with heteroskedasticity-robust standard errors) 
# from `estimatr` package

design + declare_estimator(Y ~ Z, model = lm_robust, inquiry = "ATE")
#> 
#> Design Summary
#> 
#> Step 1 (model): declare_model(N = 100, female = rbinom(N, 1, 0.5), U = rnorm(N),     potential_outcomes(Y ~ rbinom(N, 1, prob = pnorm(0.2 * Z +         0.2 * female + 0.1 * Z * female + U)))) 
#> 
#> N = 100 
#> 
#> Added variable: ID 
#>  N_missing N_unique     class
#>          0      100 character
#> 
#> Added variable: female 
#>     0    1
#>    53   47
#>  0.53 0.47
#> 
#> Added variable: U 
#>    min median mean  max   sd N_missing N_unique
#>  -4.19   0.11 0.01 2.65 1.13         0      100
#> 
#> Added variable: Y_Z_0 
#>     0    1
#>    46   54
#>  0.46 0.54
#> 
#> Added variable: Y_Z_1 
#>     0    1
#>    41   59
#>  0.41 0.59
#> 
#> Step 2 (inquiry): declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0)) -------------------
#> 
#> A single draw of the inquiry:
#>  inquiry estimand
#>      ATE     0.05
#> 
#> Step 3 (assignment): declare_assignment(Z = complete_ra(N, m = 50)) ------------
#> 
#> Added variable: Z 
#>     0    1
#>    50   50
#>  0.50 0.50
#> 
#> Step 4 (measurement): declare_measurement(Y = reveal_outcomes(Y ~ Z)) ----------
#> 
#> Added variable: Y 
#>     0    1
#>    45   55
#>  0.45 0.55
#> 
#> Step 5 (estimator): declare_estimator(Y ~ Z, model = lm_robust, inquiry = "ATE") 
#> 
#> Formula: Y ~ Z 
#> 
#> Model:	lm_robust 
#> 
#> A single draw of the estimator:
#>  estimator term estimate std.error  statistic   p.value   conf.low conf.high df
#>  estimator    Z    -0.06  0.100326 -0.5980504 0.5511859 -0.2590937 0.1390937 98
#>  outcome inquiry
#>        Y     ATE
#> 

# use `term` to select particular coefficients
design + declare_estimator(Y ~ Z*female, term = "Z:female", model = lm_robust)
#> 
#> Design Summary
#> 
#> Step 1 (model): declare_model(N = 100, female = rbinom(N, 1, 0.5), U = rnorm(N),     potential_outcomes(Y ~ rbinom(N, 1, prob = pnorm(0.2 * Z +         0.2 * female + 0.1 * Z * female + U)))) 
#> 
#> N = 100 
#> 
#> Added variable: ID 
#>  N_missing N_unique     class
#>          0      100 character
#> 
#> Added variable: female 
#>     0    1
#>    47   53
#>  0.47 0.53
#> 
#> Added variable: U 
#>   min median mean  max   sd N_missing N_unique
#>  -2.1  -0.04 -0.1 2.03 1.04         0      100
#> 
#> Added variable: Y_Z_0 
#>     0    1
#>    53   47
#>  0.53 0.47
#> 
#> Added variable: Y_Z_1 
#>     0    1
#>    45   55
#>  0.45 0.55
#> 
#> Step 2 (inquiry): declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0)) -------------------
#> 
#> A single draw of the inquiry:
#>  inquiry estimand
#>      ATE     0.08
#> 
#> Step 3 (assignment): declare_assignment(Z = complete_ra(N, m = 50)) ------------
#> 
#> Added variable: Z 
#>     0    1
#>    50   50
#>  0.50 0.50
#> 
#> Step 4 (measurement): declare_measurement(Y = reveal_outcomes(Y ~ Z)) ----------
#> 
#> Added variable: Y 
#>     0    1
#>    49   51
#>  0.49 0.51
#> 
#> Step 5 (estimator): declare_estimator(Y ~ Z * female, term = "Z:female", model = lm_robust) 
#> 
#> Formula: Y ~ Z * female 
#> 
#> Model:	lm_robust 
#> 
#> A single draw of the estimator:
#>  estimator     term    estimate std.error  statistic   p.value   conf.low
#>  estimator Z:female -0.08116883 0.2044955 -0.3969224 0.6923057 -0.4870891
#>  conf.high df outcome
#>  0.3247515 96       Y
#> 

# Use glm from base R
design + declare_estimator(
  Y ~ Z + female,
  family = "gaussian",
  inquiry = "ATE",
  model = glm
)
#> 
#> Design Summary
#> 
#> Step 1 (model): declare_model(N = 100, female = rbinom(N, 1, 0.5), U = rnorm(N),     potential_outcomes(Y ~ rbinom(N, 1, prob = pnorm(0.2 * Z +         0.2 * female + 0.1 * Z * female + U)))) 
#> 
#> N = 100 
#> 
#> Added variable: ID 
#>  N_missing N_unique     class
#>          0      100 character
#> 
#> Added variable: female 
#>     0    1
#>    53   47
#>  0.53 0.47
#> 
#> Added variable: U 
#>    min median  mean  max sd N_missing N_unique
#>  -2.77  -0.16 -0.16 1.96  1         0      100
#> 
#> Added variable: Y_Z_0 
#>     0    1
#>    54   46
#>  0.54 0.46
#> 
#> Added variable: Y_Z_1 
#>     0    1
#>    50   50
#>  0.50 0.50
#> 
#> Step 2 (inquiry): declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0)) -------------------
#> 
#> A single draw of the inquiry:
#>  inquiry estimand
#>      ATE     0.04
#> 
#> Step 3 (assignment): declare_assignment(Z = complete_ra(N, m = 50)) ------------
#> 
#> Added variable: Z 
#>     0    1
#>    50   50
#>  0.50 0.50
#> 
#> Step 4 (measurement): declare_measurement(Y = reveal_outcomes(Y ~ Z)) ----------
#> 
#> Added variable: Y 
#>     0    1
#>    56   44
#>  0.56 0.44
#> 
#> Step 5 (estimator): declare_estimator(Y ~ Z + female, family = "gaussian", inquiry = "ATE",     model = glm) 
#> 
#> Formula: Y ~ Z + female 
#> 
#> Model:	glm 
#> 
#> A single draw of the estimator:
#>  estimator term  estimate std.error statistic   p.value    conf.low conf.high
#>  estimator    Z 0.1149719  0.096784  1.187922 0.2377645 -0.07472126  0.304665
#>  inquiry
#>      ATE
#> 

# If we use logit, we'll need to estimate the average marginal effect with 
# margins::margins. We wrap this up in function we'll pass to model_summary

library(margins) # for margins
#> Error in library(margins): there is no package called ‘margins’
library(broom) # for tidy

tidy_margins <- function(x) {
  tidy(margins(x, data = x$data), conf.int = TRUE)
}

design +
  declare_estimator(
    Y ~ Z + female,
    model = glm,
    family = binomial("logit"),
    model_summary = tidy_margins,
    term = "Z"
  ) 
#> Error in margins(x, data = x$data): could not find function "margins"

# Multiple estimators for one inquiry

two_estimators <-
  design +
  declare_estimator(Y ~ Z,
                    model = lm_robust,
                    inquiry = "ATE",
                    label = "OLS") +
  declare_estimator(
    Y ~ Z + female,
    model = glm,
    family = binomial("logit"),
    model_summary = tidy_margins,
    inquiry = "ATE",
    term = "Z",
    label = "logit"
  )

run_design(two_estimators)
#> Error: Error in step 6 (logit):
#> 	Error in margins(x, data = x$data): could not find function "margins"

# Declare estimator using a custom handler

# Define your own estimator and use the `label_estimator` function for labeling
# Must have `data` argument that is a data.frame
my_dim_function <- function(data){
  data.frame(estimate = with(data, mean(Y[Z == 1]) - mean(Y[Z == 0])))
}

design + declare_estimator(
  handler = label_estimator(my_dim_function),
  inquiry = "ATE"
)
#> 
#> Design Summary
#> 
#> Step 1 (model): declare_model(N = 100, female = rbinom(N, 1, 0.5), U = rnorm(N),     potential_outcomes(Y ~ rbinom(N, 1, prob = pnorm(0.2 * Z +         0.2 * female + 0.1 * Z * female + U)))) 
#> 
#> N = 100 
#> 
#> Added variable: ID 
#>  N_missing N_unique     class
#>          0      100 character
#> 
#> Added variable: female 
#>     0    1
#>    49   51
#>  0.49 0.51
#> 
#> Added variable: U 
#>   min median  mean  max   sd N_missing N_unique
#>  -3.4  -0.17 -0.09 2.04 1.11         0      100
#> 
#> Added variable: Y_Z_0 
#>     0    1
#>    51   49
#>  0.51 0.49
#> 
#> Added variable: Y_Z_1 
#>     0    1
#>    45   55
#>  0.45 0.55
#> 
#> Step 2 (inquiry): declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0)) -------------------
#> 
#> A single draw of the inquiry:
#>  inquiry estimand
#>      ATE     0.06
#> 
#> Step 3 (assignment): declare_assignment(Z = complete_ra(N, m = 50)) ------------
#> 
#> Added variable: Z 
#>     0    1
#>    50   50
#>  0.50 0.50
#> 
#> Step 4 (measurement): declare_measurement(Y = reveal_outcomes(Y ~ Z)) ----------
#> 
#> Added variable: Y 
#>     0    1
#>    53   47
#>  0.53 0.47
#> 
#> Step 5 (estimator): declare_estimator(inquiry = "ATE", handler = label_estimator(my_dim_function)) 
#> 
#> A single draw of the estimator:
#>  estimator estimate inquiry
#>  estimator     0.02     ATE
#>