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.0557648146  1.0557648146  4.13068330 0
#> 2   002 -1.2041543007 -1.2041543007 -2.98083209 0
#> 3   003 -0.2219172322 -0.2219172322  3.23219208 0
#> 4   004 -0.9824607144 -0.9824607144  3.02613674 1
#> 5   005  1.3135059845  1.3135059845  6.45042007 1
#> 6   006  0.5235882557  0.5235882557  3.76438618 0
#> 7   007 -0.4563831020 -0.4563831020  4.45130623 1
#> 8   008 -0.1658107398 -0.1658107398 -0.31709837 0
#> 9   009 -1.2863207110 -1.2863207110  1.74769872 0
#> 10  010  0.7133780515  0.7133780515  3.31770331 1
#> 11  011  0.0651739025  0.0651739025  2.59339297 0
#> 12  012  1.2841444299  1.2841444299  4.98466342 0
#> 13  013  0.4786947723  0.4786947723  3.21542743 0
#> 14  014 -0.4259549131 -0.4259549131  0.52767185 1
#> 15  015 -0.0297104064 -0.0297104064 -0.65361147 0
#> 16  016  1.0686731368  1.0686731368  6.41289894 0
#> 17  017  0.7769835878  0.7769835878  1.39412884 1
#> 18  018  0.2776454850  0.2776454850  0.15881726 0
#> 19  019 -0.1173885488 -0.1173885488  3.96181609 0
#> 20  020  0.2262515622  0.2262515622  2.80765195 1
#> 21  021  0.5999789111  0.5999789111  3.32275532 1
#> 22  022 -0.6974498544 -0.6974498544  0.47544151 1
#> 23  023  1.5282643664  1.5282643664  6.45869039 1
#> 24  024 -0.0458642623 -0.0458642623  4.80402790 1
#> 25  025  0.2967305713  0.2967305713  2.49828096 0
#> 26  026  0.0004382916  0.0004382916  2.59904044 1
#> 27  027 -0.6568342182 -0.6568342182  2.74069317 0
#> 28  028  1.0400107880  1.0400107880  3.98587165 1
#> 29  029 -0.3681429804 -0.3681429804  1.56088870 1
#> 30  030  1.1670996966  1.1670996966  0.24335657 1
#> 31  031 -2.0916406802 -2.0916406802 -1.65563713 1
#> 32  032 -0.3038961909 -0.3038961909  3.07610092 1
#> 33  033  0.8885799915  0.8885799915  1.01757250 1
#> 34  034 -0.7540059583 -0.7540059583  0.19090951 1
#> 35  035  0.7559153706  0.7559153706  1.37274293 1
#> 36  036 -0.0962283242 -0.0962283242 -1.13645286 1
#> 37  037 -1.1775877854 -1.1775877854 -1.23555125 0
#> 38  038  1.1511887415  1.1511887415  2.91560256 1
#> 39  039 -1.4373844673 -1.4373844673 -0.32268694 1
#> 40  040 -0.3127468323 -0.3127468323  6.15977351 0
#> 41  041 -0.6644142282 -0.6644142282  1.97792063 1
#> 42  042 -0.9401360644 -0.9401360644  3.68808687 0
#> 43  043  1.6551135156  1.6551135156  5.77058040 1
#> 44  044  0.5499690331  0.5499690331  0.44247041 0
#> 45  045 -0.3623054563 -0.3623054563  0.22577873 0
#> 46  046  0.6448137987  0.6448137987  2.24451533 0
#> 47  047  0.4657680266  0.4657680266  4.53784931 0
#> 48  048 -0.1274661864 -0.1274661864  4.18879884 0
#> 49  049 -1.2420892054 -1.2420892054  0.24296173 0
#> 50  050 -2.1967179128 -2.1967179128  0.07982102 1
#> 51  051 -0.5707402446 -0.5707402446  3.29749704 0
#> 52  052 -0.2028381752 -0.2028381752  3.35230088 0
#> 53  053  0.2509295680  0.2509295680  4.27803847 0
#> 54  054  0.5828057745  0.5828057745  2.50635983 0
#> 55  055 -0.5523831301 -0.5523831301 -3.34686818 0
#> 56  056 -0.3648312517 -0.3648312517  1.67841326 1
#> 57  057 -2.3391057131 -2.3391057131  0.05889155 0
#> 58  058  0.6400764074  0.6400764074  2.50815835 1
#> 59  059 -0.0567857904 -0.0567857904  4.14359353 0
#> 60  060 -0.3722907022 -0.3722907022  1.41192149 1
#> 61  061 -0.6799643957 -0.6799643957  3.44865738 1
#> 62  062  0.2906825064  0.2906825064  3.01049964 0
#> 63  063  0.2167898196  0.2167898196  4.84215148 0
#> 64  064  0.2276709711  0.2276709711  3.56708341 1
#> 65  065  1.5194823942  1.5194823942  6.28545260 1
#> 66  066 -0.7222060877 -0.7222060877  2.01387276 0
#> 67  067  0.4007120451  0.4007120451  0.31479961 1
#> 68  068 -1.7211768856 -1.7211768856  2.30845480 1
#> 69  069 -0.0138401780 -0.0138401780  1.98340505 1
#> 70  070 -0.4231698457 -0.4231698457 -0.81502385 0
#> 71  071 -1.1754862392 -1.1754862392 -2.77679271 0
#> 72  072 -0.7380578765 -0.7380578765  3.31294716 0
#> 73  073 -1.0106253654 -1.0106253654  3.18460925 1
#> 74  074 -0.2318845978 -0.2318845978 -0.07105635 0
#> 75  075  1.2957412047  1.2957412047  7.56867198 0
#> 76  076  2.3680950871  2.3680950871  3.66256063 0
#> 77  077 -1.2381115002 -1.2381115002  0.07596947 1
#> 78  078  0.4269229195  0.4269229195  0.45330241 1
#> 79  079 -0.6883743016 -0.6883743016  1.54062677 0
#> 80  080  0.4803652725  0.4803652725  1.87656159 1
#> 81  081 -0.5058851451 -0.5058851451  0.64588485 1
#> 82  082 -1.4828512797 -1.4828512797  1.16690463 0
#> 83  083  0.5914922360  0.5914922360 -1.70216630 0
#> 84  084  1.8114201513  1.8114201513  3.58411836 1
#> 85  085 -0.6898119794 -0.6898119794  2.48937464 1
#> 86  086 -1.5977899714 -1.5977899714 -1.23905301 0
#> 87  087 -0.9995042992 -0.9995042992  0.57725845 0
#> 88  088 -0.6030947483 -0.6030947483  1.36870757 0
#> 89  089 -1.1249083217 -1.1249083217  0.60676849 1
#> 90  090  0.8687806713  0.8687806713  0.21452787 0
#> 91  091 -0.3616779625 -0.3616779625  1.47919012 0
#> 92  092 -0.8556794358 -0.8556794358  0.64135429 1
#> 93  093  2.1354465506  2.1354465506  5.07245071 1
#> 94  094  0.5137863186  0.5137863186 -0.39639117 0
#> 95  095 -0.5924127920 -0.5924127920  3.01705808 1
#> 96  096 -0.2033279395 -0.2033279395  3.61301498 0
#> 97  097 -0.7535537044 -0.7535537044 -0.31364833 1
#> 98  098 -0.5915549915 -0.5915549915  2.04160977 1
#> 99  099 -0.6836084903 -0.6836084903  3.55529877 1
#> 100 100 -0.0535903234 -0.0535903234  5.49658410 1
 
 design_modified <- replace_step(design, 2, my_assignment_2)
 
 draw_data(design)
