Skip to contents

Insert, delete and replace steps in an (already declared) design object.

Usage

insert_step(design, new_step, before, after)

delete_step(design, step)

replace_step(design, step, new_step)

Arguments

design

A design object, usually created using the + operator, expand_design, or the design library.

new_step

The new step; Either a function or a partial call.

before

The step before which to add steps.

after

The step after which to add steps.

step

The quoted label of the step to be deleted or replaced.

Value

A new design object.

Details

See modify_design for details.

Examples


 my_model <- 
   declare_model(
     N = 100, 
     U = rnorm(N),
     Y_Z_0 = U,
     Y_Z_1 = U + rnorm(N, mean = 2, sd = 2)
   )

 my_assignment <- declare_assignment(Z = complete_ra(N, m = 50))
 my_assignment_2 <- declare_assignment(Z = complete_ra(N, m = 25))

 design <- my_model + my_assignment

 draw_data(design)
#>      ID           U       Y_Z_0       Y_Z_1 Z
#> 1   001 -1.02624712 -1.02624712  3.81601446 1
#> 2   002  2.69821501  2.69821501  4.85396600 0
#> 3   003 -0.58670606 -0.58670606  0.75522359 0
#> 4   004  0.13346371  0.13346371  3.81154954 1
#> 5   005  0.46808953  0.46808953  1.88777330 1
#> 6   006  0.86869199  0.86869199  4.38653067 0
#> 7   007 -0.80237184 -0.80237184  1.99126239 1
#> 8   008 -0.17402737 -0.17402737 -1.99075824 0
#> 9   009 -1.04517787 -1.04517787  4.20490134 1
#> 10  010  0.97089947  0.97089947  2.29929012 1
#> 11  011  0.17671631  0.17671631  4.23151610 1
#> 12  012 -1.09805341 -1.09805341  0.54752962 0
#> 13  013 -0.07058643 -0.07058643  1.52715523 0
#> 14  014  0.28741585  0.28741585 -1.13105128 0
#> 15  015 -0.97186707 -0.97186707 -0.88167605 0
#> 16  016  0.27063879  0.27063879  4.84354103 1
#> 17  017 -1.79973026 -1.79973026  1.19269731 0
#> 18  018  3.59526336  3.59526336  7.38523218 1
#> 19  019  0.05264761  0.05264761  1.31861467 0
#> 20  020 -0.10362641 -0.10362641  0.84626984 1
#> 21  021 -0.32299909 -0.32299909  0.76396118 1
#> 22  022  1.43762028  1.43762028  2.49082901 1
#> 23  023 -0.51686174 -0.51686174  0.69100701 0
#> 24  024 -1.23120265 -1.23120265  1.55851968 1
#> 25  025  0.61896545  0.61896545  1.48833038 1
#> 26  026  2.18380657  2.18380657  5.35435711 1
#> 27  027  0.13695438  0.13695438  1.06760337 0
#> 28  028 -0.07914129 -0.07914129  2.13020131 0
#> 29  029 -0.11356890 -0.11356890  0.10484514 1
#> 30  030 -0.86452745 -0.86452745 -0.06874436 0
#> 31  031 -0.86136749 -0.86136749  2.43592094 0
#> 32  032 -0.80931566 -0.80931566  3.26692686 0
#> 33  033 -0.05328584 -0.05328584  2.11093889 1
#> 34  034 -0.09204005 -0.09204005  3.65561050 0
#> 35  035  0.18239398  0.18239398 -0.96481198 0
#> 36  036 -0.01833054 -0.01833054  5.95558855 0
#> 37  037  0.78009821  0.78009821  3.22233879 1
#> 38  038  0.97227254  0.97227254  5.01651797 1
#> 39  039 -0.50521002 -0.50521002  2.51463835 0
#> 40  040 -1.57086613 -1.57086613 -0.49203904 1
#> 41  041  0.34572214  0.34572214  3.15785306 1
#> 42  042  0.50808190  0.50808190  2.52814602 1
#> 43  043 -1.31503293 -1.31503293  2.99420301 1
#> 44  044  0.30944841  0.30944841  6.03171846 1
#> 45  045  1.43587819  1.43587819  5.53780452 0
#> 46  046  2.32953233  2.32953233  3.67220791 1
#> 47  047 -1.58977863 -1.58977863 -0.19852049 1
#> 48  048 -0.33145920 -0.33145920  0.83818190 0
#> 49  049  0.55948264  0.55948264  3.30500356 1
#> 50  050  0.83630254  0.83630254  3.00425227 1
#> 51  051  0.55184394  0.55184394  3.06183701 0
#> 52  052 -0.93088723 -0.93088723  2.98640011 0
#> 53  053  0.46946087  0.46946087  0.41756315 0
#> 54  054  0.10714849  0.10714849  2.73920267 0
#> 55  055  0.06500698  0.06500698  3.59094260 0
#> 56  056 -1.37126924 -1.37126924  0.01552290 1
#> 57  057 -0.47254728 -0.47254728  3.14775098 1
#> 58  058 -0.57883410 -0.57883410 -2.43004703 0
#> 59  059 -1.12146708 -1.12146708  0.17543741 1
#> 60  060 -0.46683401 -0.46683401  2.93661744 0
#> 61  061  0.26156089  0.26156089  5.27814955 0
#> 62  062  0.64048686  0.64048686  0.45478760 1
#> 63  063  0.45404258  0.45404258  2.22344860 0
#> 64  064  0.18376597  0.18376597  3.06126655 1
#> 65  065 -0.79361424 -0.79361424 -1.54897383 0
#> 66  066 -0.97231115 -0.97231115  3.12013999 0
#> 67  067  0.24819798  0.24819798 -1.45969607 1
#> 68  068  0.96705807  0.96705807  1.03051041 0
#> 69  069  0.23297456  0.23297456  1.88705766 0
#> 70  070 -0.31624525 -0.31624525  3.48965969 0
#> 71  071 -0.09835847 -0.09835847  2.38888523 1
#> 72  072 -0.42471634 -0.42471634  3.51801818 0
#> 73  073  0.18350906  0.18350906  0.66241080 1
#> 74  074  1.69884571  1.69884571  3.13452634 1
#> 75  075 -0.06499373 -0.06499373  2.62635355 1
#> 76  076 -1.30362169 -1.30362169  0.57699832 1
#> 77  077  0.86811734  0.86811734  2.07741180 1
#> 78  078  1.23951995  1.23951995  3.91409042 0
#> 79  079  0.41534532  0.41534532 -0.17800633 0
#> 80  080 -0.06976308 -0.06976308 -0.98095682 0
#> 81  081 -0.31924940 -0.31924940  1.85487884 0
#> 82  082  0.28867911  0.28867911 -1.35283488 0
#> 83  083 -1.28266514 -1.28266514 -0.57948341 1
#> 84  084 -0.85636359 -0.85636359  2.74193280 0
#> 85  085  0.20308678  0.20308678  2.18528577 0
#> 86  086  0.23772022  0.23772022  5.94183086 1
#> 87  087 -1.35126034 -1.35126034  3.61614882 0
#> 88  088  0.42593055  0.42593055  3.65514747 0
#> 89  089 -0.43179542 -0.43179542 -0.45598480 1
#> 90  090  1.27697779  1.27697779  3.37437267 1
#> 91  091 -0.28704180 -0.28704180  3.51919977 1
#> 92  092 -0.99204252 -0.99204252  3.55840977 0
#> 93  093 -0.08134308 -0.08134308 -2.24107566 0
#> 94  094 -0.43554017 -0.43554017  1.05980934 0
#> 95  095  1.42263364  1.42263364  3.87366022 1
#> 96  096  0.07030440  0.07030440  3.56286291 1
#> 97  097 -0.93266633 -0.93266633  2.18561310 0
#> 98  098 -1.59884003 -1.59884003 -1.81254799 1
#> 99  099  0.51875666  0.51875666  7.25546269 1
#> 100 100  0.31261746  0.31261746  1.50025743 1
 
 design_modified <- replace_step(design, 2, my_assignment_2)
 
 draw_data(design)
