Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Randomization process in STATA 18

    Hi all,

    i am new to this and i need to know if the commands i am using are correct to continue with the study .

    what i am trying to do is to do the randomization for some nurseries from different governorates. I should be randomizing the list of nurseries by infrastructure (whether the nursery has a kitchen or not) and by size (total number of children). this is the code i wrote:

    xtile nursery_size = total_children, n(3) // assigning tertiles of nursery size

    egen strata = group(nursery_size kitchen_binary )

    gen nursery_id = _n // _n = the row number of the nursery in the dataset

    set seed 9450 // setting the seed for reproducibility based on the timing now

    gen rand = runiform() //shuffling the list before the group assignment

    sort strata rand // nurseries wihtin the same stratum stay together but they are shuffled randomly within the stratum. //assigning 15 nurseries per treatment arm within each stratum. each treatment group is assigned based on the nursery's position (_n) in each stratum relative to the total number (_N) of nurseries in each stratum.

    gen treatment_group = .
    bysort strata (rand): replace treatment_group = 1 if _n <= _N/4 // Fortified sandwich (first 25%)
    bysort strata (rand): replace treatment_group = 2 if _n > _N/4 & _n <= _N/2 // Fortified sandwich + BCC (in first half but not first 25%)
    bysort strata (rand): replace treatment_group = 3 if _n > _N/2 & _n <= 3*_N/4 // Hot meals (between 50% and 75% of the stratum)
    bysort strata (rand): replace treatment_group = 0 if _n > 3*_N/4 // Control (last 25%)

    drop rand

    does this code make any sense? or should it be edited? would highly appreciate it if someone can help

  • #2
    Ganna:
    welcome to this forum.
    As per FAQ, and excerpt/example of your dataset (shared via CODE delimiters) could help enormously. Thanks.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Carlo Lazzaro I am sorry but i don't understand, could you kindly share this if it has already been asked before? i can't find it in the FAQ section

      Comment


      • #4
        Sure: Help - Statalist
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          The Stata package randomize offers some advanced features and might simplify your code drastically, check it out.

          Code:
          ssc install randomize, replace
          help randomize
          Best wishes

          (Stata 16.1 MP)

          Comment


          • #6
            It might be easier to create an indicator variable for the strata first. Then, preserve, keep in the dataset only strata ===1, run the randomization process, save the results, and restore. After that, repeat the process for strata==2,...,N. Once done, combine all the results.

            This approach is simpler, and allows investigators to follow more easily what has been implemented.

            Comment


            • #7
              there are a number of user-written routines, some of which allow you to specify a strata variable; in addition to -randomize- (mentioned in #5 above), you might look at -ralloc-, at -randtreat- or at -ccrand- (note that you give little info about your design so some of these might not be relevant); use -search- to find and download/install

              Comment

              Working...
              X