#>      ID            U        Y_Z_0        Y_Z_1 Z
#> 1   001 -0.005390775 -0.005390775 -0.887400181 1
#> 2   002 -0.203131043 -0.203131043  2.340872106 1
#> 3   003 -2.518741475 -2.518741475 -1.903311413 1
#> 4   004  1.138341886  1.138341886  1.975354938 1
#> 5   005 -0.452576398 -0.452576398 -1.242422037 0
#> 6   006 -0.962793721 -0.962793721  2.317896618 0
#> 7   007 -1.067573816 -1.067573816 -3.690913289 1
#> 8   008 -0.006817742 -0.006817742  2.909691089 1
#> 9   009 -0.987444655 -0.987444655 -0.286810044 1
#> 10  010  2.621672876  2.621672876  2.195890088 0
#> 11  011 -1.089103689 -1.089103689 -1.114919063 1
#> 12  012  1.281289189  1.281289189  2.107286106 1
#> 13  013  1.898194419  1.898194419  2.957156396 0
#> 14  014  0.101060886  0.101060886  3.472473050 1
#> 15  015 -0.882328716 -0.882328716  0.569333745 1
#> 16  016  0.532369401  0.532369401  2.512259992 1
#> 17  017  0.287178087  0.287178087  1.395760507 0
#> 18  018  1.923649433  1.923649433  5.625713032 0
#> 19  019 -0.653623117 -0.653623117 -2.776767021 1
#> 20  020  0.235884818  0.235884818  3.020238160 1
#> 21  021 -0.097388741 -0.097388741  1.085434588 1
#> 22  022 -0.020985348 -0.020985348  1.155378661 1
#> 23  023  1.257036956  1.257036956  4.753048019 1
#> 24  024  0.086122440  0.086122440  4.484682354 1
#> 25  025 -0.237735634 -0.237735634  1.159073069 1
#> 26  026  0.549312241  0.549312241  0.712011368 0
#> 27  027 -0.446672620 -0.446672620  5.283934780 1
#> 28  028 -0.710892756 -0.710892756  0.440399670 1
#> 29  029  0.420807033  0.420807033  8.405692692 1
#> 30  030  0.759226888  0.759226888  1.180361035 0
#> 31  031  0.955231964  0.955231964  4.036797915 0
#> 32  032 -1.196881983 -1.196881983 -4.628752459 1
#> 33  033  0.775033246  0.775033246 -0.000647647 0
#> 34  034  0.174844648  0.174844648  4.294892885 1
#> 35  035 -0.695422040 -0.695422040  3.364604599 0
#> 36  036 -0.222536586 -0.222536586 -3.432179891 0
#> 37  037 -0.632694526 -0.632694526  4.323653662 0
#> 38  038  0.407235674  0.407235674  3.701049525 0
#> 39  039 -0.648210797 -0.648210797  2.479294374 1
#> 40  040  0.436791930  0.436791930  7.353343191 1
#> 41  041 -0.082176451 -0.082176451  5.875837641 0
#> 42  042  0.017807959  0.017807959  5.284303661 1
#> 43  043  1.365421873  1.365421873  3.896120620 1
#> 44  044  1.042770770  1.042770770  0.502354364 0
#> 45  045  0.673398731  0.673398731  4.157736059 1
#> 46  046  0.084206704  0.084206704  1.741523072 0
#> 47  047  0.315010076  0.315010076  2.146434708 0
#> 48  048  0.080343593  0.080343593  1.369495234 1
#> 49  049 -0.991117668 -0.991117668  0.438234659 1
#> 50  050 -0.349930135 -0.349930135  3.559115206 1
#> 51  051 -0.683567045 -0.683567045  1.520235733 1
#> 52  052 -0.014281046 -0.014281046 -0.214642871 0
#> 53  053 -0.614937652 -0.614937652  1.168790666 0
#> 54  054  0.196932210  0.196932210  4.697957942 0
#> 55  055  0.842169406  0.842169406  1.049893495 1
#> 56  056  0.459826987  0.459826987  3.789702055 0
#> 57  057  0.425710108  0.425710108  3.384444446 0
#> 58  058 -1.877763012 -1.877763012  0.981588037 1
#> 59  059  2.017194353  2.017194353  3.691516774 0
#> 60  060 -0.975002667 -0.975002667  0.956854011 0
#> 61  061  0.485431668  0.485431668  3.779121326 0
#> 62  062  0.653330681  0.653330681  2.994897964 0
#> 63  063 -0.752953453 -0.752953453  2.396555667 0
#> 64  064  0.397994089  0.397994089  3.892846329 0
#> 65  065  0.499298129  0.499298129  2.499571482 1
#> 66  066  0.161834044  0.161834044  5.897124570 0
#> 67  067 -1.255932976 -1.255932976  4.263330081 1
#> 68  068  0.465746076  0.465746076  2.310192806 1
#> 69  069  0.004170127  0.004170127  1.725703401 1
#> 70  070  0.152160094  0.152160094  2.773151299 0
#> 71  071 -0.420789742 -0.420789742  1.558084941 0
#> 72  072 -0.181145083 -0.181145083  4.629003654 0
#> 73  073  0.945305169  0.945305169  1.480160481 1
#> 74  074 -0.558223549 -0.558223549  1.659136095 1
#> 75  075  0.227361215  0.227361215  3.292424017 0
#> 76  076 -0.528083856 -0.528083856  0.113752239 1
#> 77  077 -0.401479245 -0.401479245  2.252437149 1
#> 78  078 -0.419249667 -0.419249667  3.195038989 0
#> 79  079 -0.280384254 -0.280384254  2.031389268 1
#> 80  080 -0.044008863 -0.044008863  2.697321297 0
#> 81  081 -0.313848163 -0.313848163  5.243729908 0
#> 82  082  0.007211932  0.007211932  5.319172185 1
#> 83  083 -0.280806508 -0.280806508 -0.233946326 0
#> 84  084  0.475873057  0.475873057  3.273039671 0
#> 85  085 -0.116402194 -0.116402194 -1.884753118 0
#> 86  086  0.849828175  0.849828175  4.686505554 0
#> 87  087 -1.340510884 -1.340510884 -0.292468981 1
#> 88  088 -1.357123048 -1.357123048  1.884131981 0
#> 89  089  0.699354497  0.699354497  2.529953909 0
#> 90  090 -0.450414570 -0.450414570 -2.277878744 0
#> 91  091  0.613040203  0.613040203  5.379965882 0
#> 92  092  0.659108565  0.659108565  5.517129719 0
#> 93  093 -3.031273247 -3.031273247 -4.478948241 1
#> 94  094  0.304154325  0.304154325  4.595724863 1
#> 95  095 -2.676150686 -2.676150686 -1.760395563 1
#> 96  096  1.959778323  1.959778323  5.907112444 0
#> 97  097  0.759083998  0.759083998  2.788143369 1
#> 98  098  1.743942112  1.743942112  2.471450619 0
#> 99  099 -1.050069639 -1.050069639  1.874968611 0
#> 100 100 -0.117426314 -0.117426314  3.341807892 0
 
 if (FALSE) {
 
 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
#> 


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.311     0.199      1.56   0.122  -0.0845
#>  conf.high df outcome
#>      0.707 98       Y
#>