#>      ID            U        Y_Z_0      Y_Z_1 Z
#> 1   001  1.381959522  1.381959522  1.1775568 0
#> 2   002 -0.270233694 -0.270233694  5.4610158 1
#> 3   003 -0.659018230 -0.659018230 -0.6370562 0
#> 4   004 -0.883031208 -0.883031208  1.3776870 1
#> 5   005  1.740929094  1.740929094  5.6910883 0
#> 6   006 -0.702283319 -0.702283319  3.9658673 0
#> 7   007 -1.678721131 -1.678721131  2.8785567 1
#> 8   008 -0.839690808 -0.839690808 -0.4555936 0
#> 9   009 -1.101467473 -1.101467473  0.5930349 0
#> 10  010 -1.716098828 -1.716098828 -1.7235728 0
#> 11  011 -0.485563759 -0.485563759  1.3275427 1
#> 12  012 -0.296316543 -0.296316543  1.2278733 0
#> 13  013 -0.488907296 -0.488907296  1.3932471 0
#> 14  014 -0.361547225 -0.361547225  2.4711364 1
#> 15  015 -0.492315606 -0.492315606  0.9195075 1
#> 16  016 -1.106327910 -1.106327910 -1.2160637 1
#> 17  017  0.542465188  0.542465188  6.6708122 1
#> 18  018  1.135317489  1.135317489  2.4107388 0
#> 19  019  1.712542667  1.712542667  4.6995405 0
#> 20  020  1.931609719  1.931609719  3.0226505 0
#> 21  021 -0.465217546 -0.465217546  2.0183466 1
#> 22  022 -0.278702239 -0.278702239  0.4142692 0
#> 23  023 -0.215237702 -0.215237702 -0.1237043 1
#> 24  024  0.768336212  0.768336212  0.6363903 0
#> 25  025 -0.683955162 -0.683955162 -0.1273476 1
#> 26  026  0.345736036  0.345736036  1.4878648 1
#> 27  027  0.860141824  0.860141824  1.7787616 0
#> 28  028 -0.758983881 -0.758983881  0.6489313 1
#> 29  029  0.410312605  0.410312605  3.4775951 1
#> 30  030  1.372511516  1.372511516  2.1167731 1
#> 31  031  1.633178083  1.633178083  3.0675176 0
#> 32  032  1.025147332  1.025147332  2.8160297 0
#> 33  033  0.025529316  0.025529316  3.0239501 1
#> 34  034 -0.426372713 -0.426372713 -0.5191580 0
#> 35  035 -1.396132476 -1.396132476  2.0768577 1
#> 36  036 -0.168638071 -0.168638071 -1.7961993 1
#> 37  037  2.434329655  2.434329655  4.0961470 0
#> 38  038  0.807843777  0.807843777  4.4638762 1
#> 39  039  0.356253261  0.356253261 -0.3281659 0
#> 40  040 -1.628434409 -1.628434409 -1.1339474 1
#> 41  041 -0.203627798 -0.203627798 -0.4301043 0
#> 42  042  2.263222747  2.263222747  1.2933561 1
#> 43  043 -0.250778197 -0.250778197  1.9433483 0
#> 44  044  0.149609992  0.149609992  5.1432069 1
#> 45  045 -0.740363986 -0.740363986  5.9641299 0
#> 46  046 -1.402994633 -1.402994633  1.1711477 0
#> 47  047  0.005404442  0.005404442 -1.9377860 1
#> 48  048  0.332382295  0.332382295  2.9940102 0
#> 49  049 -0.552716474 -0.552716474  1.8944859 0
#> 50  050 -1.145323514 -1.145323514  1.1892342 1
#> 51  051 -1.947909452 -1.947909452  0.1077584 1
#> 52  052  0.667138308  0.667138308  0.1925762 0
#> 53  053 -0.627774727 -0.627774727  0.2587327 1
#> 54  054  0.039702975  0.039702975  5.4606865 1
#> 55  055  1.203124986  1.203124986  4.4376395 0
#> 56  056  1.462178734  1.462178734  3.2182276 1
#> 57  057  0.314710079  0.314710079  3.8346610 1
#> 58  058  0.584208481  0.584208481  1.8252497 0
#> 59  059 -0.322932209 -0.322932209 -0.1980250 1
#> 60  060 -0.978638176 -0.978638176 -3.3376118 1
#> 61  061 -0.436544545 -0.436544545 -2.1048638 1
#> 62  062  0.244983643  0.244983643  0.9560768 0
#> 63  063 -1.409306032 -1.409306032 -0.2979871 1
#> 64  064 -1.001751448 -1.001751448 -0.6221681 0
#> 65  065 -0.473792828 -0.473792828  2.1678761 1
#> 66  066  1.244686597  1.244686597  0.4182112 1
#> 67  067 -0.831281878 -0.831281878  0.3115454 0
#> 68  068  0.203936855  0.203936855  4.4549692 0
#> 69  069  0.518249762  0.518249762  1.1933338 0
#> 70  070 -1.065746650 -1.065746650  0.7740812 0
#> 71  071 -0.220438445 -0.220438445  2.5325294 1
#> 72  072  0.966818316  0.966818316  4.8789573 0
#> 73  073  1.674064104  1.674064104  1.6633460 1
#> 74  074  0.075804028  0.075804028  0.6508087 1
#> 75  075  1.451011655  1.451011655  5.1951844 1
#> 76  076  0.853951653  0.853951653  4.3539685 0
#> 77  077 -1.175784141 -1.175784141  0.7378444 0
#> 78  078  0.086623832  0.086623832  5.5390727 1
#> 79  079 -0.270853964 -0.270853964 -1.9371986 1
#> 80  080 -0.521132653 -0.521132653  4.3846010 0
#> 81  081  0.550609623  0.550609623  1.1243984 1
#> 82  082  0.344768468  0.344768468  4.5081095 0
#> 83  083  0.306922065  0.306922065  2.6872661 1
#> 84  084  1.251051781  1.251051781  5.7650943 0
#> 85  085 -1.009193817 -1.009193817  1.4495266 1
#> 86  086 -0.559260916 -0.559260916  2.0021389 1
#> 87  087 -0.476168834 -0.476168834  1.5308481 0
#> 88  088  0.115424095  0.115424095  3.7888496 0
#> 89  089  1.133806504  1.133806504  3.3269583 1
#> 90  090 -0.607712654 -0.607712654 -1.3762844 0
#> 91  091  1.441005480  1.441005480  5.8248282 0
#> 92  092 -0.219202969 -0.219202969 -0.1331686 0
#> 93  093  0.137221926  0.137221926  2.8729107 1
#> 94  094  0.003397613  0.003397613  1.9430760 0
#> 95  095 -1.513956431 -1.513956431 -2.9401120 0
#> 96  096 -0.445123871 -0.445123871  0.6317582 1
#> 97  097  0.110982741  0.110982741  3.1845293 0
#> 98  098 -1.707963272 -1.707963272 -0.1799702 1
#> 99  099  0.427144323  0.427144323  2.0877073 0
#> 100 100  0.614444046  0.614444046 -1.1339667 1
 
 if (FALSE) { # \dontrun{
 
 design <- 
   declare_model(
     N = 100,
     U = rnorm(N),
     potential_outcomes(Y ~ 0.20 * Z + U)
   ) + 
     declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0)) + 
     declare_assignment(Z = complete_ra(N, m = N/2)) + 
     declare_measurement(Y = reveal_outcomes(Y ~ Z)) + 
     declare_estimator(Y ~ Z, inquiry = "ATE")
 
 insert_step(design, declare_sampling(S = complete_rs(N, n = 50)),
             after = 1)

 # If you are using a design created by a designer, for example from
 # the DesignLibrary package, you will not have access to the step
 # objects. Instead, you can always use the label of the step.
 
 design <- DesignLibrary::two_arm_designer()
 
 # get the labels for the steps
 names(design)
 
 insert_step(design, 
   declare_sampling(S = complete_rs(N, n = 50)),
   after = "potential_outcomes")
   
 } # }


