Skip to content

Commit

Permalink
tidy_step is dead, long live declare_step with no default handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
acoppock committed Jun 19, 2018
1 parent bdbf9a9 commit dd3f055
Show file tree
Hide file tree
Showing 22 changed files with 43 additions and 200 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Expand Up @@ -40,7 +40,6 @@ export(reveal_outcomes_handler)
export(run_design)
export(simulate_design)
export(tidy_estimator)
export(tidy_step)
importFrom(estimatr,difference_in_means)
importFrom(fabricatr,add_level)
importFrom(fabricatr,fabricate)
Expand Down
6 changes: 4 additions & 2 deletions R/construct_design.R
Expand Up @@ -38,11 +38,13 @@
#' my_estimand <- declare_estimand(ATE = mean(Y_Z_1 - Y_Z_0))
#'
#' my_estimator <- declare_estimator(Y ~ Z, estimand = my_estimand)
#'
#' my_mutate <- declare_step(dplyr::mutate, noise_sq = noise^2)
#'
#' my_reveal <- declare_reveal()
#'
#' design <- my_population + my_potential_outcomes + my_sampling +
#' my_estimand + tidy_step(dplyr::mutate(noise_sq = noise^2)) +
#' my_estimand + my_mutate +
#' my_assignment + my_reveal + my_estimator
#'
#' design
Expand Down Expand Up @@ -93,7 +95,7 @@
} else{

if (!inherits(rhs, "dd") & !inherits(rhs, "function")) {
stop("The right hand side does not appear to be a DeclareDesign object. Can you wrap the step with `tidy_step()`?", call. = FALSE)
stop("The right hand side does not appear to be a DeclareDesign object or a function", call. = FALSE)
}

if (inherits(lhs, "design")) {
Expand Down
8 changes: 5 additions & 3 deletions R/declare_step.R
@@ -1,15 +1,17 @@
#' Declare a custom step
#'
#' With declare_step, you can include any function that takes data as one of its arguments and returns data in a design declaration. The first argument is always a "handler", which is the name of the data-in, data-out function.
#'
#' @inheritParams declare_internal_inherit_params
#' @return a function that returns a data.frame
#' @export
#' @importFrom fabricatr fabricate
#' @examples
#'
#' N <- 50
#' my_population <- declare_population(N = N, noise = rnorm(N))
#' my_assignment <- declare_assignment(m = 25)
#' my_step <- declare_step(Z2 = Z, q = 5)
#' my_step <- declare_step(fabricate, Z2 = Z, q = 5)
#'
#' design <- my_population + my_assignment + my_step
#'
declare_step <- make_declarations(fabricate, "custom")
declare_step <- make_declarations(function(data, ...f, ...) ...f(data, ...), "custom")
6 changes: 4 additions & 2 deletions R/design_helper_functions.R
Expand Up @@ -9,7 +9,7 @@
#' declare_potential_outcomes(Y ~ noise + Z * rnorm(N, 2, 2)) +
#' declare_sampling(n = 250) +
#' declare_estimand(ATE = mean(Y_Z_1 - Y_Z_0)) +
#' tidy_step(dplyr::mutate(noise_sq = noise^2)) +
#' declare_step(dplyr::mutate, noise_sq = noise^2) +
#' declare_assignment(m = 25) +
#' declare_reveal() +
#' declare_estimator(Y ~ Z, estimand = "my_estimand")
Expand Down Expand Up @@ -353,14 +353,16 @@ print.design <- function(x, verbose = TRUE, ...) {
#' my_estimand <- declare_estimand(ATE = mean(Y_Z_1 - Y_Z_0))
#'
#' my_estimator <- declare_estimator(Y ~ Z, estimand = my_estimand)
#'
#' my_mutate <- declare_step(dplyr::mutate, noise_sq = noise ^ 2)
#'
#' my_reveal <- declare_reveal()
#'
#' design <- my_population +
#' my_potential_outcomes +
#' my_sampling +
#' my_estimand +
#' tidy_step(dplyr::mutate(noise_sq = noise ^ 2)) +
#' my_mutate +
#' my_assignment +
#' my_reveal +
#' my_estimator
Expand Down
54 changes: 0 additions & 54 deletions R/tidy_step.R

This file was deleted.

7 changes: 4 additions & 3 deletions man/declare_step.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/plus-.dd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/post_design.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 0 additions & 38 deletions man/tidy_step.Rd

This file was deleted.

22 changes: 0 additions & 22 deletions tests/testthat/test-allow-custom-functions.R
Expand Up @@ -37,25 +37,3 @@ test_that("a dplyr pipeline can be used in a design", {

})


# case 3

test_that("a function call can be used in a design", {

`%dd%` <- DeclareDesign:::`+.dd`

my_func <- function(data, my_n = 50){
data %>% sample_n(n = my_n)
}

# errors before sending to our operator function
my_pop + my_func(my_n = 50)

# sends into our operator function
my_pop %dd% my_func(my_n = 50)

dat <- draw_data(des)

expect_equal(nrow(dat), 50)

})
14 changes: 3 additions & 11 deletions tests/testthat/test-declare-design.R
Expand Up @@ -18,13 +18,15 @@ test_that(

my_estimator <- declare_estimator(Y ~ Z, estimand = my_estimand)

my_mutate <- declare_step(dplyr::mutate, noise_sq = noise^2)

my_reveal <- declare_reveal()

design <- my_population +
my_potential_outcomes +
my_sampling +
my_estimand +
tidy_step(dplyr::mutate(q = 5)) +
my_mutate +
my_assignment +
my_reveal +
my_estimator
Expand Down Expand Up @@ -59,13 +61,3 @@ test_that("No estimators / estimands", {

})




test_that("Declare a bare function", {

expect_error(tidy_step(function(foo) foo), "The arguments of the tidy'd function do not include data or .data.")

})


2 changes: 1 addition & 1 deletion tests/testthat/test-declare-step.R
Expand Up @@ -3,7 +3,7 @@ context("declare step")
test_that("test declare step ", {
my_population <- declare_population(N = 50, noise = rnorm(N))
my_assignment <- declare_assignment(m = 25)
my_step <- declare_step(Z2 = Z, q = 5)
my_step <- declare_step(fabricate, Z2 = Z, q = 5)

design <- my_population + my_assignment + my_step
df <- draw_data(design)
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-demo.R
Expand Up @@ -97,7 +97,7 @@ test_that("demo runs", {
design <- my_population +
my_potential_outcomes +
my_estimand +
tidy_step(dplyr::mutate(big_income = 5*income)) +
declare_step(dplyr::mutate, big_income = 5*income) +
my_sampling +
my_assignment +
reveal_outcomes +
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-design-summary.R
Expand Up @@ -20,7 +20,7 @@ test_that("Basic design summary", {
my_potential_outcomes +
my_sampling +
my_estimand +
tidy_step(dplyr::mutate(q = 5)) +
declare_step(dplyr::mutate, q = 5) +
my_assignment +
reveal_outcomes +
my_estimator
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-diagnose-design.R
Expand Up @@ -24,7 +24,7 @@ test_that("allow design functions to be sent to simulate design and diagnose_des
my_potential_outcomes +
my_sampling +
my_estimand +
tidy_step(dplyr::mutate(q = 5)) +
declare_step(dplyr::mutate, q = 5) +
my_assignment +
my_reveal +
my_estimator
Expand Down
9 changes: 5 additions & 4 deletions tests/testthat/test-factorial.R
Expand Up @@ -24,10 +24,11 @@ test_that("Factorial", {
my_potential_outcomes +
my_estimand +
my_assignment +
tidy_step(dplyr::mutate(Z1 = as.numeric(Z %in% c("T2", "T4")),
Z2 = as.numeric(Z %in% c("T3", "T4")))) +
reveal_outcomes +
my_estimator
declare_step(dplyr::mutate,
Z1 = as.numeric(Z %in% c("T2", "T4")),
Z2 = as.numeric(Z %in% c("T3", "T4"))) +
reveal_outcomes +
my_estimator

expect_equal(my_design %>% draw_data %>% nrow, 2000)
expect_equal(my_design %>% run_design %>% names, c("estimates_df", "estimands_df"))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-modify-design.R
Expand Up @@ -13,7 +13,7 @@ test_that("test modify declare design ", {

design <- my_population +
my_potential_outcomes +
tidy_step(dplyr::mutate(q = 5)) +
declare_step(dplyr::mutate, q = 5) +
my_assignment

my_assignment_2 <- declare_assignment(m = 25, assignment_variable = "Z2")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-multiple-estimators.R
Expand Up @@ -12,7 +12,7 @@ test_that("Two estimators, Two estimands (matched)",{
subset = ID %in% 1:5) +
declare_estimand(CATE_6_10 = mean(extra[group == 2]) - mean(extra[group == 1]),
subset = ID %in% 6:10) +
declare_step(extra = extra + rnorm(N)) +
declare_step(fabricate, extra = extra + rnorm(N)) +
declare_estimator(
extra ~ group,
subset = ID %in% 1:5,
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-noncompliance.R
Expand Up @@ -46,7 +46,7 @@ test_that("Noncompliance", {
design <- my_population +
POS_Y +
POS_Z +
declare_step(complier = as.numeric(D_Z_0 == 0 & D_Z_1 == 1)) +
declare_step(fabricate, complier = as.numeric(D_Z_0 == 0 & D_Z_1 == 1)) +
ITT_d +
CACE +
my_assignment +
Expand Down

0 comments on commit dd3f055

Please sign in to comment.