Re-use intermediate values across inquiries?

Is it possible to re-use intermediate values across inquiries?

For example, I want to do something like this:

ate_machine <- function(data) {
  ate <- with(data, mean(Y[Z==1] - Y[Z==0]))
  data.frame(
    term = c("ate", "ate2")
    estimate = c(ate, ate / 2)
  )
}

ate <- declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0))
ate2 <- declare_inquiry(ATE2 = mean(Y_Z_1 - Y_Z_0) / 2)

declare_estimator(handler=label_estimator(ate_machine), inquiry=c(ate, ate2), term=c("ate", "ate2"))

Is something like this possible?

Sure is!

library(DeclareDesign)

ate_machine <- function(data) {
  ate <- with(data, mean(Y[Z==1] - Y[Z==0]))
  data.frame(
    term = c("ate", "ate2"),
    estimate = c(ate, ate / 2)
  )
}


design <-
  declare_model(N = 100, U = rnorm(N), potential_outcomes(Y  ~ Z + U)) +
  declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0),
                  ATE2 = ATE / 2) +
  declare_assignment(Z = complete_ra(N)) +
  declare_measurement(Y = reveal_outcomes(Y ~ Z)) +
  declare_estimator(handler=label_estimator(ate_machine), 
                    inquiry=c("ATE", "ATE2"))


run_design(design)
1 Like

This works great, thanks Alex!