design <- 
  declare_model(
    N = 100,
    U = rnorm(N),
    potential_outcomes(Y ~ 0.20 * Z + U)
  ) + 
    declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0)) + 
    declare_assignment(Z = complete_ra(N, m = N/2)) + 
    declare_measurement(Y = reveal_outcomes(Y ~ Z)) + 
    declare_estimator(Y ~ Z, inquiry = "ATE")
delete_step(design, step = 5)
#> 
#> Research design declaration summary
#> 
#> Step 1 (model): declare_model(N = 100, U = rnorm(N), potential_outcomes(Y ~ 0.2 * Z + U)) 
#> 
#> Step 2 (inquiry): declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0)) -------------------
#> 
#> Step 3 (assignment): declare_assignment(Z = complete_ra(N, m = N/2)) -----------
#> 
#> Step 4 (measurement): declare_measurement(Y = reveal_outcomes(Y ~ Z)) ----------
#> 
#> Run of the design:
#> 
#>  inquiry estimand
#>      ATE      0.2
#> 
#> No modifiable parameters saved in design 


design <- 
  declare_model(
    N = 100,
    U = rnorm(N),
    potential_outcomes(Y ~ 0.20 * Z + U)
  ) + 
    declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0)) + 
    declare_assignment(Z = complete_ra(N, m = N/2)) + 
    declare_measurement(Y = reveal_outcomes(Y ~ Z)) + 
    declare_estimator(Y ~ Z, inquiry = "ATE")

replace_step(
  design, 
  step = 3, 
  new_step = declare_assignment(Z = simple_ra(N, prob = 0.5)))
#> 
#> Research design declaration summary
#> 
#> Step 1 (model): declare_model(N = 100, U = rnorm(N), potential_outcomes(Y ~ 0.2 * Z + U)) 
#> 
#> Step 2 (inquiry): declare_inquiry(ATE = mean(Y_Z_1 - Y_Z_0)) -------------------
#> 
#> Step 3 (assignment): declare_assignment(Z = simple_ra(N, prob = 0.5)) ----------
#> 
#> Step 4 (measurement): declare_measurement(Y = reveal_outcomes(Y ~ Z)) ----------
#> 
#> Step 5 (estimator): declare_estimator(Y ~ Z, inquiry = "ATE") ------------------
#> 
#> Run of the design:
#> 
#>  inquiry estimand estimator term estimate std.error statistic p.value conf.low
#>      ATE      0.2 estimator    Z    0.088      0.23     0.383   0.702   -0.368
#>  conf.high df outcome
#>      0.544 98       Y
#> 
#> No modifiable parameters saved in design